// Place the order and save in the data base public void CreateOrder(Order order) { order.DateTime = DateTime.Now; order.OrderTotal = GetShoppingCartITotal(); _context.Orders.Add(order); _context.SaveChanges(); var shoppingCartItems = GetShoppingCartItems(); foreach (var shoppingCartItem in shoppingCartItems) { var orderDetail = new OrderDetail { Quantity = shoppingCartItem.Quantity, Price = shoppingCartItem.Book.Price, BookId = shoppingCartItem.Book.BookId, BookName = shoppingCartItem.Book.BookName, BookRating = 5, OrderId = order.OrderId, UserId = order.UserId }; _context.OrderDetails.Add(orderDetail); } _context.SaveChanges(); }
// Add a book to the Cart and Save it (the item) in the Data Base, ShoppingCartItems table public void AddToCart(Book book, int quantity) { var shoppingCartItem = _context.ShoppingCartItems.SingleOrDefault (s => s.Book.BookId == book.BookId && s.ShoppingCartId == _shoppingCart.ShoppingCartId); // if If the condition is true: We are creating a new instance of ShoppingCartItem about the Book (not instance of Shopping Cart) if (shoppingCartItem == null) { shoppingCartItem = new ShoppingCartItem { ShoppingCartId = _shoppingCart.ShoppingCartId, Book = book, Quantity = quantity }; // Add this new Item into the same Shopping Cart _context.ShoppingCartItems.Add(shoppingCartItem); } else // Else if the Item allready exists in the same Shopping Cart { shoppingCartItem.Quantity++; } _context.SaveChanges(); }
public void Add(Author entity) { _booksStoreContext.Author.Add(entity); _booksStoreContext.SaveChanges(); }
public void Delete(Publisher entity) { _booksStoreContext.Remove(entity); _booksStoreContext.SaveChanges(); }