Пример #1
0
        public bool PlaceOrder(Order order)
        {
            var products = _cookiesDbContext.Products.Join(order.OrderDetails.Select(od => od.ProductId),
                                                           dbP => dbP.ProductId, op => op,
                                                           (dbP, op) => new
            {
                ProductId = dbP.ProductId,
                Price     = dbP.Price
            }).ToList();


            var newDbOrder = new DbContext.Order
            {
                CustomerId   = order.CustomerId,
                DateOfOrder  = DateTime.UtcNow,
                OrderDetails = order.OrderDetails.Select(od => new OrderDetail
                {
                    ProductId = od.ProductId,
                    Quantity  = od.Quantity,
                    Cost      = products.Where(p => p.ProductId.Equals(od.ProductId)).Sum(x => x.Price.Value)
                }).ToList()
            };

            newDbOrder.TotalCost = newDbOrder.OrderDetails.Sum(x => x.Cost);

            _cookiesDbContext.Orders.Add(newDbOrder);

            return(_cookiesDbContext.SaveChanges() > 0);
        }
Пример #2
0
        public bool UpdateProduct(Product product)
        {
            try
            {
                var productDbOrject = _cookiesDbContext.Products.First(x => x.ProductId.Equals(product.ProductId));

                productDbOrject.Price       = product.Price ?? productDbOrject.Price;
                productDbOrject.ProductName = product.ProductName ?? productDbOrject.ProductName;
                productDbOrject.Stock.NumberOfItemsAvailable = product.ProductStocksAvailable ?? productDbOrject.Stock.NumberOfItemsAvailable;
                productDbOrject.Stock.NumberOfDefectiveItems = product.ProductStocksDamage ?? productDbOrject.Stock.NumberOfDefectiveItems;
                return(_cookiesDbContext.SaveChanges() > 0);
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
        }
        public bool AddCustomer(Customer customer)
        {
            var newDbCustomer = new DbContext.Customer()
            {
                Name    = customer.Name,
                Address = customer.Address,
                Phone   = customer.Phone
            };

            _cookiesDbContext.Customers.Add(newDbCustomer);
            return(_cookiesDbContext.SaveChanges() > 0);
        }
        public bool UpdateStock(IEnumerable <Domain.Model.Stock> stocks)
        {
            stocks.ForEach(st =>
            {
                var dbStock = _cookiesDbContext.Stocks.First(dbS => dbS.ProductId.Equals(st.ProductId));

                dbStock.NumberOfDefectiveItems = st.NumberOfDefectiveItems;
                dbStock.NumberOfItemsAvailable = st.NumberOfItemsAvailable;
            });

            return(_cookiesDbContext.SaveChanges() > 0);
        }
Пример #5
0
        public bool UpdateStock(IEnumerable <Stock> stocks)
        {
            var enumerable = stocks as IList <Stock> ?? stocks.ToList();

            _cookiesDbContext.Stocks
            .Where(stt => enumerable.Select(st => st.ProductId)
                   .Contains(stt.ProductId))
            .ForEach(st =>
            {
                st.NumberOfDefectiveItems =
                    enumerable.First(s => s.ProductId.Equals(st.ProductId)).NumberOfDefectiveItems;

                st.NumberOfItemsAvailable =
                    enumerable.First(s => s.ProductId.Equals(st.ProductId)).NumberOfItemsAvailable;
            });

            return(_cookiesDbContext.SaveChanges() > 0);
        }
        public bool PlaceOrder(Domain.Model.Order order)
        {
            var newDbOrder = new Order()
            {
                CustomerId  = order.CustomerId,
                DateOfOrder = DateTime.UtcNow
            };

            var newDbOrderdetails = order.OrderDetails.Select(od => new OrderDetail()
            {
                ProductId = od.ProductId,
                Quantity  = od.Quantity,
                OrderId   = newDbOrder.Id
            });

            newDbOrder.TotalCost = order.OrderDetails.Sum(x => (x.Quantity * x.Price));

            _cookiesDbContext.Orders.Add(newDbOrder);
            _cookiesDbContext.OrderDetails.AddRange(newDbOrderdetails);

            return(_cookiesDbContext.SaveChanges() > 0);
        }