Exemplo n.º 1
0
 public static void Initialize(CapContext context)
 {
     if (!context.Caps.Any())
     {
         context.Caps.AddRange(
             new Cap
         {
             Name       = "Cap2323",
             SupplierId = 1,
             Price      = 600
         },
             new Cap
         {
             Name       = "Cap2t5",
             SupplierId = 1,
             Price      = 899
         },
             new Cap
         {
             Name       = "Cap23",
             SupplierId = 1,
             Price      = 678
         }
             );
         context.SaveChanges();
     }
 }
Exemplo n.º 2
0
        public IActionResult Checkout()
        {
            var items = _cart.GetCartCaps();

            _cart.CartCaps = items;
            if (_cart.CartCaps.Count == 0)
            {
                ModelState.AddModelError("", "Your cart is empty, add some caps first");
                ViewBag.CheckoutMessage = "Your cart is empty, add some caps first";
            }

            if (ModelState.IsValid)
            {
                var     order      = new Order();
                var     customerId = _userManager.GetUserId(User);
                decimal totalCost  = 0;

                order.OrderPlaced = DateTime.Now;
                order.CustomerId  = customerId;
                order.Status      = "Active";
                order.TotalCost   = _cart.GetCartTotal();

                _context.Orders.Add(order);

                var cartCaps = _cart.CartCaps;

                foreach (var cartCap in cartCaps)
                {
                    var orderCap = new OrderCap()
                    {
                        Quantity = cartCap.Amount,
                        Cap      = cartCap.Cap,
                        OrderId  = order.Id,
                        Price    = cartCap.Cap.Price
                    };

                    totalCost += orderCap.Price * orderCap.Quantity;
                    _context.OrderCaps.Add(orderCap);
                }

                _context.SaveChanges();
                _cart.ClearCart();
                return(RedirectToAction("CheckoutComplete"));
            }

            return(View());
        }