public IHttpActionResult Add(ProductBindingModel model) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } if (model == null) { return this.BadRequest("Invalid input parameters."); } var product = new Product(); product.Name = model.Name; product.ImageUrl = model.ImageUrl; product.Description = model.Description; product.CategoryId = model.CategoryId; product.TradeId = model.TradeId; product.Price = model.Price; product.Quantity = model.Quantity; try { this.Data.Products.Add(product); this.Data.SaveChanges(); } catch (Exception ex) { return this.GetExceptionMessage(ex); } return this.Ok(string.Format("Product name: {0} and id: {1} is created", product.Name, product.Id)); }
public IHttpActionResult Edit(int id, ProductBindingModel model) { if (!this.ModelState.IsValid) { return this.BadRequest(ModelState); } if (model == null) { return this.BadRequest("Invalid input parameters."); } var product = this.Data.Products.All() .Where(x => x.Id == id) .FirstOrDefault(); this.CheckObjectForNull(product, "product", id); product.Name = model.Name; product.ImageUrl = model.ImageUrl; product.Description = model.Description; product.CategoryId = model.CategoryId; product.TradeId = model.TradeId; product.Price = model.Price; product.Quantity = model.Quantity; try { this.Data.Products.Update(product); this.Data.SaveChanges(); } catch (Exception ex) { return this.GetExceptionMessage(ex); } return Ok(string.Format("Product with id {0} is changed successfully", id)); }