public void Edit(ProductChangeModel model) { _logger.LogInformation($"{MethodBase.GetCurrentMethod().Name} started"); var product = _repositories.Products.Get(model.ProductId); if (product == null) { throw new LogicException("There is no product with that Id"); } if (model.SupplierId != null) { var supplier = _repositories.Suppliers.Get((int)model.SupplierId); if (supplier == null) { throw new LogicException("There is no supplier with that Id"); } } if (model.CategoryId != null) { var category = _repositories.Categories.Get((int)model.CategoryId); if (category == null) { throw new LogicException("There is no category with that Id"); } } product.SupplierId = model.SupplierId; product.CategoryId = model.CategoryId; product.ProductName = model.ProductName == null ? product.ProductName : model.ProductName; product.QuantityPerUnit = model.QuantityPerUnit == null ? product.QuantityPerUnit : model.QuantityPerUnit; product.UnitPrice = model.UnitPrice == null ? product.UnitPrice : model.UnitPrice; product.UnitsInStock = model.UnitsInStock == null ? product.UnitsInStock : model.UnitsInStock; _repositories.Products.Update(product); _repositories.SaveChanges(); _logger.LogInformation($"{MethodBase.GetCurrentMethod().Name} finished"); }
public IActionResult Edit([FromQuery] ProductChangeModel model) { if (ModelState.IsValid) { _productOperations.Edit(model); } else { return(BadRequest()); } return(Ok()); }