Exemplo n.º 1
0
        public void CreateAndSplitBillAsync_WhenCreateNewGroupIsNotValid_ShouldThrow()
        {
            //Arrange
            var sutBuilder = new BillServiceSutBuilder();

            var createNewBill = new CreateNewBill();
            var billService   = sutBuilder.CreateSut();

            //Act & Assert
            var exception = Assert.ThrowsAsync <ValidationException>(async() => await billService.CreateAndSplitBillAsync(createNewBill));
        }
Exemplo n.º 2
0
        public async Task <Bill> CreateAndSplitBillAsync(CreateNewBill newBillData)
        {
            newBillData.Validate();

            var group = await groupRepository.GetByIdAsync(newBillData.GroupContextId);

            if (group == null)
            {
                throw new NotFoundException($"Group with id {newBillData.GroupContextId} does not exist.");
            }

            var loaner = group.Users.FirstOrDefault(user => user.Id == newBillData.LoanerId);

            if (loaner == null)
            {
                throw new NotFoundException($"Payee with id {newBillData.LoanerId} does not exist in group.");
            }

            var bill = new Bill()
            {
                Name           = newBillData.Name,
                Total          = newBillData.Total,
                LoanerId       = loaner.Id,
                Loaner         = loaner,
                GroupContextId = group.Id,
                GroupContext   = group,
            };

            bill = await billRepository.AddAsync(bill);

            var loans = SplitBill(bill);

            loans.ForEach(loan => bill.Loans.Add(loan));

            await billRepository.SaveChangesAsync();

            return(bill);
        }
Exemplo n.º 3
0
        public async Task <ActionResult <ApiBill> > CreateBill([FromBody] CreateNewBill newBillData)
        {
            var bill = await billService.CreateAndSplitBillAsync(newBillData);

            return(mapper.Map <ApiBill>(bill));
        }