Beispiel #1
0
        public async Task <IActionResult> Checkout()
        {
            string userEmail = User.Identity.Name;
            var    userRaw   = await _userManager.FindByEmailAsync(userEmail);

            string userID = userRaw.Id;

            Basket basket = await _basketcontext.GetBasketByUserId(userID);

            List <BasketProductViewModel> basketProducts = await _basketProduct.GetBasket(basket.ID);

            Checkout checkout = new Checkout();

            checkout.UserID    = userEmail;
            checkout.Name      = User.Claims.First(name => name.Type == "FullName").Value;
            checkout.OrderDate = DateTime.Now;
            await _context.CreateCheckoutAsync(checkout);

            foreach (var bp in basketProducts)
            {
                await _checkoutProduct.AddCheckoutProduct(bp.ProductID, bp.Quantity, checkout.ID);

                bp.BasketID     = checkout.ID;
                checkout.Total += Convert.ToDecimal(bp.Quantity) * bp.Price;
                await _context.UpdateCheckoutAsync(checkout);
            }

            CheckoutViewModel checkoutVM = new CheckoutViewModel()
            {
                ID        = checkout.ID,
                Name      = checkout.Name,
                Email     = userEmail,
                Address   = userRaw.Address,
                City      = userRaw.City,
                State     = userRaw.State,
                ZipCode   = userRaw.ZipCode,
                OrderDate = checkout.OrderDate,
                Total     = checkout.Total,
            };

            return(View(checkoutVM));
        }