public void AddToCart(int mediaRecordId, int sizeId) { var shoppingCartItem = _archivesDbContext.ShoppingCartItems .SingleOrDefault( s => s.MediaRecordId == mediaRecordId && s.SizeId == sizeId && s.ShoppingCartId == ShoppingCartId); if (shoppingCartItem == null) { var cost = _archivesDbContext.OrderCosts.FirstOrDefault( c => c.SizeId == sizeId); var size = _archivesDbContext.OrderSizes.SingleOrDefault( s => s.OrderSizeId == sizeId); shoppingCartItem = new ShoppingCartItem { ShoppingCartId = ShoppingCartId, MediaRecordId = mediaRecordId, SizeId = sizeId, OrderSize = size, Cost = cost.Cost, Amount = 1 }; _archivesDbContext.ShoppingCartItems.Add(shoppingCartItem); } else { shoppingCartItem.Amount++; } _archivesDbContext.SaveChanges(); }
public void CreateOrder(Order order) { order.OrderPlaced = DateTime.Now; var shoppingCartItems = _shoppingCart.ShoppingCartItems; order.OrderTotal = _shoppingCart.GetShoppingCartTotal(); order.OrderDetails = new List <OrderDetail>(); //adding the order with its details foreach (var shoppingCartItem in shoppingCartItems) { var orderDetail = new OrderDetail { Amount = shoppingCartItem.Amount, MediaRecordId = shoppingCartItem.MediaRecordId, Size = shoppingCartItem.OrderSize, SizeId = shoppingCartItem.SizeId, Price = shoppingCartItem.Cost }; order.OrderDetails.Add(orderDetail); } _archivesDbContext.Orders.Add(order); _archivesDbContext.SaveChanges(); }