Example #1
0
        // POST: api/Products
        public IHttpActionResult Post([FromBody]Product product)
        {
            try
            {
                if (product == null)
                {
                    return BadRequest("Product cannot be null");
                }

                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
                var pr = new ProductRepository();
                var newProduct = pr.Save(product);
                if (newProduct == null)
                {
                    return Conflict();
                }
                return Created(Request.RequestUri + newProduct.ProductId.ToString(), newProduct);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
Example #2
0
 // 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 pr = new ProductRepository();
         var updateProduct = pr.Save(id, product);
         if (updateProduct == null)
         {
             return NotFound();
         }
         return Ok();
     }
     catch (Exception ex)
     {
         return InternalServerError(ex);
         throw;
     }
 }