public ProductsOfOrderDto(ProductsOfOrder productsOfOrder)
 {
     this.OrderID     = productsOfOrder.OrderID;
     this.ProductID   = productsOfOrder.ProductID;
     this.Amount      = productsOfOrder.Amount;
     this.UnitPrice   = productsOfOrder.Product.Price;
     this.SellerID    = productsOfOrder.Product.SellerID;
     this.SellerName  = productsOfOrder.Product.Seller.Name;
     this.ProductName = productsOfOrder.Product.Name;
 }
        public List <OrderDto> CreateOrdersAndDeleteCart(int customerID, string deliveryAddress)
        {
            //If a cart of customer has products of different sellers --> create many orders

            var carts = this._unitOfWork.Carts.Find(c => c.CustomerID == customerID);

            if (carts.Count() == 0)
            {
                throw new Exception("Empty Cart !!!");
            }

            var orders       = new List <Order>();
            var timeNow      = DateTime.Now;
            var timeDelivery = timeNow.AddDays(7);

            foreach (var cart in carts)
            {
                if (cart.Amount == 0)
                {
                    throw new Exception("Amount of product is 0");
                }

                this._unitOfWork.Products.Load(p => p.ID == cart.ProductID);
                this._unitOfWork.Sellers.Load(s => s.ID == cart.Product.SellerID);

                var productsOfOrder = new ProductsOfOrder();

                if (cart.Amount > cart.Product.Quantity)
                {
                    throw new Exception("The product is out of stock");
                }

                productsOfOrder.ProductID = cart.ProductID;
                productsOfOrder.Amount    = cart.Amount;
                cart.Product.Quantity    -= cart.Amount;

                if (IsExistSellerID(orders, cart.Product))
                {
                    foreach (var order in orders)
                    {
                        if (order.SellerID == cart.Product.SellerID)
                        {
                            order.ProductsOfOrders.Add(productsOfOrder);
                            break;
                        }
                    }
                }
                else
                {
                    var order = new Order();
                    order.CustomerID      = customerID;
                    order.DeliveryAddress = deliveryAddress;
                    order.OrderTime       = timeNow;
                    order.DeliveryDate    = timeDelivery;
                    order.ShippingCost    = Shipping.ShippingCost;
                    order.SellerID        = cart.Product.SellerID;
                    order.ProductsOfOrders.Add(productsOfOrder);

                    orders.Add(order);
                }
            }

            this._unitOfWork.Orders.AddRange(orders);
            this._unitOfWork.Carts.RemoveRange(carts);
            this._unitOfWork.Complete();

            var orderDtos = new List <OrderDto>();

            foreach (var order in orders)
            {
                orderDtos.Add(new OrderDto(order));
            }

            return(orderDtos);
        }