Beispiel #1
0
        public ActionResult AddItem(Library.OrderDetail item)
        {
            if (ModelState.IsValid)
            {
                var order     = _storeRepo.GetOrderById(item.OrderId);
                var inventory = _storeRepo.GetInventoryByLocationId(order.LocationId);
                var product   = inventory.Find(o => o.ProductId == item.ProductId);
                if (product.Quantity - item.Quantity < 0)
                {
                    return(RedirectToAction(nameof(AddItem), new { id = item.OrderId }));
                }
                if (order.OrderDetails.Any(o => o.ProductId == item.ProductId))
                {
                    foreach (var entry in order.OrderDetails)
                    {
                        if (entry.ProductId == item.ProductId)
                        {
                            entry.Quantity += item.Quantity;
                            _storeRepo.UpdateOrderDetail(entry);
                            return(RedirectToAction(nameof(Details), new { id = item.OrderId }));
                        }
                    }
                    ;
                }
                else
                {
                    _storeRepo.AddOrderItem(item);
                }

                product.Quantity -= item.Quantity;

                _storeRepo.UpdateInventory(product.LocationId, product.ProductId, product.Quantity);
                return(RedirectToAction(nameof(Details), new { id = item.OrderId }));
            }
            return(View("Index"));
        }