コード例 #1
0
        // GET: api/Products/5
        public IHttpActionResult Get(int id)
        {
            try
            {
                Product product;
                var productRepository = new ProductRepository();

                if (id > 0)
                {
                    // gets all products and then searches - not to be used in a live database
                    var products = productRepository.Retrieve();
                    product = products.FirstOrDefault(p => p.ProductId == id);
                    if (product == null)
                    {
                        return NotFound();
                    }
                }
                else
                {
                    product = productRepository.Create();
                }
                return Ok(product);
            }
            catch(Exception ex)
            {
                return InternalServerError(ex);
            }
        }
コード例 #2
0
 public IEnumerable<Product> Get(string search)
 {
     var productRepository = new ProductRepository();
     var products = productRepository.Retrieve();
     var result = products.Where(p => p.ProductCode.Contains(search));
     return result;
 }
コード例 #3
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 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);
     }
 }
コード例 #4
0
        // GET: api/Products/5
        public IHttpActionResult Get(int id)
        {
            try
            {
                Product product;
                var productRepository = new ProductRepository();

                if (id > 0)
                {
                    var products = productRepository.Retrieve();
                    product = products.FirstOrDefault(p => p.ProductId == id);
                    if (product == null)
                    {
                        return NotFound();
                    }
                }
                else
                {
                    product = productRepository.Create();
                }
                return Ok(product);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
コード例 #5
0
 public IHttpActionResult Get()
 {
     try
     {
         var productRepository = new ProductRepository();
         return Ok(productRepository.Retrieve().AsQueryable());
     }
     catch (Exception ex)
     {
         return InternalServerError(ex);
     }
 }
コード例 #6
0
 //public IEnumerable<Product> Get(string search)
 //{
 //    var productRepository = new ProductRepository();
 //    var products = productRepository.Retrieve();
 //    return products.Where(p => p.ProductCode.Contains(search));
 //}
 // GET: api/Products/5
 public Product Get(int id)
 {
     Product product;
     var productRepository = new ProductRepository();
     if (id > 0)
     {
         var products = productRepository.Retrieve();
         product = products.FirstOrDefault(p => p.ProductId == id);
     }
     else
     {
         product = productRepository.Create();
     }
     return product;
 }
コード例 #7
0
        //Search: api/Products/5
        public IHttpActionResult Get(string productToFind)
        {
            try
            {
                var productRepository = new ProductRepository();

                if (productToFind == "undefined" || productToFind == "null")
                    return NotFound();
                else
                    return Ok(productRepository.Retrieve().Where(p => p.ProductCode.Contains(productToFind)));
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
コード例 #8
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);
        }
コード例 #9
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();
        }
コード例 #10
0
 public IHttpActionResult Get(int id)
 {
     return HandleExceptions(() =>
     {
         Product product;
         var productRepository = new ProductRepository();
         if (id > 0)
         {
             var products = productRepository.Retrieve();
             product = products.FirstOrDefault(p => p.ProductId == id);
             if (product == null)
             {
                 return NotFound();
             }
         }
         else
         {
             product = productRepository.Create();
         }
         return Ok(product);
     });
 }
コード例 #11
0
        // GET: api/Products/5
        public IHttpActionResult Get(int id)
        {
            Product product;
            var productRepo = new ProductRepository();

            if (id > 0)
            {
                var products = productRepo.Retrieve();  // this is not effecient, you should query the db directly
                product = products.FirstOrDefault(p => p.ProductId == id);

                if (product == null)
                {
                    return NotFound();
                }
            }
            else
            {
                product = productRepo.Create();
            }

            return Ok(product);
        }
コード例 #12
0
 public IHttpActionResult Get()
 {
     var productRepository = new ProductRepository();
     return Ok(productRepository.Retrieve().AsQueryable());
 }
コード例 #13
0
 // GET: api/Products
 public IEnumerable<Product> Get()
 {
     var repo = new ProductRepository();
     return repo.Retrieve();
 }
コード例 #14
0
 // PUT: api/Products/5
 public void Put(int id, [FromBody]Product product)
 {
     var repo = new ProductRepository();
     var updateProduct = repo.Save(id, product);
 }
コード例 #15
0
        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);
            }
        }
コード例 #16
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 productRepository = new ProductRepository();
         var newProduct = productRepository.Save(id, product);
         if (newProduct == null)
         {
             return NotFound();
         }
         return Ok();
     }
     catch(Exception ex)
     {
         return InternalServerError(ex);
     }
 }
コード例 #17
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 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);
            }
        }
コード例 #18
0
 // DELETE: api/Products/5
 public void Delete(int id)
 {
     var productRepository = new ProductRepository();
     productRepository.Delete(id);
 }
コード例 #19
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();
 }
コード例 #20
0
 public IEnumerable<Product> Get()
 {
     var productRepository = new ProductRepository();
     return productRepository.Retrieve();
 }
コード例 #21
0
 public IQueryable<Product> Get()
 {
     var productRepository = new ProductRepository();
     return productRepository.Retrieve().AsQueryable();
 }
コード例 #22
0
 // POST: api/Products
 public void Post([FromBody]Product product)
 {
     var productRepository = new ProductRepository();
     var newProduct = productRepository.Save(product);
 }
コード例 #23
0
 // 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);
     }
 }