public IHttpActionResult PostProduct(Product product) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Products.Add(product); try { db.SaveChanges(); } catch (DbUpdateException) { if (ProductExists(product.Id)) { return Conflict(); } else { throw; } } return CreatedAtRoute("DefaultApi", new { id = product.Id }, product); }
public HttpResponseMessage PostProduct(Product product) { Repository.SaveProduct(product); var response = Request.CreateResponse<Product>(System.Net.HttpStatusCode.Created, product); return response; }
public IEnumerable<Product> GetProductsByCategory(string category) { Product[] products = new Product[] { new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } }; return products; }
public ActionResult Create(Product collection) { try { // TODO: Add insert logic here return RedirectToAction("Index"); } catch { return View(); } }
public ProductsRepository() { var ctx = HttpContext.Current; if (ctx != null) { if (ctx.Cache[CacheKey] == null) { var products = new Product[] { new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } }; ctx.Cache[CacheKey] = products; } } }
public bool SaveProduct(Product product) { var ctx = HttpContext.Current; if (ctx != null) { try { var currentData = ((Product[])ctx.Cache[CacheKey]).ToList(); currentData.Add(product); ctx.Cache[CacheKey] = currentData.ToArray(); return true; } catch (Exception ex) { Console.WriteLine(ex.ToString()); return false; } } return false; }
public IHttpActionResult PutProduct(int id, Product product) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != product.Id) { return BadRequest(); } db.Entry(product).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public string PutProduct(int id, Product product) { return "put " + product.Name; }
// public HttpResponseMessage PostProduct(Product item) public string PostProduct(Product product) { return "post " + product.Name; }
public async Task<IHttpActionResult> UpdateProduct(Product product) { if (product == null) return BadRequest(); foreach(var p in products) { if(p.Id == product.Id) { p.Category = product.Category; p.Name = product.Name; p.Price = product.Price; return Ok(p); } } return NotFound(); }
public async Task<IHttpActionResult> AddProduct(Product product) { products.Add(product); return Ok(); }