public IHttpActionResult PutCustomer(int id, Customer customer) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != customer.CustomerId) { return(BadRequest()); } db.Entry(customer).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public IHttpActionResult PutItem(int id, Item item) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != item.ItemId) { return(BadRequest()); } db.Entry(item).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ItemExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public IHttpActionResult PostOrder(Order order) { try { //Order table if (order.OrderId == 0) { db.Order.Add(order); } else { //db.Order.Attach(order); db.Entry(order).State = EntityState.Modified; } //OrderItems table foreach (var item in order.OrderItems) { if (item.OrderItemId == 0) { db.OrderItems.Add(item); } else { db.Entry(item).State = EntityState.Modified; } } //Delete for OrderItems if (order.DeletedOrderItemIds != null) { foreach (var id in order.DeletedOrderItemIds.Split(',').Where(x => x != "")) { OrderItems x = db.OrderItems.Find(Convert.ToInt64(id)); db.OrderItems.Remove(x); } } db.SaveChanges(); return(Ok()); } catch (Exception ex) { throw ex; } }