示例#1
0
        void IOrderHelper.AddOrder(OrderModel model)
        {
            var adress = model.Client.Name + " " + model.Client.Surname
                         + ";" + model.Client.Street + ";" + model.Client.PostalCode
                         + ";" + model.Client.City;

            var order = new orders
            {
                Id_Client        = model.Client.Id,
                Id_Delivery      = model.selectedDeliveryId,
                Id_Status        = 1,
                ToPay            = model.cart.ComputeTotalValue(),
                AdressToDelivery = adress,
                InsertTime       = DateTime.Now,
            };

            //dodanie nowego zamówienia => przejscie do wypełnienia szczegółów zamówienia
            var orderId = _ordersRepository.AddOrder(order);

            var orderDetails = new List <order_details>();
            var arrayToList  = model.cart.Lines.ToList();

            for (int i = 0; i < arrayToList.Count - 1; i++)
            {
                orderDetails.Add(new order_details
                {
                    Id_Order = orderId,
                    Id_Item  = arrayToList[i].Item.Id,
                    Quantity = arrayToList[i].Quantity
                });
            }

            _ordersRepository.AddOrderDetails(orderDetails);
        }
        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);
        }