public Product AddProduct(Product item) { _db.Products.Add(item); _db.SaveChanges(); return item; }
/// <summary> /// Insert new product /// </summary> /// <param name="product">Product object</param> /// <returns></returns> public IHttpActionResult PostProduct(Product product) { if (!ModelState.IsValid) return BadRequest(ModelState); var addedProduct = _prodService.AddProduct(product); if (addedProduct == null) return BadRequest(); return CreatedAtRoute("DefaultApi", new { id = product.Id }, product); }
public Product UpdateProduct(Product item) { bool productExists = item.Id > 0 && _db.Products.Any(c => c.Id == item.Id); if (productExists) { _db.Entry(item).State = System.Data.Entity.EntityState.Modified; _db.SaveChanges(); return item; } return null; }
public Product AddOrUpdateProduct(Product item) { bool itemExists = item.Id > 0 && _db.Products.Any(p => p.Id == item.Id); if (itemExists) { _db.Entry(item).State = System.Data.Entity.EntityState.Modified; } else { _db.Products.Add(item); } _db.SaveChanges(); return item; }
/// <summary> /// Update existing product /// </summary> /// <param name="id">Product id</param> /// <param name="product">Product object</param> /// <returns></returns> public IHttpActionResult PutProduct(int id, Product product) { if (!ModelState.IsValid) return BadRequest(ModelState); if (id != product.Id) return BadRequest(); try { var updatedProduct = _prodService.UpdateProduct(product); if (updatedProduct == null) return NotFound(); } catch (Exception) { throw; } return StatusCode(HttpStatusCode.NoContent); }