public async Task <ImportOrder> Create(ImportOrder orderToCreate,
                                               IList <ProductItem> importedProductsData)
        {
            ImportOrder order = new ImportOrder()
            {
                PlacementDate     = orderToCreate?.PlacementDate ?? DateTime.MinValue,
                WholesalerName    = orderToCreate.WholesalerName,
                WholesalerAddress = orderToCreate.WholesalerAddress,
                WholesalerPhone   = orderToCreate.WholesalerPhone,
            };
            var importedProducts = new List <ImportedProduct>();
            var productsQuery    = _context.Products
                                   .Include(p => p.ImportedProducts)
                                   .ThenInclude(op => op.ImportOrder);

            foreach (var productItem in importedProductsData)
            {
                var product = await productsQuery
                              .FirstOrDefaultAsync(p => p.ID == productItem.Product.ID);

                var importedProduct = new ImportedProduct
                {
                    Product     = product,
                    ImportOrder = order,
                    Quantity    = productItem.Quantity
                };
                product.ImportedProducts.Add(importedProduct);
                product.Stock = product.Stock + productItem.Quantity;

                importedProducts.Add(importedProduct);

                _context.Update(product);
            }
            order.ImportedProducts = importedProducts;
            _context.Add(order);
            await _context.SaveChangesAsync();

            return(order);
        }
Exemple #2
0
        public async Task <Order> Create(Order orderToCreate, IList <ProductItem> orderedProductsData)
        {
            Order order = new Order()
            {
                PlacementDate    = orderToCreate?.PlacementDate ?? DateTime.MinValue,
                Customer         = orderToCreate.Customer,
                RecipientName    = orderToCreate.RecipientName,
                RecipientAddress = orderToCreate.RecipientAddress,
                RecipientPhone   = orderToCreate.RecipientPhone,
            };
            var orderedProducts = new List <OrderedProduct>();
            var products        = await _context.Products
                                  .Include(p => p.OrderedProducts)
                                  .ThenInclude(op => op.Order)
                                  .ToListAsync();

            foreach (var productItem in orderedProductsData)
            {
                var product        = products.FirstOrDefault(p => p.ID == productItem.Product.ID);
                var orderedProduct = new OrderedProduct
                {
                    Product  = product,
                    Order    = order,
                    Quantity = productItem.Quantity
                };
                product.OrderedProducts.Add(orderedProduct);
                if (product.Stock - productItem.Quantity >= 0)
                {
                    product.Stock = product.Stock - productItem.Quantity;
                }
                else
                {
                    throw new DbUpdateException();
                }
                orderedProducts.Add(orderedProduct);
                _context.Update(product);
            }
            order.OrderedProducts = orderedProducts;
            _context.Add(order);
            await _context.SaveChangesAsync();

            return(order);
        }
Exemple #3
0
        internal static async void EnsurePopulated(IApplicationBuilder app)
        {
            StoreDbContext context = app.ApplicationServices
                                     .CreateScope().ServiceProvider
                                     .GetRequiredService <StoreDbContext>();

            if ((await context.Database.GetPendingMigrationsAsync()).Any())
            {
                await context.Database.MigrateAsync();
            }

            if (!await context.Products.AnyAsync())
            {
                await context.Products.AddRangeAsync(

                    new Product
                {
                    Name        = "Kayak",
                    Description = "A boat for one person",
                    Category    = "Watersports",
                    Price       = 275
                },
                    new Product
                {
                    Name        = "Lifejacket",
                    Description = "Protective and fashionable",
                    Category    = "Watersports",
                    Price       = 48.95m
                },
                    new Product
                {
                    Name        = "Soccer Ball",
                    Description = "FIFA-approved size and weight",
                    Category    = "Soccer",
                    Price       = 19.50m
                },
                    new Product
                {
                    Name        = "Corner Flags",
                    Description = "Give your playing field a professional touch",
                    Category    = "Soccer",
                    Price       = 34.95m
                },
                    new Product
                {
                    Name        = "Stadium",
                    Description = "Flat-packed 35,000-seat stadium",
                    Category    = "Soccer",
                    Price       = 79500
                },
                    new Product
                {
                    Name        = "Thinking Cap",
                    Description = "Improve brain efficiency by 75%",
                    Category    = "Chess",
                    Price       = 16
                },
                    new Product
                {
                    Name        = "Unsteady Chair",
                    Description = "Secretly give your opponent a disadvantage",
                    Category    = "Chess",
                    Price       = 29.95m
                },
                    new Product
                {
                    Name        = "Human Chess Board",
                    Description = "A fun game for the family",
                    Category    = "Chess",
                    Price       = 75
                },
                    new Product
                {
                    Name        = "Bling-Bling King",
                    Description = "Gold-plated, diamond-studded King",
                    Category    = "Chess",
                    Price       = 1200
                }
                    );

                await context.SaveChangesAsync();
            }
        }
 public async Task SaveProductAsync(Product p)
 {
     await _context.SaveChangesAsync();
 }