public async Task <IActionResult> PutPayment([FromRoute] int id, [FromBody] Payment payment) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != payment.Id) { return(BadRequest()); } _context.Entry(payment).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PaymentExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutDeal([FromRoute] int id, [FromBody] Deal deal) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != deal.Id) { return(BadRequest()); } #region Deal Modify _context.Entry(deal).State = EntityState.Modified; #endregion #region Payments Modify if (deal.DealPayment.Method == Models.Enums.PaymentMethod.Cash && !deal.DealPayment.Paid) { deal.DealPayment.Amount = 0; } deal.DealPayment.PayerId = deal.BuyerId; deal.DealPayment.PayeeId = deal.SellerId; deal.DealPrice.PayerId = deal.SellerId; deal.DealPrice.PayeeId = deal.BuyerId; deal.DealPrice.PayDate = deal.DealTime; _context.Entry(deal.DealPayment).State = EntityState.Modified; _context.Entry(deal.DealPrice).State = EntityState.Modified; #endregion #region Deal Items Modify var newItems = deal.Items.ToList(); var deletingItems = _context.DealItem.Where(u => u.DealId == id && !newItems.Select(i => i.Id).Contains(u.Id)).ToList(); deletingItems.ForEach(i => { _context.Entry(i).State = EntityState.Deleted; }); newItems.ForEach(i => { if (i.Id != 0) { _context.Entry(i).State = EntityState.Modified; } else { _context.Entry(i).State = EntityState.Added; } }); #endregion try { _context.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { if (!DealExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }