private void UpdateTotal(IEnumerable <OrderItem> orderItems, IBookStoreAPIContext db) { decimal total = 0; foreach (OrderItem item in orderItems) { total += db.Books.Find(item.BookId).Price; } this.TotalAmount = total; }
public static bool BookAvailable(string id, int quantity, IBookStoreAPIContext db) { if (db.Books.FirstOrDefault(a => a.Id == id) == null) { return(false); } int stock = db.Books.FirstOrDefault(a => a.Id == id).InStock; int reserved = 0; if (db.OrderItems.Count() != 0) { reserved = db.OrderItems.Where(item => item.BookId == id && item.ShippingStatus == Enums.ShippingStatus.NotShipped) .Sum(b => (int?)b.Quantity) ?? 0; } if (quantity > stock - reserved) { return(false); } return(true); }
public IEnumerable <OrderItem> CreateOrderItemsFromCart(Cart cart, IBookStoreAPIContext db) { List <OrderItem> newOrderItems = new List <OrderItem>(); foreach (CartItem item in cart.CartItems) { if (BookAvailable(item.Book.Id, item.Quantity, db)) { OrderItem orderItem = new OrderItem(); orderItem.Id = Util.Util.GetNewId(); orderItem.OrderId = this.Id; orderItem.BookId = item.Book.Id; orderItem.Quantity = item.Quantity; orderItem.SellingPrice = db.Books.Find(orderItem.BookId).Price; orderItem.ShippingStatus = Enums.ShippingStatus.NotShipped; orderItem.CreatedAt = DateTime.Now; newOrderItems.Add(orderItem); } } UpdateTotal(newOrderItems, db); return(newOrderItems); }
public OrdersController(IBookStoreAPIContext context) { db = context; }
private bool BookAvailable(string id, int quantity, IBookStoreAPIContext db) { return(DbFunctions.BookAvailable(id, quantity, db)); }