public CartDetails GetCartDetails(int cartId)
    {
        var cartDetails = new CartDetails();

        cartDetails.CartItems = cartDAL.GetCart(cartId);
        cartDetails.Tax       = cartDAL.GetCart(cartId);
        cartDetails.Total     = cartDAL.GetCart(cartId);
        return(cartDetails);
    }
示例#2
0
        public async Task <IActionResult> BuyAsync(int?id)
        {
            if (!id.HasValue)
            {
                return(NotFound("You must pass a product ID in the route, for example, /cart/buy/21"));
            }

            List <CartItem> cart  = CartDAL.GetCart(HttpContext.Session);
            int             index = isExist(id.Value);

            if (index != -1)
            {
                cart[index].Quantity++;
            }
            else
            {
                CartProduct cartProduct = await MapNorthwindToCartAsync(id.Value);

                cart.Add(new CartItem {
                    Product = cartProduct, Quantity = 1
                });
            }
            CartDAL.SetCart(HttpContext.Session, cart);

            return(RedirectToAction("Index"));
        }
示例#3
0
        public IActionResult Index()
        {
            var cart = CartDAL.GetCart(HttpContext.Session);

            ViewBag.cart  = cart;
            ViewBag.total = cart.Sum(item => item.Product.Price * item.Quantity);
            return(View());
        }
示例#4
0
        public IActionResult Remove(int id)
        {
            List <CartItem> cart  = CartDAL.GetCart(HttpContext.Session);
            int             index = isExist(id);

            cart.RemoveAt(index);
            CartDAL.SetCart(HttpContext.Session, cart);
            return(RedirectToAction("Index"));
        }
示例#5
0
        public IActionResult Receipt(CartCheckoutViewModel cartCheckout)
        {
            //int employeeID = cartCheckout.EmployeeID;
            //int shipperID = cartCheckout.ShipperID;
            //string customerID = cartCheckout.CustomerID;

            //create order
            DateTime orderDate = DateTime.Now;
            Order    order     = new Order();
            var      cart      = CartDAL.GetCart(HttpContext.Session);

            order.CustomerID   = cartCheckout.CustomerID;
            order.EmployeeID   = cartCheckout.EmployeeID;
            order.Freight      = 10 + (cart.Count * 1);
            order.OrderDate    = orderDate;
            order.RequiredDate = DateTime.Now.AddDays(7);
            //order.ShippedDate = DateTime.Now.AddDays(5);// filled out in Admin section
            order.ShipVia = cartCheckout.ShipperID;

            if (cartCheckout.CustomerID != null)
            {
                Customer customer = _NorthwindDAL.GetCustomer(cartCheckout.CustomerID);
                order.ShipName       = customer.ContactName; // cartCheckout.Name;
                order.ShipAddress    = customer.Address;     // cartCheckout.Address;
                order.ShipCity       = customer.City;        // cartCheckout.City;
                order.ShipRegion     = customer.Region;      // cartCheckout.State;
                order.ShipPostalCode = customer.PostalCode;  // cartCheckout.PostalCode;
                order.ShipCountry    = customer.Country;
            }

            int orderID = _NorthwindDAL.AddOrder(order).Value;

            for (int i = 0; i < cart.Count; i++)
            {
                OrderDetail orderDetail = new OrderDetail();
//                orderDetail.Discount = 0;
                orderDetail.OrderID   = orderID;
                orderDetail.ProductID = cart[i].Product.Id;
                orderDetail.Quantity  = cart[i].Quantity;
                orderDetail.UnitPrice = cart[i].Product.Price;
                _NorthwindDAL.AddOrderDetail(orderDetail);
            }

            CartReceiptViewModel receipt = new CartReceiptViewModel
            {
                OrderID   = orderID,
                OrderDate = orderDate
            };

            //clear cart
            CartDAL.SetCart(HttpContext.Session, null);

            return(View(receipt));
        }
示例#6
0
        private int isExist(int id)
        {
            List <CartItem> cart = CartDAL.GetCart(HttpContext.Session);

            for (int i = 0; i < cart.Count; i++)
            {
                if (cart[i].Product.Id.Equals(id))
                {
                    return(i);
                }
            }
            return(-1);
        }