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());
        }
コード例 #2
0
        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));
        }