public RedirectToRouteResult RemoveFromCart(Cart cart, int cofeeId, int volumeOptionId, bool isSugarOption, bool isMilkOption = false) { Cofee cofee = repository.Products .FirstOrDefault(p => p.CofeeId == cofeeId); VolumeOption volumeOption = cofee?.VolumeOptions .FirstOrDefault(vo => vo.VolumeOptionId == volumeOptionId); if ((cofee != null) && (volumeOption != null)) { int?sugarOptionId = isSugarOption ? cofee.SugarOptions.FirstOrDefault()?.SugarOptionId : null; int? milkoptionId = isMilkOption ? cofee.MilkOptions.FirstOrDefault()?.MilkOptionId : null; CofeeOptions cofeeOptions = new CofeeOptions { MilkOptionId = milkoptionId, SugarOptionId = sugarOptionId }; Recipe recipe = new Recipe { CofeeId = cofee.CofeeId, VolumeSize = volumeOption.Size, IsCupCap = volumeOption.IsCupCap, Options = cofeeOptions }; cart.RemoveLine(recipe); } return(RedirectToAction("Index")); }
public ViewResult Edit(int cofeeId) { Cofee cofee = repository.Products .FirstOrDefault(p => p.CofeeId == cofeeId); return(View(cofee)); }
public Cofee DeleteProduct(int cofeeId) { Cofee dbEntry = context.CofeeRecords.Find(cofeeId); if (dbEntry != null) { context.CofeeRecords.Remove(dbEntry); context.SaveChanges(); } return(dbEntry); }
public ActionResult Delete(int cofeeId) { Cofee deletedProduct = repository.DeleteProduct(cofeeId); if (deletedProduct != null) { TempData["message"] = string.Format("{0} was deleted", deletedProduct.Name); } return(RedirectToAction("Index")); }
public void Can_Delete_Valid_Products() { // Arrange - create a Product Cofee cofee = new Cofee { CofeeId = 2, Name = "Test" }; // Arrange - create the mock repository // Arrange - create the mock repository Mock <ICofeeRepository> mock = new Mock <ICofeeRepository>(); var volumeOptions = new VolumeOption[] { new VolumeOption { VolumeOptionId = 1, Size = 0.133M, Unit = new Unit { Name = "Litre" } } }; var sugarOptions = new SugarOption[] { new SugarOption { SugarOptionId = 1, Size = 1, Price = 0, Unit = new Unit { Name = "Teaspoon" } } }; var espresso = new Cofee { CofeeId = 1, Name = "Espresso", PriceCoeff = 2.0M, VolumeOptions = volumeOptions, SugarOptions = sugarOptions }; var americano = new Cofee { CofeeId = 2, Name = "Americano", PriceCoeff = 3.0M, VolumeOptions = volumeOptions, SugarOptions = sugarOptions }; mock.Setup(m => m.Products).Returns(new Cofee[] { espresso, americano }.AsQueryable()); // Arrange - create the controller AdminController target = new AdminController(mock.Object); // Act - delete the product target.Delete(cofee.CofeeId); // Assert - ensure that the repository delete method was // called with the correct Product mock.Verify(m => m.DeleteProduct(cofee.CofeeId)); }
public ActionResult Edit(Cofee cofee) { if (ModelState.IsValid) { repository.SaveProduct(cofee); TempData["message"] = string.Format("{0} has been saved", cofee.Name); return(RedirectToAction("Index")); } else { // there is something wrong with the data values return(View(cofee)); } }
public void Can_Edit_Product() { // Arrange - create the mock repository Mock <ICofeeRepository> mock = new Mock <ICofeeRepository>(); var volumeOptions = new VolumeOption[] { new VolumeOption { VolumeOptionId = 1, Size = 0.133M, Unit = new Unit { Name = "Litre" } } }; var sugarOptions = new SugarOption[] { new SugarOption { SugarOptionId = 1, Size = 1, Price = 0, Unit = new Unit { Name = "Teaspoon" } } }; var espresso = new Cofee { CofeeId = 1, Name = "Espresso", PriceCoeff = 2.0M, VolumeOptions = volumeOptions, SugarOptions = sugarOptions }; var americano = new Cofee { CofeeId = 2, Name = "Americano", PriceCoeff = 3.0M, VolumeOptions = volumeOptions, SugarOptions = sugarOptions }; mock.Setup(m => m.Products).Returns(new Cofee[] { espresso, americano }.AsQueryable()); // Arrange - create the controller AdminController target = new AdminController(mock.Object); // Act Cofee c1 = target.Edit(1).ViewData.Model as Cofee; Cofee c2 = target.Edit(2).ViewData.Model as Cofee; // Assert Assert.AreEqual(1, c1.CofeeId); Assert.AreEqual(2, c2.CofeeId); }
public RedirectToRouteResult AddToCart(Cart cart, int quantity, int cofeeId, int volumeOptionId, bool isSugarOption, bool isMilkOption = false) { Cofee cofee = repository.Products .FirstOrDefault(c => c.CofeeId == cofeeId); VolumeOption volumeOption = cofee?.VolumeOptions .FirstOrDefault(vo => vo.VolumeOptionId == volumeOptionId); if ((cofee != null) && (volumeOption != null)) { Recipe recipe = new Recipe(); recipe.CofeeId = cofee.CofeeId; recipe.CofeeName = cofee.Name; recipe.CofeePriceCoef = cofee.PriceCoeff; recipe.VolumeOptionId = volumeOption.VolumeOptionId; recipe.VolumeSize = volumeOption.Size; recipe.IsCupCap = volumeOption.IsCupCap; recipe.Options = new CofeeOptions(); if (isSugarOption) { SugarOption sugarOption = cofee.SugarOptions.FirstOrDefault(); if (sugarOption != null) { recipe.Options.SugarOptionId = sugarOption.SugarOptionId; recipe.Options.SugarPrice = sugarOption.Price; recipe.Options.SugarSize = sugarOption.Size; recipe.Options.SugarUnitName = sugarOption.Unit.Name; } } if (isMilkOption) { MilkOption milkOption = cofee.MilkOptions.FirstOrDefault(); if (milkOption != null) { recipe.Options.MilkOptionId = milkOption.MilkOptionId; recipe.Options.MilkPrice = milkOption.Price; recipe.Options.MilkSize = milkOption.Size; recipe.Options.MilkUnitName = milkOption.Unit.Name; } } cart.AddItem(recipe, quantity); } return(RedirectToAction("Index")); }
public void Cannot_Edit_Nonexistent_Product() { // Arrange - create the mock repository Mock <ICofeeRepository> mock = new Mock <ICofeeRepository>(); var volumeOptions = new VolumeOption[] { new VolumeOption { VolumeOptionId = 1, Size = 0.133M, Unit = new Unit { Name = "Litre" } } }; var sugarOptions = new SugarOption[] { new SugarOption { SugarOptionId = 1, Size = 1, Price = 0, Unit = new Unit { Name = "Teaspoon" } } }; var espresso = new Cofee { CofeeId = 1, Name = "Espresso", PriceCoeff = 2.0M, VolumeOptions = volumeOptions, SugarOptions = sugarOptions }; var americano = new Cofee { CofeeId = 2, Name = "Americano", PriceCoeff = 3.0M, VolumeOptions = volumeOptions, SugarOptions = sugarOptions }; mock.Setup(m => m.Products).Returns(new Cofee[] { espresso, americano }.AsQueryable()); // Arrange - create the controller AdminController target = new AdminController(mock.Object); // Act Cofee result = (Cofee)target.Edit(4).ViewData.Model; // Assert Assert.IsNull(result); }
public void Can_Save_Valid_Changes() { // Arrange - create mock repository Mock <ICofeeRepository> mock = new Mock <ICofeeRepository>(); // Arrange - create the controller AdminController target = new AdminController(mock.Object); // Arrange - create a product Cofee cofee = new Cofee { Name = "Test" }; // Act - try to save the product ActionResult result = target.Edit(cofee); // Assert - check that the repository was called mock.Verify(m => m.SaveProduct(cofee)); // Assert - check the method result type Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }
public void SaveProduct(Cofee product) { if (product.CofeeId == 0) { context.CofeeRecords.Add(product); } else { Cofee dbEntry = context.CofeeRecords.Find(product.CofeeId); if (dbEntry != null) { dbEntry.Name = product.Name; dbEntry.Description = product.Description; dbEntry.PriceCoeff = product.PriceCoeff; dbEntry.ImagePath = product.ImagePath; } } context.SaveChanges(); }
public override async Task <ActionResult <IEnumerable <New> > > GetTopNews([FromQuery] int quantity, [FromQuery] string subject) { if (quantity > 0) { var result = new Result(); Website cofee = new Cofee(); var news = await cofee.GetTopNews(quantity, subject); if (news != null) { foreach (var n in news) { result.Total++; var existed = _context.News.FirstOrDefault(e => e.Title.ToLower() == n.Title); if (existed != null) { continue; } try { var post = PostCreateModel.Create(n); if (post == null) { continue; } Wordpress.CreateAsync(post); _context.News.Add(n); result.Published++; } catch (System.Exception e) { continue; } } await _context.SaveChangesAsync(); return(Ok(result)); } } return(BadRequest(nameof(quantity))); }
public void Cannot_Save_Invalid_Changes() { // Arrange - create mock repository Mock <ICofeeRepository> mock = new Mock <ICofeeRepository>(); // Arrange - create the controller AdminController target = new AdminController(mock.Object); // Arrange - create a product Cofee cofee = new Cofee { Name = "Test" }; // Arrange - add an error to the model state target.ModelState.AddModelError("error", "error"); // Act - try to save the product ActionResult result = target.Edit(cofee); // Assert - check that the repository was not called mock.Verify(m => m.SaveProduct(It.IsAny <Cofee>()), Times.Never()); // Assert - check the method result type Assert.IsInstanceOfType(result, typeof(ViewResult)); }
static void Main(string[] args) { Cofee sample = new Cofee(Cofee.CofeeSize.Normal); Console.WriteLine($"The \"Sample\" is a {sample.Size} ({(int)sample.Size}ml) cofee."); Order order = new Order(); order.Add(new Cofee(Cofee.CofeeSize.Small), 0.5); order.Add(new Cofee(Cofee.CofeeSize.Normal), 1.5); order.Add(new Cofee(Cofee.CofeeSize.Double), 2.75); order.Add(new Cofee(Cofee.CofeeSize.Small)); order.Add(new Cofee(Cofee.CofeeSize.Normal)); Console.WriteLine("\nThe order contains: "); order.PrintOrderItems(); Console.WriteLine($"The order has {order.OrderItems.Count} cofees and costs a total of {order.CalculateCost()} euros (inc VAT)."); Console.ReadKey(); }
public void Add(Cofee cofee) { double price; switch (cofee.Size) { case Cofee.CofeeSize.Small: price = 0.5; break; case Cofee.CofeeSize.Normal: price = 1.5; break; case Cofee.CofeeSize.Double: price = 2.5; break; default: price = 2.5; break; } OrderItems.Add(Tuple.Create(cofee, price)); }
public void Add(Cofee cofee, double price) { OrderItems.Add(Tuple.Create(cofee, price)); }
public void ProcessSeed(LittleCofeeShopDbContext context) { var notDefUnit = new Unit { Name = "Not defined", ShortName = "N/A" }; Unit litreUnit = new Unit { Name = "Litre", ShortName = "L" }; Unit teaspoonUnit = new Unit { Name = "Teaspoon", ShortName = "t" }; Unit pieceUnit = new Unit { Name = "Piece", ShortName = "pcs" }; context.Units.AddOrUpdate(notDefUnit); context.Units.AddOrUpdate(litreUnit); context.Units.AddOrUpdate(teaspoonUnit); context.Units.AddOrUpdate(pieceUnit); //context.Units.AddRange(units); IList <VolumeOption> defaultVolumeOptions = new List <VolumeOption>(); defaultVolumeOptions.Add(new VolumeOption { Unit = litreUnit, Size = 0.133M, Name = "Small", IsCupCap = true }); defaultVolumeOptions.Add(new VolumeOption { Unit = litreUnit, Size = 0.250M, Name = "Middle", IsCupCap = true }); IList <VolumeOption> latteVolumeOptions = new List <VolumeOption>(defaultVolumeOptions); latteVolumeOptions.Add(new VolumeOption { Unit = litreUnit, Size = 0.500M, Name = "Large", IsCupCap = true }); context.VolumeOptionRecords.AddRange(latteVolumeOptions); IList <MilkOption> milkOptions = new List <MilkOption>(); milkOptions.Add(new MilkOption { Unit = notDefUnit, Price = 0.1M, Size = 1.0M }); context.MilkOptionRecords.AddRange(milkOptions); SugarOption sugarOption = new SugarOption { Unit = teaspoonUnit, Price = 0.1M, Size = 1.0M }; context.SugarOptionRecords.AddOrUpdate(sugarOption); IList <SugarOption> sugarOptions = new List <SugarOption>(); sugarOptions.Add(sugarOption); Cofee espressoCofee = new Cofee { Name = "Espresso", Description = @"Espresso is coffee of Italian origin, brewed by expressing or forcing a small amount of nearly boiling water under pressure through finely ground coffee beans.Espresso is generally thicker than coffee brewed by other methods, has a higher concentration of suspended and dissolved solids, and has crema on top(a foam with a creamy consistency).", VolumeOptions = new List <VolumeOption>(defaultVolumeOptions), SugarOptions = new List <SugarOption>(sugarOptions), PriceCoeff = 3.0M, ImagePath = "~/Static/Espresso.jpg" }; context.CofeeRecords.AddOrUpdate(espressoCofee); Cofee latteCofee = new Cofee { Name = "Latte", Description = @"A latte is a coffee drink made with espresso and steamed milk. A cafe latte consists of 2 fluid ounces of espresso, 3 ounces of steamed milk, and typically a thin layer of foam on top.It can sometimes be referred to as a “Wet Cappuccino”.", MilkOptions = new List <MilkOption>(milkOptions), VolumeOptions = new List <VolumeOption>(latteVolumeOptions), SugarOptions = new List <SugarOption>(sugarOptions), PriceCoeff = 4.0M, ImagePath = "~/Static/Latte.jpg" }; context.CofeeRecords.AddOrUpdate(latteCofee); Cofee americanoCofee = new Cofee { Name = "Americano", Description = @"Caffè Americano, or Americano (Italian: American coffee) is a style of coffee prepared by adding hot water to espresso, giving a similar strength but different flavor from regular drip coffee. The strength of an Americano varies with the number of shots of espresso and the amount of water added.", VolumeOptions = new List <VolumeOption>(defaultVolumeOptions), SugarOptions = new List <SugarOption>(sugarOptions), PriceCoeff = 3.0M, ImagePath = "~/Static/Americano.jpg" }; context.CofeeRecords.AddOrUpdate(americanoCofee); base.Seed(context); }
public Cofee Create(Cofee cofee) { _dbContext.Cofees.Add(cofee); _dbContext.SaveChanges(); return(cofee); }
// GET: MakeOrder /* public ActionResult MakeOrder(int cofeeId) * { * return View(); * } */ public PartialViewResult MakeOrder(Cofee cofee) { return(PartialView(cofee)); }