public IHttpActionResult Post([FromBody]Product product) { try { if (product == null) { return BadRequest("Product cannot be null"); } if (!ModelState.IsValid) { return BadRequest(ModelState); } var productRepository = new Models.ProductRepository(); var newProduct = productRepository.Save(product); if (newProduct == null) { return Conflict(); } return Created<Product>(Request.RequestUri + newProduct.ProductId.ToString(), newProduct); } catch (Exception ex) { return InternalServerError(ex); } }
// PUT: api/Products/5 public IHttpActionResult Put(int id, [FromBody]Product product) { try { if (product == null) { return BadRequest("Product cannot be null"); } if (!ModelState.IsValid) { return BadRequest(ModelState); } var productRepository = new Models.ProductRepository(); var updatedProduct = productRepository.Save(id, product); if (updatedProduct == null) { return NotFound(); } return Ok(); } catch (Exception ex) { return InternalServerError(ex); } }