public async Task <IActionResult> Index() { viewModel = new OrderShoppingCartsViewModel() { OrderInfo = new Models.OrderInfo(), ShoppingCarts = new List <Models.ShoppingCart>() }; var claimsIdentity = (ClaimsIdentity)this.User.Identity; var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier); var shoppingCarts = _db.ShoppingCarts.Where(s => s.ApplicationUserId == claim.Value); if (shoppingCarts != null) { viewModel.ShoppingCarts = shoppingCarts.ToList(); } foreach (var shoppingCart in viewModel.ShoppingCarts) { shoppingCart.MenuItem = await _db.MenuItems.FirstOrDefaultAsync(m => m.Id == shoppingCart.MenuItemId); viewModel.OrderInfo.OrderSubTotal += shoppingCart.MenuItem.Price * shoppingCart.Count; shoppingCart.MenuItem.Description = Utility.StaticMethods.ConvertToRawHtml(shoppingCart.MenuItem.Description); if (shoppingCart.MenuItem.Description.Length > 100) { var first100CharsDescription = shoppingCart.MenuItem.Description.Substring(0, 99); var lastSpaceIndex = first100CharsDescription.LastIndexOf(' '); shoppingCart.MenuItem.Description = first100CharsDescription.Substring(0, lastSpaceIndex + 1) + "..."; } } viewModel.OrderInfo.OrderTotal = viewModel.OrderInfo.OrderSubTotal; if (HttpContext.Session.GetString(Utility.Constants.sessionCouponCode) != null) { viewModel.OrderInfo.CouponCode = HttpContext.Session.GetString(Utility.Constants.sessionCouponCode); var coupon = await _db.Coupons.FirstOrDefaultAsync(c => c.Name.ToLower() == viewModel.OrderInfo.CouponCode.ToLower()); viewModel.OrderInfo.OrderTotal = Utility.StaticMethods.DiscountedPrice(coupon, viewModel.OrderInfo.OrderSubTotal); } return(View(viewModel)); }
public async Task <IActionResult> Summary() { viewModel = new OrderShoppingCartsViewModel() { OrderInfo = new Models.OrderInfo(), ShoppingCarts = new List <Models.ShoppingCart>() }; var claimsIdentity = (ClaimsIdentity)this.User.Identity; var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier); ApplicationUser applicationUser = await _db.ApplicationUsers.FirstOrDefaultAsync(u => u.Id == claim.Value); var shoppingCarts = _db.ShoppingCarts.Where(s => s.ApplicationUserId == claim.Value); if (shoppingCarts != null) { viewModel.ShoppingCarts = shoppingCarts.ToList(); } foreach (var shoppingCart in viewModel.ShoppingCarts) { shoppingCart.MenuItem = await _db.MenuItems.FirstOrDefaultAsync(m => m.Id == shoppingCart.MenuItemId); viewModel.OrderInfo.OrderSubTotal += shoppingCart.MenuItem.Price * shoppingCart.Count; } viewModel.OrderInfo.OrderTotal = viewModel.OrderInfo.OrderSubTotal; viewModel.OrderInfo.PickUpName = applicationUser.Name; viewModel.OrderInfo.PhoneNumber = applicationUser.PhoneNumber; viewModel.OrderInfo.PickUpTime = DateTime.Now; if (HttpContext.Session.GetString(Utility.Constants.sessionCouponCode) != null) { viewModel.OrderInfo.CouponCode = HttpContext.Session.GetString(Utility.Constants.sessionCouponCode); var coupon = await _db.Coupons.FirstOrDefaultAsync(c => c.Name.ToLower() == viewModel.OrderInfo.CouponCode.ToLower()); viewModel.OrderInfo.OrderTotal = Utility.StaticMethods.DiscountedPrice(coupon, viewModel.OrderInfo.OrderSubTotal); } return(View(viewModel)); }