public void AddToBooked(int productId, int quantity)
        {
            ProductStock stock = Stocks.FirstOrDefault(s => s.ProductIdent == productId);

            if (stock != null && stock.InStock >= quantity)
            {
                stock.Booked += quantity;
            }
            context.SaveChanges();
        }
        public void AddToStock(int productId, int quantity)
        {
            ProductStock stock = Stocks.FirstOrDefault(s => s.ProductIdent == productId);

            if (stock != null)
            {
                stock.InStock += quantity;
            }
            else
            {
                stock = new ProductStock {
                    ProductIdent = productId, InStock = quantity, Booked = 0
                };
                context.ProductStocks.Add(stock);
            }
            context.SaveChanges();
        }
コード例 #3
0
        public bool CheckQuantity(IEnumerable <CartLine> cartLines)
        {
            int enought = 0;
            int entire  = cartLines.Count();

            foreach (var line in cartLines)
            {
                ProductStock ps        = context.ProductStocks.FirstOrDefault(t => t.ProductIdent == line.Product.ProductID);
                int          booked    = ps != null ? ps.Booked : 0;
                int          available = (ps != null ? ps.InStock : 0) - booked;
                if (line.Quantity <= available)
                {
                    enought++;
                }
            }
            return(enought == entire ? true : false);
        }