// 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); } }
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; }
// 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); } }
// 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); } }
public IHttpActionResult Get() { try { var productRepository = new ProductRepository(); return Ok(productRepository.Retrieve().AsQueryable()); } catch (Exception ex) { return InternalServerError(ex); } }
//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; }
//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); } }
// 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); }
// 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(); }
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); }); }
// 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); }
public IHttpActionResult Get() { var productRepository = new ProductRepository(); return Ok(productRepository.Retrieve().AsQueryable()); }
// GET: api/Products public IEnumerable<Product> Get() { var repo = new ProductRepository(); return repo.Retrieve(); }
// PUT: api/Products/5 public void Put(int id, [FromBody]Product product) { var repo = new ProductRepository(); var updateProduct = repo.Save(id, product); }
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 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); } }
// DELETE: api/Products/5 public void Delete(int id) { var productRepository = new ProductRepository(); productRepository.Delete(id); }
// 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(); }
public IEnumerable<Product> Get() { var productRepository = new ProductRepository(); return productRepository.Retrieve(); }
public IQueryable<Product> Get() { var productRepository = new ProductRepository(); return productRepository.Retrieve().AsQueryable(); }
// POST: api/Products public void Post([FromBody]Product product) { var productRepository = new ProductRepository(); var newProduct = productRepository.Save(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); } }