Пример #1
0
        public IHttpActionResult Get(int id)
        {
            try
            {
                // throw new Exception("Invalid operation occured");
                Product product;
                var productRepository = new ProductRepository();
                if (id > 0)
                {
                    var res = productRepository.Retrieve();
                    product = res.Find(x => x.ProductId.Equals(id));
                    if (product == null) return NotFound();
                }
                else
                {
                    product = productRepository.Create();
                }
                return Ok(product);
            }
            catch (Exception ex)
            {

                return InternalServerError(ex);
            }
        }
Пример #2
0
 public IHttpActionResult Get()
 {
     try
     {
         var productRepository = new ProductRepository();
         return Ok(productRepository.Retrieve().AsQueryable());
     }
     catch (Exception ex)
     {
         return InternalServerError(ex);
     }
 }
Пример #3
0
        // POST api/product
        public IHttpActionResult Post([FromBody]Product product)
        {
            try
            {
                if (product == null)
                    return BadRequest("Product can not be null");

                if (!ModelState.IsValid) return BadRequest(ModelState);

                var productReppstory = new ProductRepository();

                var newProduct = productReppstory.Save(product);

                if (newProduct == null)
                    return Conflict();

                return Created(Request.RequestUri + newProduct.ProductId.ToString(), newProduct);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
Пример #4
0
        // PUT api/product/5
        public IHttpActionResult Put(int id, [FromBody]Product product)
        {
            try
            {
                if (product == null) return BadRequest("Product can not be null");

                if (!ModelState.IsValid) return BadRequest(ModelState);

                var productReppstory = new ProductRepository();
                var updatedProduct = productReppstory.Save(id, product);
                if (updatedProduct == null) NotFound();

                return Ok("Product Updated");
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }