コード例 #1
0
        public async Task <OrderInventory> AddOrderInventoryAsync(OrderInventory orderInventory)
        {
            _dbContext.OrderInventory.Add(orderInventory);
            await _dbContext.SaveChangesAsync();

            return(orderInventory);
        }
コード例 #2
0
        public async Task <IActionResult> HandleCustomerOrderSubmission(CustomerOrderFormViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index", "Order", new { error = "You done goofed!" }));
            }

            var customerForOrder = await _dbContext.Customer.SingleOrDefaultAsync(c => c.Email == model.Email);

            var newOrder = new Order
            {
                CustomerId     = customerForOrder.Id,
                DateCreatedUTC = DateTime.UtcNow
            };

            _dbContext.Order.Add(newOrder);
            await _dbContext.SaveChangesAsync();

            decimal orderTotal = 0;

            foreach (var item in model.OrderInventory)
            {
                var inventoryItem = await _dbContext.Inventory.SingleOrDefaultAsync(i => i.Id == item.InventoryId);

                var enoughInventoryAvailable = inventoryItem.AmountAvailable >= item.Quantity;

                if (enoughInventoryAvailable)
                {
                    item.OrderId = newOrder.Id;

                    _dbContext.OrderInventory.Add(item);
                    await _dbContext.SaveChangesAsync();

                    orderTotal += (inventoryItem.PricePerUnit * item.Quantity);

                    var newAvailableAmount = inventoryItem.AmountAvailable - item.Quantity;

                    inventoryItem.AmountAvailable = newAvailableAmount;

                    _dbContext.Inventory.Update(inventoryItem);
                    await _dbContext.SaveChangesAsync();
                }
            }

            var newPayment = new Payment
            {
                CustomerId = customerForOrder.Id,
                OrderId    = newOrder.Id,
                Total      = orderTotal
            };

            _dbContext.Payment.Add(newPayment);
            await _dbContext.SaveChangesAsync();

            return(RedirectToAction("Index", "Home"));
        }
コード例 #3
0
        public async Task <Customer> CreateCustomerAsync(Customer customer)
        {
            _dbContext.Customer.Add(customer);
            await _dbContext.SaveChangesAsync();

            return(customer);
        }
コード例 #4
0
        public async Task <Inventory> UpdateInventoryAsync(Inventory inventory)
        {
            _dbContext.Inventory.Update(inventory);
            await _dbContext.SaveChangesAsync();

            return(inventory);
        }