public void CreateOrder(Order order) { order.OrderPlacedDateTime = DateTime.Now; _qualityBookDbContext.Orders.Add(order); var subtotal = 0m; foreach (var item in _shoppingCart.ShoppingCartItems) { var OrderDetail = new OrderDetail() { Amount = item.Amount, BookId = item.Book.BookId, OrderId = order.OrderId, Price = item.Book.Price }; subtotal += OrderDetail.Amount * OrderDetail.Price; _qualityBookDbContext.OrderDetails.Add(OrderDetail); } //order.CustomerId order.State = "waiting"; order.Subtotal = subtotal; order.GST = subtotal * 0.15m; order.GrandTotal = subtotal * 1.15m; _qualityBookDbContext.Orders.Add(order); _qualityBookDbContext.SaveChanges(); }
public void AddToCart(Book book, int amount) { var shoppingCartItem = _qualityBookDbContext.ShoppingCartItems.SingleOrDefault( sci => sci.Book.BookId == book.BookId && sci.ShoppingCartId == ShoppingCartId ); if (shoppingCartItem == null) { shoppingCartItem = new ShoppingCartItem { ShoppingCartId = ShoppingCartId, Book = book, Amount = 1 }; _qualityBookDbContext.ShoppingCartItems.Add(shoppingCartItem); } else { shoppingCartItem.Amount++; } _qualityBookDbContext.SaveChanges(); }
public static void Initialize(QualityBookDbContext context) { context.Database.EnsureCreated(); if (context.Books.Any()) { return; // DB has been seeded } var categories = new Category[] { //new Category{ CategoryId=1,CategoryName = "dbcat1"}, //new Category{ CategoryId=2,CategoryName = "dbcat2"}, //new Category{ CategoryId=3,CategoryName = "dbcat3"} new Category { CategoryName = "dbcat1" }, new Category { CategoryName = "dbcat2" }, new Category { CategoryName = "dbcat3" } }; foreach (Category c in categories) { context.Categories.Add(c); } context.SaveChanges(); var suppliers = new Supplier[] { //new Supplier{ SupplierId = 1,SupplierName = "dbsup1"}, //new Supplier{ SupplierId = 2,SupplierName = "dbsup2"}, //new Supplier{ SupplierId = 3,SupplierName = "dbsup3"} new Supplier { SupplierName = "dbsup1" }, new Supplier { SupplierName = "dbsup2" }, new Supplier { SupplierName = "dbsup3" } }; foreach (Supplier s in suppliers) { context.Suppliers.Add(s); } context.SaveChanges(); var books = new Book[] { new Book { BookName = "dbbook1", Price = 1.11M, CategoryId = 1, SupplierId = 1, ShortDescription = "This is a very good book.", Author = "LiQi", PreferredFlag = 1, ImageUrl = "book1.jpg" }, new Book { BookName = "dbbook2", Price = 2.22M, CategoryId = 2, SupplierId = 2, ShortDescription = "This is a very good book.", Author = "LiQi", PreferredFlag = 2, ImageUrl = "book2.jpg" }, new Book { BookName = "dbbook3", Price = 3.33M, CategoryId = 3, SupplierId = 3, ShortDescription = "This is a very good book.", Author = "LiQi", PreferredFlag = 3, ImageUrl = "book3.jpg" } }; foreach (Book b in books) { context.Books.Add(b); } context.SaveChanges(); }