public void InsertOrUpdate(Payment payment) { if (payment.Id == default(int)) { // New entity context.Payments.Add(payment); } else { // Existing entity context.Entry(payment).State = EntityState.Modified; } }
public IHttpActionResult ShareBalance(ShareToBindingModel Share) { if (!ModelState.IsValid) { return BadRequest(ModelState); } string UserId = User.Identity.GetUserId(); var ToUser = UserManager.FindByEmail(Share.Email); if (ToUser == null) { ModelState.AddModelError("Share.Email", "User not found."); return BadRequest(ModelState); } int balance = _payments.GetUserBalance(UserId); if (balance - Share.Amount < 0) { ModelState.AddModelError("Share.Amount", "Insufficient balance."); return BadRequest(ModelState); } Payment payment_to = new Payment() { UserID = ToUser.Id, Sum = Share.Amount, Date = DateTime.UtcNow }; Payment payment_from = new Payment() { UserID = UserId, Sum = -1 * Share.Amount, Date = DateTime.UtcNow }; _payments.InsertOrUpdate(payment_to); _payments.InsertOrUpdate(payment_from); _payments.Save(); return CreatedAtRoute("ShareBalance", new { id = payment_from.Id }, payment_from); }
public IHttpActionResult PostPayment(Payment payment) { if (payment == null) { return BadRequest(ModelState); } payment.Id = 0; payment.Date = DateTime.UtcNow; return PutPayment(0, payment); }
public IHttpActionResult PutPayment(int id, Payment payment) { if (payment == null) { return BadRequest(); } if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != payment.Id) { return BadRequest(); } _payments.InsertOrUpdate(payment); _payments.Save(); return CreatedAtRoute("DefaultApi", new { id = payment.Id }, payment); }