public async Task <IActionResult> Index() { GroceryCart model = null; if (User.Identity.IsAuthenticated) { var currentUser = _userManager.GetUserAsync(User).Result; model = await _context.GroceryCart.Include(x => x.GroceryCartProducts).ThenInclude(x => x.GroceryProduct).FirstOrDefaultAsync(x => x.ApplicationUserID == currentUser.Id); } else if (Request.Cookies.ContainsKey("cart_id")) { int existingCartID = int.Parse(Request.Cookies["cart_id"]); model = await _context.GroceryCart.Include(x => x.GroceryCartProducts).ThenInclude(x => x.GroceryProduct).FirstOrDefaultAsync(x => x.ID == existingCartID); //model = await _context.GroceryCart.Include(x => x.GroceryCartProducts).ThenInclude(x => x.GroceryProducts).FirstOrDefaultAsync(x => x.ID == existingCartID); } else { model = new GroceryCart(); } return(View(model)); }
public IActionResult Index(CheckoutModel model) { if (ModelState.IsValid) { GroceryOrder order = new GroceryOrder { City = model.City, State = model.State, Email = model.Email, StreetAddress = model.StreetAddress, AptSuite = model.AptSuite, ZipCode = model.ZipCode, DateCreated = DateTime.Now, DateLastModified = DateTime.Now, PaidDate = (DateTime?)null }; GroceryCart cart = null; if (User.Identity.IsAuthenticated) { var currentUser = _userManager.GetUserAsync(User).Result; cart = _context.GroceryCart.Include(x => x.GroceryCartProducts).ThenInclude(x => x.GroceryProduct).Single(x => x.ApplicationUserID == currentUser.Id); } else if (Request.Cookies.ContainsKey("cart_id")) { int existingCartID = int.Parse(Request.Cookies["cart_id"]); cart = _context.GroceryCart.Include(x => x.GroceryCartProducts).ThenInclude(x => x.GroceryProduct).FirstOrDefault(x => x.ID == existingCartID); } foreach (var cartItem in cart.GroceryCartProducts) { order.GroceryOrderProducts.Add(new GroceryOrderProduct { DateCreated = DateTime.Now, DateLastModified = DateTime.Now, Quantity = cartItem.Quantity ?? 1, ProductID = cartItem.GroceryProductID, ProductDescription = cartItem.GroceryProduct.Description, ProductName = cartItem.GroceryProduct.Name, ProductPrice = cartItem.GroceryProduct.Price }); } _context.GroceryCartProducts.RemoveRange(cart.GroceryCartProducts); if (Request.Cookies.ContainsKey("cart_id")) { Response.Cookies.Delete("cart_id"); } _context.GroceryOrders.Add(order); _context.SaveChanges(); return(RedirectToAction("Payment", new { id = order.ID })); } return(View()); }
public void CheckCart_ValidInput_HasItem() { // Arrange Mock <IPrices> pricesMock = new Mock <IPrices>(); GroceryCart cart = new GroceryCart(pricesMock.Object); cart.LoadItem(Item.Eggs, 4); var expected = new Dictionary <Item, double>(); expected.Add(Item.Eggs, 4); // Act var actual = cart.CheckCart(); // Assert actual.Should().BeEquivalentTo(expected); }
public void Evaluate_ValidInput_CorrectBalance() { // Arrange Mock <IPrices> pricesMock = new Mock <IPrices>(); pricesMock.Setup(p => p.CheckPrice(It.IsAny <Item>())).Returns(2); // This will make CheckPrice always return $2 ... Now we don't need to call the DB GroceryCart cart = new GroceryCart(pricesMock.Object); cart.LoadItem(Item.Bacon, 5); // Should be $10 cart.LoadItem(Item.Milk, 5); // Should be $10 cart.LoadItem(Item.Bread, 5); // Should be $10 cart.LoadItem(Item.Eggs, 5); // Should be $10, Total should be $40 var expected = 40; // Act var actual = cart.Evaluate(); // Assert actual.Should().Be(expected); }
public IActionResult CheckoutGroceries([FromBody] GroceryCart cart) { if (!ModelState.IsValid || cart.basket.Count == 0) { _logger.LogWarning(LoggingEvents.InsertItemBadRequest, $"Insert BAD REQUEST"); return(BadRequest()); } try { foreach (Grocery item in cart.basket) { _context.Database.ExecuteSqlInterpolated( $@"INSERT INTO GroceriesView (user, supermarket, name, date, count, weight, organic, seasonal, amount) VALUES ({item.user}, {item.supermarket}, {item.name}, {item.date}, {item.count}, {item.weight}, {item.organic}, {item.seasonal}, {item.amount}); "); } } catch (Exception ex) { _logger.LogWarning(LoggingEvents.InsertItemApplicationError, ex.Message); return(StatusCode(StatusCodes.Status500InternalServerError)); } return(NoContent()); }
public async Task <IActionResult> Details(int?id, int quantity, string category, string name, decimal price) { GroceryCart cart = null; if (User.Identity.IsAuthenticated) { var currentUser = await _userManager.GetUserAsync(User); cart = await _context.GroceryCart.Include(x => x.GroceryCartProducts).FirstOrDefaultAsync(x => x.ApplicationUserID == currentUser.Id); if (cart == null) { cart = new GroceryCart(); cart.ApplicationUserID = currentUser.Id; cart.DateCreated = DateTime.Now; cart.DateLastModified = DateTime.Now; _context.GroceryCart.Add(cart); } } else { if (Request.Cookies.ContainsKey("cart_id")) { int existingCartID = int.Parse(Request.Cookies["cart_id"]); cart = await _context.GroceryCart.Include(x => x.GroceryCartProducts).FirstOrDefaultAsync(x => x.ID == existingCartID); } if (cart == null) { cart = new GroceryCart { DateCreated = DateTime.Now, DateLastModified = DateTime.Now }; _context.GroceryCart.Add(cart); } } GroceryCartProducts product = cart.GroceryCartProducts.FirstOrDefault(x => x.GroceryProductID == id); if (product == null) { product = new GroceryCartProducts { DateCreated = DateTime.Now, DateLastModified = DateTime.Now, GroceryProductID = id ?? 0, Quantity = 0, Name = name, Price = price }; cart.GroceryCartProducts.Add(product); } product.Quantity += quantity; product.DateLastModified = DateTime.Now; product.Name = name; product.Price = price; await _context.SaveChangesAsync(); if (!User.Identity.IsAuthenticated) { Response.Cookies.Append("cart_id", cart.ID.ToString(), new Microsoft.AspNetCore.Http.CookieOptions { Expires = DateTime.Now.AddYears(1) }); } return(RedirectToAction("Index", "GroceryCart")); }
public decimal BillItems(GroceryCart cart) { foreach item in cart if(itemIsfresh) Bill it. }