public ActionResult Create() { // display empty create page with dropdowns for suppliers and categories var productsViewModel = new ProductsViewModel(); productsViewModel.Suppliers = this._unit.Suppliers.GetAll().AsEnumerable<Supplier>(); productsViewModel.Categories = this._unit.Categories.GetAll().AsEnumerable<Category>(); return View(productsViewModel); }
public ActionResult Create(ProductsViewModel productViewModel) { //get selected category var selectedCategory = this._unit.Categories.GetById(productViewModel.SelectedCategoryValue); //get selected supplier var selectedSupplier = this._unit.Suppliers.GetById(productViewModel.SelectedSupplierValue); //populate suppliers and categories in viewmodel in case validation fails productViewModel.Suppliers = this._unit.Suppliers.GetAll().AsEnumerable<Supplier>(); productViewModel.Categories = this._unit.Categories.GetAll().AsEnumerable<Category>(); //create a new product object that contains what the user entered Product newProduct = new Product { ProductName = productViewModel.Name, Supplier = selectedSupplier, Category = selectedCategory, UnitPrice = productViewModel.UnitPrice, UnitsInStock = productViewModel.UnitsInStock, UnitsOnOrder = productViewModel.UnitsOnOrder }; try { if (ModelState.IsValid) { this._unit.Products.Add(newProduct); this._unit.SaveChanges(); TempData["CreateSuccess"] = true; return RedirectToAction("Index"); } } catch (DataException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ViewBag.DeleteError = true; ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return View(productViewModel); }
public void POST_Edit_should_save_updated_record() { // Arrange Supplier selectedSupplier = new Supplier { SupplierID = 99, CompanyName = "Supplier 99" }; Category selectedCategory = new Category { CategoryID = 99, CategoryName = "Category 99" }; Product dbproduct = new Product { ProductID = 1, ProductName = "Product 1", UnitPrice = 10.00M, UnitsInStock = 1, UnitsOnOrder = 1, CategoryID = 1, SupplierID = 1, Supplier = new Supplier { SupplierID = 1, CompanyName = "Supplier 1" }, Category = new Category { CategoryID = 1, CategoryName = "Category 1" } }; ProductsViewModel productViewModel = new ProductsViewModel { ID = 1, Name = "Product Romeo", UnitPrice = 20.00M, UnitsInStock = 3, UnitsOnOrder = 3, SelectedCategoryValue = 99, SelectedSupplierValue = 99 }; mock.Setup(m => m.Suppliers.GetById(It.IsAny<int>())).Returns(selectedSupplier); mock.Setup(m => m.Categories.GetById(It.IsAny<int>())).Returns(selectedCategory); mock.Setup(m => m.Products.GetById(It.IsAny<int>())).Returns(dbproduct); mock.Setup(m => m.Suppliers.GetAll()).Returns(suppliers.AsQueryable<Supplier>()); mock.Setup(m => m.Categories.GetAll()).Returns(categories.AsQueryable<Category>()); mock.Setup(m => m.Products.Update(It.IsAny<Product>())); mock.Setup(m => m.SaveChanges()); ProductController controller = new ProductController(mock.Object); //act var result = controller.Edit(productViewModel) as ViewResult; var viewModel = controller.ViewData.Model as ProductsViewModel; // Assert // Check that each method was only called once. mock.Verify(x => x.Products.Update(It.IsAny<Product>()), Times.Once()); mock.Verify(x => x.SaveChanges(), Times.Once()); Assert.AreEqual(dbproduct.ProductID, viewModel.ID); Assert.AreEqual(dbproduct.ProductName, viewModel.Name); Assert.AreEqual(dbproduct.UnitPrice, viewModel.UnitPrice); Assert.AreEqual(dbproduct.UnitsInStock, viewModel.UnitsInStock); Assert.AreEqual(dbproduct.UnitsOnOrder, viewModel.UnitsOnOrder); Assert.AreEqual(dbproduct.Supplier, selectedSupplier); Assert.AreEqual(dbproduct.Category, selectedCategory); Assert.AreEqual("", result.ViewName); }
public void POST_Create_should_save_the_record_and_redirect_to_index() { // Arrange Supplier selectedSupplier = new Supplier { SupplierID = 99, CompanyName = "Supplier 99" }; Category selectedCategory = new Category { CategoryID = 99, CategoryName = "Category 99" }; ProductsViewModel productViewModel = new ProductsViewModel { Name = "Product Romeo", UnitPrice = 20.00M, UnitsInStock = 3, UnitsOnOrder = 3, SelectedCategoryValue = 99, SelectedSupplierValue = 99 }; mock.Setup(m => m.Suppliers.GetById(It.IsAny<int>())).Returns(selectedSupplier); mock.Setup(m => m.Categories.GetById(It.IsAny<int>())).Returns(selectedCategory); mock.Setup(m => m.Suppliers.GetAll()).Returns(suppliers.AsQueryable<Supplier>()); mock.Setup(m => m.Categories.GetAll()).Returns(categories.AsQueryable<Category>()); mock.Setup(m => m.Products.Add(It.IsAny<Product>())); mock.Setup(m => m.SaveChanges()); ProductController controller = new ProductController(mock.Object); // Act var result = controller.Create(productViewModel) as RedirectToRouteResult; var viewModel = controller.ViewData.Model as ProductsViewModel; // Assert // Check that each method was only called once. mock.Verify(x => x.Products.Add(It.IsAny<Product>()), Times.Once()); mock.Verify(x => x.SaveChanges(), Times.Once()); //check that redirec to index happened Assert.AreEqual("Index", result.RouteValues["action"]); }
public ActionResult Edit(ProductsViewModel productViewModel) { //get selected category var selectedCategory = this._unit.Categories.GetById(productViewModel.SelectedCategoryValue); //get selected supplier var selectedSupplier = this._unit.Suppliers.GetById(productViewModel.SelectedSupplierValue); //populate suppliers and categories in viewmodel in case validation fails productViewModel.Suppliers = this._unit.Suppliers.GetAll().AsEnumerable<Supplier>(); productViewModel.Categories = this._unit.Categories.GetAll().AsEnumerable<Category>(); //get product from database using the ID Product product = this._unit.Products.GetById(productViewModel.ID); //update values in product product.ProductName = productViewModel.Name; product.Supplier = selectedSupplier; product.Category = selectedCategory; product.UnitPrice = productViewModel.UnitPrice; product.UnitsInStock = productViewModel.UnitsInStock; product.UnitsOnOrder = productViewModel.UnitsOnOrder; try { if (ModelState.IsValid) { this._unit.Products.Update(product); this._unit.SaveChanges(); ViewBag.EditSuccess = true; } } catch (DataException /* dex */) { //Log the error (uncomment dex variable name after DataException and add a line here to write a log. ViewBag.EditError = true; ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return View(productViewModel); }
public ActionResult Edit(int id) { //edit a particular product Product product = this._unit.Products.GetById(id); if (product == null) { return new HttpStatusCodeResult(HttpStatusCode.NotFound); } var productsViewModel = new ProductsViewModel(); productsViewModel.ID = product.ProductID; productsViewModel.Name = product.ProductName; productsViewModel.UnitPrice = product.UnitPrice; productsViewModel.UnitsInStock = product.UnitsInStock; productsViewModel.UnitsOnOrder = product.UnitsOnOrder; productsViewModel.SelectedCategoryValue = product.CategoryID; productsViewModel.SelectedSupplierValue = product.SupplierID; productsViewModel.Suppliers = this._unit.Suppliers.GetAll().AsEnumerable<Supplier>(); productsViewModel.Categories = this._unit.Categories.GetAll().AsEnumerable<Category>(); return View(productsViewModel); }