// [Route("api/payment/{id}")] public IHttpActionResult Post([FromBody] PaymentDTO value) { try { if (value == null) { return(BadRequest()); } string userName = null; if (HttpContext.Current != null && HttpContext.Current.User != null && HttpContext.Current.User.Identity.Name != null) { userName = HttpContext.Current.User.Identity.Name; } if (value.Amount != value.PaymentItems.Sum(it => it.Amount)) { return(BadRequest("Invocie Amount Miss Match with total Invoice Item Amount")); } foreach (var item in value.PaymentItems) { var invoice = _InvoiceRepo.Get().FirstOrDefault <Invoice>(i => i.InvoiceNo == item.InvoiceNo); if (invoice == null) { return(BadRequest(item.InvoiceNo + "InvoiceNo not found")); } item.InvoiceID = invoice.ID; } var payment = value.ToDomain(); payment.CreateUser = userName; payment.ChangeUser = userName; payment.Concurrency = Guid.NewGuid(); _PaymentRepo.Add(payment); _uow.SaveChanges(); if (payment.ID > 0) { return(Created <PaymentDTO>(Request.RequestUri + "/" + payment.ID, payment.ToDTO())); } return(BadRequest()); } catch (Exception ex) { return(InternalServerError()); } }
public IHttpActionResult Put(int id, [FromBody] PaymentDTO value) { if (value == null) { return(BadRequest()); } string userName = null; if (HttpContext.Current != null && HttpContext.Current.User != null && HttpContext.Current.User.Identity.Name != null) { userName = HttpContext.Current.User.Identity.Name; } if (value.Amount != value.PaymentItems.Sum(it => it.Amount)) { return(BadRequest("Invocie Amount Miss Match with total Invoice Item Amount")); } var originalPayment = _PaymentRepo.Get().Include(p => p.PaymentItems).FirstOrDefault <Payment>(c => c.ID == id); if (originalPayment == null) { return(NotFound()); } foreach (var item in value.PaymentItems) { var invoice = _InvoiceRepo.Get().FirstOrDefault <Invoice>(i => i.InvoiceNo == item.InvoiceNo); if (invoice == null) { return(BadRequest(item.InvoiceNo + "InvoiceNo not found")); } item.InvoiceID = invoice.ID; } var payment = value.ToDomain(originalPayment); payment.ChangeUser = userName; payment.Concurrency = Guid.NewGuid(); _PaymentRepo.Update(payment); try { _uow.SaveChanges(); return(Created <PaymentDTO>(Request.RequestUri + "/" + payment.ID, payment.ToDTO())); } catch (DbEntityValidationException ex) { //StringBuilder sb = new StringBuilder(); //foreach (var failure in ex.EntityValidationErrors) //{ // sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType()); // foreach (var error in failure.ValidationErrors) // { // sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage); // sb.AppendLine(); // } //} // throw new DbEntityValidationException( // "Entity Validation Failed - errors follow:\n" + // sb.ToString(), ex //); // Add the original exception as the innerException return(InternalServerError()); } catch (Exception ex) { return(InternalServerError()); } }