public ActionResult UpdateSupplier(UpdateSupplierModel model) { bool updatedSupplier; string errorMessage; Supplier formSupplierData = null; try { if (model.IsValid(out errorMessage)) { using (ProductListContext productContext = new ProductListContext()) { // update the supplier productContext.UpdateSupplier(model.SupplierID, model.Name, model.PhoneNumber); productContext.SaveChanges(); } updatedSupplier = true; } else { // save form data for when we return formSupplierData = new Supplier { SupplierID = model.SupplierID, Name = model.Name, PhoneNumber = model.PhoneNumber }; updatedSupplier = false; } } catch (Exception e) { updatedSupplier = false; errorMessage = Errors.GenericMVCInternalError; ErrorLog.LogError(e); } ActionResult result; if (updatedSupplier) { // Updated product, return to list ProductListModel viewModel = new ProductListModel { AddedOrUpdatedProduct = true }; result = RedirectToAction("Index", viewModel); } else { // Error, show message AddUpdateSupplierViewModel viewModel = AddUpdateSupplierViewModel.GetModel(formSupplierData, errorMessage); result = View("AddUpdateSupplier", viewModel); } return(result); }
public ViewResult AddUpdateProduct(AddUpdateProductModel model) { string errorMessage; ProductBase productToUpdate = null; try { if (model.IsValid(out errorMessage)) { // If we're updating a product if (model.ProductID.HasValue) { using (ProductListContext productContext = new ProductListContext()) { // find the product to update switch (model.ProductType) { case ProductType.Game: productToUpdate = productContext.GameProducts.FirstOrDefault(p => p.ProductID == model.ProductID); break; case ProductType.Car: productToUpdate = productContext.CarProducts.FirstOrDefault(p => p.ProductID == model.ProductID); break; } } } } } catch (Exception e) { errorMessage = Errors.GenericMVCInternalError; ErrorLog.LogError(e); } ViewResult viewResult; if (errorMessage != null) { // Error, show the message ProductListViewModel viewModel; using (ProductListContext productContext = new ProductListContext()) { viewModel = ProductListViewModel.GetModel(productContext, errorMessage: errorMessage); } viewResult = View("Index", viewModel); } else { // Load the Add/Update view AddUpdateProductViewModel viewModel = null; using (ProductListContext productContext = new ProductListContext()) { viewModel = AddUpdateProductViewModel.GetModel(productContext, productToUpdate, model.ProductType); } viewResult = View(viewModel); } return(viewResult); }
public ActionResult UpdateCarProduct(UpdateCarProdutModel model) { bool updatedProduct; string errorMessage; CarProduct formProductData = null; try { if (model.IsValid(out errorMessage)) { // round price model.Price = Math.Round(model.Price, 2); using (ProductListContext productContext = new ProductListContext()) { // update the product productContext.UpdateCarProduct(model.ProductID, model.Name, model.Price, model.Quantity, model.Year, model.Color, model.SupplierID); productContext.SaveChanges(); } updatedProduct = true; } else { // save form data for when we return formProductData = new CarProduct { ProductID = model.ProductID, Name = model.Name, Price = model.Price, Quantity = model.Quantity, Year = model.Year, Color = model.Color }; updatedProduct = false; } } catch (Exception e) { updatedProduct = false; errorMessage = Errors.GenericMVCInternalError; ErrorLog.LogError(e); } ActionResult result; if (updatedProduct) { // Updated product, return to list ProductListModel viewModel = new ProductListModel { AddedOrUpdatedProduct = true }; result = RedirectToAction("Index", viewModel); } else { // Error, show message AddUpdateProductViewModel viewModel; using (ProductListContext productContext = new ProductListContext()) { viewModel = AddUpdateProductViewModel.GetModel(productContext, formProductData, ProductType.Car, errorMessage); } result = View("AddUpdateProduct", viewModel); } return(result); }
public ProductRepository(ProductListContext context) : base(context) { }