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 ActionResult AddGameProduct(AddGameProductModel model)
        {
            bool        addedProduct;
            string      errorMessage;
            GameProduct formProductData = null;

            try
            {
                if (model.IsValid(out errorMessage))
                {
                    // round price
                    model.Price = Math.Round(model.Price, 2);

                    using (ProductListContext productContext = new ProductListContext())
                    {
                        // insert the product
                        productContext.AddGameProduct(model.Name,
                                                      model.Price,
                                                      model.Quantity,
                                                      model.GameType,
                                                      model.YearOfRelease,
                                                      model.RecommendedAge,
                                                      model.SupplierID);

                        productContext.SaveChanges();
                    }

                    addedProduct = true;
                }
                else
                {
                    // save form data for when we return
                    formProductData = new GameProduct
                    {
                        ProductID      = -1,
                        Name           = model.Name,
                        Price          = model.Price,
                        Quantity       = model.Quantity,
                        GameType       = model.GameType,
                        YearOfRelease  = model.YearOfRelease,
                        RecommendedAge = model.RecommendedAge,
                    };

                    addedProduct = false;
                }
            }
            catch (Exception e)
            {
                addedProduct = false;
                errorMessage = Errors.GenericMVCInternalError;
                ErrorLog.LogError(e);
            }

            ActionResult result;

            if (addedProduct)
            {
                // Added 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.Game, errorMessage);
                }

                result = View("AddUpdateProduct", 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);
        }