Ejemplo n.º 1
0
        public void AddProductToOrder(int productId, int orderId)
        {
            var order = _context.Orders
                        .Include(o => o.ProductLines)
                        .ThenInclude(pl => pl.Product)
                        .First(o => o.ID == orderId);

            var existingProductLine = _context.ProductLines.Where(pl => pl.Order.ID == orderId && pl.Product.ID == productId).FirstOrDefault();

            if (existingProductLine is not null)
            {
                existingProductLine.Quantity += 1;
            }
            else
            {
                var newProductLine = new ProductLine()
                {
                    Product = new()
                    {
                        ID = productId
                    },
                    Quantity = 1,
                    Order    = order
                };

                _context.ProductLines.Add(newProductLine);
            }

            order.TotalPrice = order.ProductLines.Sum(pl => pl.Quantity * pl.Product.Price);
            _context.SaveChanges();
        }
    }
Ejemplo n.º 2
0
 public void CreateOrder(Order order)
 {
     _context.Orders.Add(order);
     _context.SaveChanges();
 }