public async Task <int> Handle(UpdatePersonBillingRequest request, CancellationToken cancellationToken)
        {
            var person = await _splitThatBillContext.People
                         .Include(i => i.PersonBillItems)
                         .FirstOrDefaultAsync(p => p.Id == request.PersonId);

            if (null == person)
            {
                throw new NullReferenceException("The person does not exist.");
            }

            var toRemove = person.PersonBillItems.Where(pbi => !request.PersonBilling.BillItems.Any(bi => bi.Id == pbi.BillItemId))
                           .ToList();

            toRemove.ForEach(item =>
            {
                _splitThatBillContext.Entry(item).State = EntityState.Deleted;
            });

            request.PersonBilling.BillItems.ForEach(item =>
            {
                if (!person.PersonBillItems.Any(pb => pb.BillItemId == item.Id))
                {
                    var billItem = _mapper.Map <BillItem>(item);
                    _splitThatBillContext.Attach(billItem);
                    person.AddBillItem(billItem, item.Amount);
                }
            });

            return(await _splitThatBillContext.SaveChangesAsync());
        }
        public async Task <BillDto> Handle(CreateBillRequest request, CancellationToken cancellationToken)
        {
            var billItems = request.BillFormModel.BillItems.Select(item =>
            {
                var billItem = new BillItem(item.BillItem.Description, item.BillItem.Amount, item.BillItem.Discount);

                if (item.PersonId > 0)
                {
                    billItem.AssignPerson(item.PersonId, item.BillItem.Amount);
                }
                return(billItem);
            }).ToList();
            var billToCreate = new Bill(request.BillFormModel.EstablishmentName, request.BillFormModel.BillDate, billItems, request.BillFormModel.Currency);

            billToCreate.Remarks = request.BillFormModel.Remarks;
            billToCreate.SetExternalId(_externalIdGenerator.Generate());


            billToCreate.SetBillItems(billItems);
            billToCreate.SetExtraCharges(request.BillFormModel.ExtraCharges.Select(item => new Core.OwnedEntities.ExtraCharge(item.Description, item.Rate)).ToList());

            request.BillFormModel.Participants.ForEach(participant =>
            {
                billToCreate.AddParticipant(participant.Person.Id);
            });

            _splitThatBillContext.Attach(_contextData.CurrentUser);
            billToCreate.UpdateBillTaker(_contextData.CurrentUser);

            _splitThatBillContext.Add(billToCreate);

            await _splitThatBillContext.SaveChangesAsync();

            return(_mapper.Map <BillDto>(billToCreate));
        }
Ejemplo n.º 3
0
        public async Task <bool> Handle(UpdateBillItemRequest request, CancellationToken cancellationToken)
        {
            var bill = await _splitThatBillContext.Bills
                       .Include(i => i.BillItems)
                       .FirstOrDefaultAsync(item => item.Id == request.BillId);

            if (null == bill)
            {
                throw new NullReferenceException("The bill to which this bill item belongs does not exist.");
            }

            var billItem = bill.BillItems.Find(item => item.Id == request.BillItemId);

            if (null == billItem)
            {
                throw new NullReferenceException("The bill item to update does not exist.");
            }

            _mapper.Map(request.BillItemFormModel, billItem);

            _splitThatBillContext.Entry(bill).State = EntityState.Modified;
            var result = await _splitThatBillContext.SaveChangesAsync();

            return(result > 0);
        }
        public async Task <bool> Handle(UpdateBillRequest request, CancellationToken cancellationToken)
        {
            var bill = await _splitThatBillContext.Bills
                       .Include(i => i.BillItems)
                       .Include(i => i.ExtraCharges)
                       .Include(i => i.Participants)
                       .FirstOrDefaultAsync(b => b.Id == request.Id);

            if (null == bill)
            {
                throw new NullReferenceException("The bill to be updated does not exist.");
            }

            RemoveExtraCharges(bill, request.BillFormModel.ExtraCharges);
            // RemoveBillItems(bill, request.BillFormModel.BillItems);
            RemoveParticipants(bill, request.BillFormModel.Participants);

            bill.Remarks = request.BillFormModel.Remarks;
            //bill.Update(request.BillFormModel.EstablishmentName,
            //    request.BillFormModel.BillDate,
            //    request.BillFormModel.BillItems.Select(item =>
            //    {
            //        var billItem = bill.BillItems.Find(bi => bi.Id == item.Id);
            //        if (billItem is object)
            //        {
            //            billItem.Update(item.Description, item.Amount, item.Discount);
            //            return billItem;
            //        }
            //        return new BillItem(item.Description, item.Amount, item.Discount);
            //    }).ToList(),
            //    request.BillFormModel.ExtraCharges.Select(item =>
            //    {
            //        var extraCharge = bill.ExtraCharges.Find(ec => ec.Id == item.Id);
            //        if (extraCharge is object)
            //        {
            //            extraCharge.Update(item.Description, item.Rate);
            //            return extraCharge;
            //        }
            //        return new ExtraCharge(item.Description, item.Rate);
            //    }).ToList());
            bill.UpdateParticipants(request.BillFormModel.Participants.Select(p =>
            {
                var billParticipant = bill.Participants.Find(bp => bp.Id == p.Id);
                if (billParticipant is object)
                {
                    return(billParticipant);
                }
                return(new BillParticipant(bill, p.Person.Id));
            }).ToList());
            _splitThatBillContext.Entry(bill).State = EntityState.Modified;

            return(await _splitThatBillContext.SaveChangesAsync() > 0);
        }
Ejemplo n.º 5
0
        public async Task <PersonDto> Handle(CreatePersonRequest request, CancellationToken cancellationToken)
        {
            var person = new Person(request.PersonForm.Firstname, request.PersonForm.Lastname);

            person.SetExternalId(_externalIdGenerator.Generate());

            _splitThatBillContext.People.Add(person);
            await _splitThatBillContext.SaveChangesAsync();

            var personDto = _mapper.Map <PersonDto>(person);

            return(personDto);
        }
        public async Task <bool> Handle(DeleteBillRequest request, CancellationToken cancellationToken)
        {
            var item = await _splitThatBillContext.Bills.FindAsync(request.Id);

            if (null == item)
            {
                throw new NullReferenceException("The bill to be deleted does not exist.");
            }

            _splitThatBillContext.Entry(item).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;

            return(await _splitThatBillContext.SaveChangesAsync() > 0);
        }
Ejemplo n.º 7
0
        public async Task <PersonDto> Handle(UpdatePersonRequest request, CancellationToken cancellationToken)
        {
            var person = await _splitThatBillContext.People.FindAsync(request.Id);

            if (null == person)
            {
                throw new NullReferenceException("The person is not found.");
            }

            person.Update(request.PersonForm.Firstname, request.PersonForm.Lastname);

            await _splitThatBillContext.SaveChangesAsync();

            return(_mapper.Map <PersonDto>(person));
        }
Ejemplo n.º 8
0
        public async Task <int> Handle(DeletePaymentDetailsRequest request, CancellationToken cancellationToken)
        {
            var person = await _splitThatBillContext.People
                         .Include(i => i.PaymentDetails)
                         .FirstOrDefaultAsync(p => p.Id == request.PersonId);

            if (null == person)
            {
                throw new NullReferenceException("The person does not exist.");
            }

            person.RemovePaymentDetail(request.PaymentDetailsId);

            _splitThatBillContext.Entry(person).State = EntityState.Modified;

            return(await _splitThatBillContext.SaveChangesAsync());
        }
        public async Task <int> Handle(UpdatePaymentDetailRequest request, CancellationToken cancellationToken)
        {
            var person = await _splitThatBillContext.People
                         .Include(i => i.PaymentDetails)
                         .FirstOrDefaultAsync(p => p.Id == request.PersonId);

            if (null == person)
            {
                throw new NullReferenceException("The person does not exist.");
            }

            person.UpdatePaymentDetail(request.PaymentDetailId, PaymentDetail.Create(
                                           request.PersonPaymentDetailFormModel.PaymentDetail.BankName,
                                           request.PersonPaymentDetailFormModel.PaymentDetail.AccountNumber,
                                           request.PersonPaymentDetailFormModel.PaymentDetail.AccountName
                                           ));

            return(await _splitThatBillContext.SaveChangesAsync());
        }
        public async Task <BillItemDto> Handle(CreateBillItemRequest request, CancellationToken cancellationToken)
        {
            var bill = await _splitThatBillContext.Bills.FindAsync(request.BillId);

            if (null == bill)
            {
                throw new NullReferenceException("The bill to which the bill item belongs does not exist.");
            }

            var billItem = _mapper.Map <BillItem>(request.BillItemFormModel);

            bill.AddBillItem(billItem);
            _splitThatBillContext.Entry(bill).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            await _splitThatBillContext.SaveChangesAsync();

            var billItemDto = _mapper.Map <BillItemDto>(billItem);

            return(billItemDto);
        }
Ejemplo n.º 11
0
        public async Task <PaymentDetailDto> Handle(CreatePaymentDetailRequest request, CancellationToken cancellationToken)
        {
            var person = await _splitThatBillContext.People
                         .Include(i => i.PaymentDetails)
                         .FirstOrDefaultAsync(p => p.Id == request.PersonId);

            if (null == person)
            {
                throw new NullReferenceException("The person does not exist.");
            }

            var paymentDetail = PaymentDetail.Create(
                request.PersonPaymentDetailsFormModel.PaymentDetail.BankName,
                request.PersonPaymentDetailsFormModel.PaymentDetail.AccountNumber,
                request.PersonPaymentDetailsFormModel.PaymentDetail.AccountName
                );

            person.AddPaymentDetail(paymentDetail);

            await _splitThatBillContext.SaveChangesAsync();

            return(_mapper.Map <PaymentDetailDto>(paymentDetail));
        }