コード例 #1
0
        public async Task TestUpdateInventoryFail()
        {
            var options = new DbContextOptionsBuilder <BusinessContext>()
                          .UseInMemoryDatabase(databaseName: "test_inventory_update_fail")
                          .Options;

            using (var bc = new BusinessContext(options)) {
                var repository = new Repository(bc);

                int productId  = 1;
                int locationId = 1;
                var inventory  = new Inventory {
                    Product = new Product {
                        ProductId = productId
                    },
                    Location = new Location {
                        LocationId = locationId
                    },
                    Quantity = 3
                };
                bc.Add(inventory);
                await bc.SaveChangesAsync();

                var orderItem = new BusinessOrderItem {
                    Product = new BusinessProduct {
                        ProductId = productId
                    },
                    Quantity = 4
                };
                var result = await repository.updateInventoryAsync(locationId, orderItem);

                Assert.Null(result);
            }
        }
コード例 #2
0
        private void AddToCart(object sender, RoutedEventArgs e)
        {
            if (lstGoods.SelectedIndex < 0)
            {
                return;
            }
            BusinessOrderItem orderItem = new BusinessOrderItem();

            orderItem.GoodId    = (lstGoods.SelectedItem as BusinessGood).GoodId;
            orderItem.GoodName  = (lstGoods.SelectedItem as BusinessGood).GoodName;
            orderItem.GoodCount = 1;
            RefreshOrderItemSum(orderItem);
            Cart.Add(orderItem);
            sumLabel.Content = string.Format(new System.Globalization.CultureInfo("en-US"), "{0:C}", CartSum);
        }
コード例 #3
0
        /// <summary>
        /// Updates an inventory with information from an order item
        /// </summary>
        /// <param name="locationId">The location of the product to update information for</param>
        /// <param name="businessOrderItem">The order item with update information</param>
        /// <returns>The same order item if the inventory quantity was updated successfully, false otherwise</returns>
        public async Task <BusinessOrderItem> updateInventoryAsync(int locationId, BusinessOrderItem businessOrderItem)
        {
            Inventory inventory = await _context.Inventories.Where(i => i.Location.LocationId == locationId && i.Product.ProductId == businessOrderItem.Product.ProductId).FirstOrDefaultAsync();

            int update = inventory.Quantity - businessOrderItem.Quantity;

            if (update >= 0)
            {
                inventory.Quantity = update;
                _context.Update(inventory);
                await _context.SaveChangesAsync();

                return(businessOrderItem);
            }
            return(null);
        }
コード例 #4
0
        /// <summary>
        /// Updates the context inventory with information from an incoming order item
        /// </summary>
        /// <param name="businessOrder">The order that contains the order item</param>
        /// <param name="businessOrderItem">The order item to be added to the order</param>
        /// <returns>A representation of the updated order</returns>
        public async Task <BusinessOrder> updateOrderAsync(BusinessOrder businessOrder, BusinessOrderItem businessOrderItem)
        {
            Order order = await _context.Orders.Include(o => o.OrderItems)
                          .Where(o => o.OrderId == businessOrder.OrderId)
                          .FirstOrDefaultAsync();

            Inventory inventory = await _context.Inventories.Include(i => i.Product)
                                  .Where(i => i.Product.ProductId == businessOrderItem.Product.ProductId)
                                  .FirstOrDefaultAsync();

            OrderItem orderItem;

            if (businessOrderItem.OrderItemId == -1)
            {
                orderItem = new OrderItem {
                    Quantity = businessOrderItem.Quantity,
                    Order    = order,
                    Product  = await _context.Products.Where(p => p.ProductId == businessOrderItem.Product.ProductId).FirstOrDefaultAsync(),
                };
                order.OrderItems.Add(orderItem);
                businessOrder.OrderItems.Add(businessOrderItem);
                _context.Add(orderItem);
            }
            else
            {
                orderItem = await _context.OrderItems.Where(oi => oi.OrderItemId == businessOrderItem.OrderItemId).FirstOrDefaultAsync();

                orderItem.Quantity += businessOrderItem.Quantity;
                _context.Update(orderItem);
            }
            inventory.Quantity -= businessOrderItem.Quantity;
            order.Total         = businessOrder.Total;
            _context.Update(order);
            _context.Update(inventory);
            await _context.SaveChangesAsync();

            return(businessOrder);
        }
コード例 #5
0
        public async Task <IActionResult> Create(OrderItemViewModel orderItemView)
        {
            try {
                int locationId   = orderItemView.Location.LocationId;
                var sessionValue = HttpContext.Session.GetInt32("CustomerId");
                if (ModelState.IsValid && sessionValue != null)
                {
                    int customerId = Convert.ToInt32(sessionValue);

                    // get the order with no order date for this location
                    await _repository.createOrderAsync(customerId, locationId);

                    var orders = await _repository.listOrdersAsync();

                    var order = orders.Where(o => o.Customer.CustomerId == customerId && o.Location.LocationId == locationId && o.OrderDate == null)
                                .FirstOrDefault();

                    // Make a new order item for this product
                    BusinessOrderItem orderItem = new BusinessOrderItem {
                        OrderItemId = -1,
                        Quantity    = orderItemView.Quantity,
                        Order       = order,
                        Product     = orderItemView.Product
                    };

                    // if the order already has the incoming product id...
                    var existingOrderItems = await _repository.listOrderItemsAsync();

                    var existingOrderItem = existingOrderItems.Where(oi => oi.Order.OrderId == order.OrderId && oi.Order.Location.LocationId == locationId && oi.Product.ProductId == orderItemView.Product.ProductId).FirstOrDefault();

                    if (existingOrderItem != null)
                    {
                        // orderItem.Quantity += existingOrderItem.Quantity;
                        orderItem.OrderItemId = existingOrderItem.OrderItemId;
                        if (existingOrderItem.Quantity + orderItem.Quantity > 10)
                        {
                            orderItem.Quantity = 0;
                        }
                    }

                    // update the order in the system
                    order.Total += orderItemView.Quantity * orderItemView.Product.ProductPrice;
                    var inventory = await _repository.updateOrderAsync(order, orderItem);

                    if (inventory == null)
                    {
                        ModelState.AddModelError("Quantity", "Please input valid quantity.");
                    }
                    else
                    {
                        _logger.LogInformation("{0} added {1} x {2} to Order #{3}", orderItem.Order.Customer.Username, orderItem.Quantity, orderItem.Product.ProductName, orderItem.Order.OrderId);
                        return(RedirectToAction(nameof(Index)));
                    }
                }
                return(View());
                // return View(orderItem);
            } catch (Exception exception) {
                _logger.LogCritical(exception.Message);
                return(RedirectToAction("Index", "Home"));
            }
        }
コード例 #6
0
 private void RefreshOrderItemSum(BusinessOrderItem item)
 {
     item.OrderItemSum = Goods.Where(p => p.GoodId == item.GoodId).FirstOrDefault().Price *item.GoodCount;
 }