public ActionResult Index(NewProductInputModel newProduct) { if (ModelState.IsValid) { var workingOnMemory = (bool)(Session?["WorkingOnMemory"] ?? true); var productsDAO = _productsDAOFactory.Create(workingOnMemory); var result = ProductRegister.Register(productsDAO, newProduct); if (result is SuccessfulProductRegistrationResult) { return(RedirectToAction("Index", "Home")); } if (result is FailedProductRegistrationResult failedResult) { ViewBag.Message = failedResult.Reason; } } return(View("", newProduct)); }
public static IProductRegistrationResult Register(IProductStoringDAO productsDAO, NewProductInputModel newProduct) { if (productsDAO.ThereIsAlreadySomeProductWith(newProduct.Number)) { return(new FailedProductRegistrationResult("There is already another product that is using the same number.")); } if (productsDAO.ThereIsAlreadySomeProductWith(newProduct.Title)) { return(new FailedProductRegistrationResult("There is already another product that is using the same title.")); } var product = new Product(newProduct.Number, Text.Create(newProduct.Title), newProduct.Price, Text.Create(newProduct.Description)); StoredProduct storedProduct; try { storedProduct = productsDAO.Store(product); } catch (Exception) { return(new FailedProductRegistrationResult("There was a problem, please try again.")); // Of course here is the opportunity to log the specific exception } return(new SuccessfulProductRegistrationResult(storedProduct)); }