public IHttpActionResult Update(int id, ProductModel product) { if (!this.ModelState.IsValid) { return this.BadRequest("Invalid data"); } var isFarmer = this.User.IsInRole("Farmer"); if (!isFarmer) { return this.BadRequest("You are not farmer!"); } var existingProduct = this.data .Products .All() .Where(p => p.Id == id && p.Deleted == false) .FirstOrDefault(); if (existingProduct == null) { return this.BadRequest("Such product does not exists!"); } existingProduct.Name = product.Name; existingProduct.Price = product.Price; this.data.SaveChanges(); product.Id = id; var newProduct = new { Id = product.Id, Name = product.Name, Price = product.Price , }; return this.Ok(newProduct); }
public IHttpActionResult Add(ProductModel product) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var farm = this.data.Farms.All().FirstOrDefault(f => f.Account == this.User.Identity.Name); var existingProduct = this.data.Products.All().FirstOrDefault(p => p.Name == product.Name && p.Farm.Id == farm.Id); if (existingProduct != null) { return this.BadRequest("You had already added this product!"); } var newProduct = new Product() { Name = product.Name, Price = product.Price, Farm = farm, FarmId = farm.Id }; this.data.Products.Add(newProduct); this.data.SaveChanges(); return this.Ok(newProduct.Id); }