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 <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); }
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); }
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 <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); }