public List <OrderItem> GetOrderItemsByOrder(Order order)
        {
            using var context = new project0Context(_contextOptions);
            var dbOrderItems = context.OrderItems
                               .Where(o => o.OrderId == order.OrderId)
                               .Include(o => o.Product)
                               .ToList();
            var result = new List <OrderItem>();

            foreach (var orderItem in dbOrderItems)
            {
                var newProduct = new Product()
                {
                    ProductId   = orderItem.Product.ProductId,
                    ProductName = orderItem.Product.ProductName,
                    Price       = orderItem.Product.Price
                };
                var newOrderItem = new OrderItem(orderItem.OrderId, newProduct, orderItem.Quantity, orderItem.Total)
                {
                    ItemId = orderItem.ItemId
                };
                result.Add(newOrderItem);
            }
            return(result);
        }
        public OrderItem GetOrderItemById(int id)
        {
            using var context = new project0Context(_contextOptions);
            var dbOrderItem = context.OrderItems
                              .Where(o => o.ItemId == id)
                              .Include(o => o.Product)
                              .FirstOrDefault();

            if (dbOrderItem == null)
            {
                return(null);
            }
            var newAnimal = new Product()
            {
                ProductId   = dbOrderItem.Product.ProductId,
                ProductName = dbOrderItem.Product.ProductName,
                Price       = dbOrderItem.Product.Price
            };
            var result = new OrderItem(dbOrderItem.OrderId, newAnimal, dbOrderItem.Quantity, (decimal)dbOrderItem.Total)
            {
                ItemId = dbOrderItem.ItemId
            };

            return(result);
        }
        public void UpdateOrderItem(OrderItem orderItem)
        {
            using var context = new project0Context(_contextOptions);
            var dbOrderItem = context.OrderItems
                              .Where(o => o.ItemId == orderItem.ItemId)
                              .FirstOrDefault();

            dbOrderItem.Quantity = orderItem.Quantity;
            dbOrderItem.Total    = orderItem.PurchasePrice;
            context.SaveChanges();
        }
Beispiel #4
0
 public ActionResult AddOrderItem(OrderItemViewModel orderItem)
 {
     if (ModelState.IsValid)
     {
         var order             = _orderRepo.GetOrderById(orderItem.OrderId);
         var product           = _productRepo.GetProductById(orderItem.ProductId);
         var locationInventory = _storeRepo.GetInventoryByStore(order.Store);
         var invItem           = locationInventory.Find(i => i.ProductName == product.ProductName);
         if (invItem.Stock - orderItem.Quantity < 0)
         {
             TempData["QuantityError"] = $"Error. Quantity is too many, not enough {product.ProductName}(s) in inventory. Currently have {invItem.Stock} {product.ProductName}(s) in stock.";
             return(RedirectToAction("AddOrderItem", new { OrderId = orderItem.OrderId }));
         }
         else
         {
             bool existInOrder = order.OrderItems.Any(o => o.Product.ProductId == product.ProductId);
             var  total        = product.Price * orderItem.Quantity;
             if (existInOrder)
             {
                 foreach (var thing in order.OrderItems)
                 {
                     if (thing.Product.ProductId == orderItem.ProductId)
                     {
                         var existingOrder = _orderRepo.GetOrderItemById(thing.ItemId);
                         existingOrder.Quantity      += orderItem.Quantity;
                         existingOrder.PurchasePrice += (decimal)total;
                         order.OrderTotalPrice       += (decimal)total;
                         _orderRepo.UpdateOrderItem(existingOrder);
                         _orderRepo.UpdateOrder(order);
                     }
                 }
                 ;
             }
             else
             {
                 var newItem = new OrderItem(orderItem.OrderId, product, orderItem.Quantity, (decimal)total);
                 order.OrderTotalPrice += (decimal)total;
                 _orderRepo.CreateOrderItem(newItem);
                 _orderRepo.UpdateOrder(order);
             }
             invItem.Stock -= orderItem.Quantity;
             _storeRepo.UpdateInventory(invItem.StoreId, invItem.ProductName, invItem.Stock);
             TempData["AddOrderItemSuccess"] = $"Success! Added {orderItem.Quantity} {product.ProductName}(s) to your order.";
             return(RedirectToAction("AddOrderItem", new { OrderId = order.OrderId }));
         }
     }
     else
     {
         throw new Exception("Controller Error!");
     }
 }
        public void CreateOrderItem(OrderItem orderItem)
        {
            using var context = new project0Context(_contextOptions);
            var orderItemEntry = new Models.OrderItem()
            {
                OrderId   = orderItem.OrderId,
                ProductId = orderItem.Product.ProductId,
                Quantity  = orderItem.Quantity,
                Total     = orderItem.PurchasePrice
            };

            context.OrderItems.Add(orderItemEntry);
            context.SaveChanges();
        }