public async Task <IHttpActionResult> UpdateItemAsync(Item item) { if (!ModelState.IsValid) { return(BadRequest("Not a valid model")); } using (var ctx = new thinkbridgeEntities()) { var existingItem = ctx.Items.Where(i => i.Id == item.Id) .FirstOrDefault <Item>(); if (existingItem != null) { existingItem.Price = item.Price; existingItem.Description = item.Description; ctx.SaveChanges(); } else { return(NotFound()); } } return(Ok()); }
public async Task <IHttpActionResult> DeleteItemAsync(string id) { if (Convert.ToInt32(id) <= 0) { return(BadRequest("Not a valid student id")); } using (var ctx = new thinkbridgeEntities()) { var item = ctx.Items .Where(i => i.Id == id) .FirstOrDefault(); ctx.Entry(item).State = System.Data.Entity.EntityState.Deleted; ctx.SaveChanges(); } return(Ok()); }
//Post an Item public async Task <IHttpActionResult> CreateItemAsync(Item item) { if (!ModelState.IsValid) { return(BadRequest("Invalid data.")); } using (var ctx = new thinkbridgeEntities()) { ctx.Items.Add(new Item() { Id = item.Id, Name = item.Name, Price = item.Price, Description = item.Description }); ctx.SaveChanges(); } return(Ok()); }