// [FromBody] means the data comes from the body of the post, not from url
        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 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);
            }
        }
Esempio n. 2
0
        // PUT: api/Products/5
        public IHttpActionResult Put(int id, [FromBody]Product product)
        {
            if (product == null)
            {
                return BadRequest("Product cannot be null");
            }

            var repo = new ProductRepository();
            var updatedProduct = repo.Save(id, product);
            if (updatedProduct == null)
            {
                return NotFound();
            }

            return Ok();
        }
Esempio n. 3
0
        // POST: api/Products
        public IHttpActionResult Post([FromBody]Product product)
        {
            if (product == null)
            {
                return BadRequest("Product cannot be null");
            }

            var repo = new ProductRepository();
            var newProduct = repo.Save(product);
            if (newProduct == null)
            {
                return Conflict();
            }

            return Created<Product>(Request.RequestUri + newProduct.ProductId.ToString(), newProduct);
        }
 // POST: api/Products
 public void Post([FromBody]Product product)
 {
     var productRepository = new ProductRepository();
     var newProduct = productRepository.Save(product);
 }
Esempio n. 5
0
 // PUT: api/Products/5
 public IHttpActionResult Put(int id, [FromBody]Product product)
 {
     if (product == null)
     {
         return BadRequest("Product cannot be null");
     }
     if (!ModelState.IsValid)
     {
         return BadRequest(ModelState);
     }
     var productRepository = new ProductRepository();
     var updatedProduct = productRepository.Save(id,product);
     if (updatedProduct == null)
     {
         return NotFound();
     }
     return Ok();
 }
 // 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 ProductRepository();
         var newProduct = productRepository.Save(id, product);
         if (newProduct == null)
         {
             return NotFound();
         }
         return Ok();
     }
     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 ProductRepository();
                var updatedProduct = productRepository.Save(id, product);
                if (updatedProduct == null)
                {
                    // The product repository could return null here too but we use the NotFound() 
                    // helper method instead because we assume the save wasn't successful because 
                    // the particular product was not found.
                    return NotFound();
                }

                return Ok();
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        public IHttpActionResult Post([FromBody]Product product)
        {
            try
            {
                if (product == null)
                {
                    // The BadRequest() helper method is used to setup the action result.
                    return BadRequest("Product cannot be null");
                }

                if (!ModelState.IsValid)
                {
                    // This is to test server-side model validation.
                    return BadRequest(ModelState);
                }

                var productRepository = new ProductRepository();
                var newProduct = productRepository.Save(product);
                if (newProduct == null)
                {
                    // The product repository could return a null product if the save wasn't 
                    // successful. 
                    return Conflict();
                }

                // If the new product was saved successfully, we send a created response. 
                // The created response is generic. In this case, we're creating a product. 
                // The first argument defines the location of the created resource. It's the
                // URL that defines the link back to the new resource. The second argument is 
                // the content of the response body and includes the new product. This is 
                // required because the new product can have new values defined by the service. 
                // In this case, the services assigned a new product id. So we're sending the data 
                // for the newly created product back in the response.  
                return Created<Product>(Request.RequestUri + newProduct.ProductId.ToString(),
                    newProduct);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
 // PUT: api/Products/5
 public void Put(int id, [FromBody]Product product)
 {
     var repo = new ProductRepository();
     var updateProduct = repo.Save(id, product);
 }
 // PUT: api/Products/5
 public IHttpActionResult Put(int id, [FromBody]Product product)
 {
     try
     {
         if (product == null)
         {
             return BadRequest("Product Can't be null");
         }
         var productRepository = new ProductRepository();
         var updatedProduct = productRepository.Save(id, product);
         if (updatedProduct == null)
         {
             return NotFound();
         }
         return Ok();
     }
     catch (Exception ex)
     {
         return InternalServerError(ex);
     }
 }