public void EmptyCart(string email)
        {
            var cToDelete = _cartsRepo.GetCarts().Where(x => x.Email == email);

            if (cToDelete != null)
            {
                _cartsRepo.EmptyCart(cToDelete);
            }
        }
Esempio n. 2
0
        public async Task <List <Cart> > Search(string text)
        {
            text = text.ToLower();
            var searchedCarts = await _cartRepo.GetCarts(cart => cart.Id.ToLower().Contains(text));

            return(searchedCarts);
        }
        public void CheckOut(string email)
        {
            // get a list of products that have been added to the cart for
            // the given email (from db)
            var carts = _cartsRepo.GetCarts().Where(x => x.Email == email);

            // loop within the list of products to check qty from the stock
            // if you find a product with qty > stock - throw new Exeption("Not enough stock") OR!!!!
            // if you find a product with qty > stock - feturn false
            foreach (var cart in carts)
            {
                var product = _productsRepo.GetProduct(cart.Product.Id);

                if (cart.Qty > product.Stock)
                {
                    throw new Exception("Out Of Stock!");
                }
            }

            // 3. create an order
            Guid  orderId = Guid.NewGuid();
            Order o       = new Order();

            o.Id         = orderId;
            o.DatePlaced = DateTime.Now;
            o.UserEmail  = email;

            // Call the AddOrder from inside the IOrdersRepository (3)
            _ordersRepo.AddOrder(o);


            // 4. loop with the list of products and create an OrderDetail for each of the products
            // start loop
            List <OrderDetail> details = new List <OrderDetail>();

            foreach (var cart in carts)
            {
                var product = _productsRepo.GetProduct(cart.Product.Id);

                OrderDetail detail = new OrderDetail();

                detail.OrderFK   = orderId;
                detail.ProductFK = cart.Product.Id;
                detail.Quantity  = cart.Qty;
                detail.Price     = Math.Round(cart.Product.Price * cart.Qty, 2);

                details.Add(detail);

                // deduct qty from stock
                product.Stock -= cart.Qty;
                // end loop
            }
            _ordersRepo.AddOrderDetails(details);
            _cartsRepo.EmptyCart(carts);
        }
        public IQueryable <CartViewModel> GetCart(CartViewModel data, string email)
        {
            var list = from c in _cartsRepo.GetCarts()
                       select new CartViewModel()
            {
                Id       = c.Id,
                email    = c.email,
                quantity = c.quantity,
                Product  = new ProductViewModel()
                {
                    Id = c.Product.Id, Name = c.Product.Name, ImageUrl = c.Product.ImageUrl
                }
            };

            return(list);
        }
 public IEnumerable <Cart> GetCarts()
 {
     return(_context.GetCarts());
 }
Esempio n. 6
0
 public IQueryable <CartViewModel> GetCartForUser(string email)
 {
     return(_cartsRepo.GetCarts().Where(p => p.Email.Equals(email))
            .ProjectTo <CartViewModel>(_autoMapper.ConfigurationProvider));
 }