public ActionResult Edit(Product edited) { using (var context = new MyShopReadModelDataContext()) { Product original = context.Products.FirstOrDefault(p => p.Id == edited.Id); if (original == null) { return View("NotFound"); } else { var commands = new List<ICommand>(GetCommandsForEditedProduct(original, edited)); if (commands.Count > 0) { MyShopWebApplication.CommandService.Execute(commands.ToArray()); } return View(edited); } } }
public ActionResult AddProduct(Product p) { Guid shoppingCartId; var visitorId = new Guid(Request.Cookies["MyShopVisitorIdentifier"].Value); var shoppingCart = new ShoppingCartRepository().FindByVisitorId(visitorId); if (shoppingCart == null) { shoppingCartId = Guid.NewGuid(); var command = new CreateShoppingCartForVisitor(shoppingCartId, visitorId); MyShopWebApplication.CommandService.Execute(command); } else { shoppingCartId = shoppingCart.Id; } var addProductCommand = new AddProductToShoppingCart(shoppingCartId, p.Id, 1); MyShopWebApplication.CommandService.Execute(addProductCommand); return RedirectToAction("Index"); }
protected IEnumerable<ICommand> GetCommandsForEditedProduct(Product original, Product edited) { if (original.Name != edited.Name || original.Description != edited.Description) { yield return new UpdateGeneralProductInformation(original.Id, edited.Name, edited.Description); } if (original.UnitPrice != edited.UnitPrice) { yield return new UpdateUnitPriceOfProduct(original.Id, edited.UnitPrice); } if (original.UnitsInStock != edited.UnitsInStock) { yield return new UpdateUnitsInStockOfProduct(original.Id, edited.UnitsInStock); } if (Request.Files.AllKeys.Contains("productImageFile")) { HttpPostedFileBase filePost = Request.Files["productImageFile"]; if (String.IsNullOrEmpty(filePost.FileName) && filePost.ContentLength > 0) { yield return new ChangeProductImage(edited.Id, filePost.FileName, filePost.InputStream); } } yield break; }
partial void DeleteProduct(Product instance);
partial void UpdateProduct(Product instance);
partial void InsertProduct(Product instance);