public HttpResponseMessage UpdatePayment(int id, object Paymentobj) { JavaScriptSerializer js = new JavaScriptSerializer(); var json = Paymentobj; Payment PaymentUpdate = js.Deserialize <Payment>(json.ToString()); if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } //if (id != Payment.PaymentId) //{ // return Request.CreateResponse(HttpStatusCode.BadRequest); //} Payment Payment = Get(id); Payment.TransactionId = PaymentUpdate.TransactionId; Payment.Invoice = PaymentUpdate.Invoice; PaymentRepository.Attach(Payment); try { unitOfWork.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
public void UpdatePayment() { using (var uow = new CapriconContext()) { var paymentRep = new PaymentRepository(uow); var existingPayments = paymentRep.GetAll().ToList(); var existingPayment = existingPayments.Find(p => p.PaymentId == 3); Assert.IsNotNull(existingPayment); var messageRepository = new MessageRepository(uow); var existingMessage = messageRepository.GetAll().FirstOrDefault(); Assert.IsNotNull(existingMessage); existingPayment.PaymentDate = DateTime.Now; existingPayment.Amount = 350; existingPayment.Message = existingMessage; //check for validation rules //existingPayment.PaymentDate = DateTime.Now; paymentRep.Attach(existingPayment); uow.Entry(existingPayment).State = EntityState.Modified; try { uow.SaveChanges(); } catch (DbEntityValidationException ex) { //Retrieve validation errors ex.EntityValidationErrors.ToList().ForEach ( v => { v.ValidationErrors.ToList().ForEach ( e => { System.Diagnostics.Debug.WriteLine(e.ErrorMessage); } ); } ); Assert.Fail("Test failed"); } }; //retrieve saved object var uow1 = new CapriconContext(); var repository = new PaymentRepository(uow1); var savedPayments = repository.GetAll().ToList(); Assert.AreEqual(savedPayments[0].Amount, 350); }
public HttpResponseMessage UpatePaymentStatus(object Paymentobj1) { JavaScriptSerializer js = new JavaScriptSerializer(); var json = Paymentobj1; Payment Payment = js.Deserialize <Payment>(json.ToString()); Payment paymentobj = PaymentRepository.GetAll(t => t.Invoice == Payment.Invoice).FirstOrDefault(); paymentobj.TransactionId = Payment.TransactionId; paymentobj.Status = Payment.Status; paymentobj.Response = Payment.Response; if (ModelState.IsValid) { PaymentRepository.Attach(Payment); unitOfWork.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, Payment); response.Content = new StringContent(Payment.PaymentId.ToString()); return(response); } else { var errors = ModelState.Values.SelectMany(v => v.Errors); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } }