public string Add(Book book) { using (BookShoppeDBContext booksContext = new BookShoppeDBContext()) { // Transaction Meathod Testing using (DbContextTransaction dbTran = booksContext.Database.BeginTransaction()) { try { if (book.UserID == 0) { return("User ID needed"); } booksContext.Books.Add(book); booksContext.SaveChanges(); dbTran.Commit(); } catch (Exception) { dbTran.Rollback(); return(null); } } } return(null); }
public void RemoveBookFormUserCart(int id) { using (BookShoppeDBContext _context = new BookShoppeDBContext()) { CartBook cartBooks = _context.CartBooks.Where(cb => cb.BookID == id).SingleOrDefault(); _context.CartBooks.Remove(cartBooks); _context.SaveChanges(); } }
public void RemoveBookFormWishlist(int id) { using (BookShoppeDBContext _context = new BookShoppeDBContext()) { WishList wishlist = _context.WishList.Where(w => w.BookID == id).SingleOrDefault(); _context.WishList.Remove(wishlist); _context.SaveChanges(); } }
public string Edit(Book book) { using (BookShoppeDBContext booksContext = new BookShoppeDBContext()) { booksContext.Entry(book).State = EntityState.Modified; booksContext.SaveChanges(); return(null); } }
public void Delete(int BookID) { using (BookShoppeDBContext bookContext = new BookShoppeDBContext()) { Book book = bookContext.Books.Where(id => id.BookID == BookID).FirstOrDefault(); bookContext.Books.Remove(book); bookContext.SaveChanges(); } }
public string AddUser(User user) { BookShoppeDBContext _Context = new BookShoppeDBContext(); _Context.Users.Add(user); _Context.SaveChanges(); return(null); }
public bool Delete(int id) { using (var Context = new BookShoppeDBContext()) { User user = Context.Users.Where(ID => ID.UserID == id).FirstOrDefault(); Context.Users.Remove(user); Context.SaveChanges(); return(true); } }
public string DeleteLanguage(int id) { using (BookShoppeDBContext _context = new BookShoppeDBContext()) { Language language = _context.Languages.Where(l => l.LanguageID == id).SingleOrDefault(); _context.Languages.Remove(language); _context.SaveChanges(); return("Language Removed Successfully"); } }
public string DeleteGenre(int id) { using (BookShoppeDBContext _context = new BookShoppeDBContext()) { Genre genre = _context.Genres.Where(Id => Id.GenreID == id).FirstOrDefault(); _context.Genres.Remove(genre); _context.SaveChanges(); return("Genre Removed Successfully"); } }
public bool AddShipment(Shipment shipment) { using (BookShoppeDBContext _context = new BookShoppeDBContext()) { Shipment _shipment = _context.Shipments.Where(s => s.Address == shipment.Address && s.UserID == shipment.UserID).FirstOrDefault(); if (_shipment == null) { _context.Shipments.Add(shipment); _context.SaveChanges(); } else if (_shipment.Address != shipment.Address && _shipment.UserID != shipment.UserID) { _context.Shipments.Add(shipment); _context.SaveChanges(); } _shipment = _context.Shipments.Where(s => s.Address == shipment.Address && s.UserID == shipment.UserID).SingleOrDefault(); return(PlaceOrder(_shipment)); } }
public string AddToWishList(int userID, int bookID) { using (BookShoppeDBContext _context = new BookShoppeDBContext()) { WishList wishlist = new WishList() { UserID = userID, BookID = bookID }; _context.WishList.Add(wishlist); _context.SaveChanges(); return(null); } }
public string AddGenre(Genre genre) { using (BookShoppeDBContext _context = new BookShoppeDBContext()) { _context.Genres.Add(genre); try { _context.SaveChanges(); } catch (DbUpdateException e) { return(e.Message); } return(null); } }
public bool PlaceOrder(Shipment shipment) { using (BookShoppeDBContext _context = new BookShoppeDBContext()) { Order order = new Order(); order.ShipmentID = shipment.ShipmentID; Cart cart = _context.Carts.Where(c => c.UserID == shipment.UserID && c.IsOrdered == false).SingleOrDefault(); order.CartID = cart.CartID; _context.Orders.Add(order); cart.IsOrdered = true; _context.SaveChanges(); return(true); } }
public string AddLanguage(Language language) { using (BookShoppeDBContext _context = new BookShoppeDBContext()) { _context.Languages.Add(language); try { _context.SaveChanges(); } catch (Exception e) { return(e.Message); } return(null); } }
public string EditUser(User user) { BookShoppeDBContext _context = new BookShoppeDBContext(); _context.Entry(user).State = System.Data.Entity.EntityState.Modified; try { _context.SaveChanges(); } catch (DbUpdateException e) { if (e.InnerException.InnerException.Message != null) { return("The User Name should not be duplicated"); } else { return("Please fill out the form correctly and sumbit your values"); } } return(null); }
// Such a complex coding - to check the cart exist in the orders table and which cart is not in the order then the book is added in the cart // if the user dont have any cart then it create a new cart and add the book // if the user has only one cart in orders then it creates new cart and add the book in the cart public int AddToCart(int userID, int bookID) { using (BookShoppeDBContext _context = new BookShoppeDBContext()) { List <Cart> user_carts = _context.Carts.Where(c => c.UserID == userID).ToList(); Cart _cart = new Cart(); int no_of_carts = user_carts.Count(); if (no_of_carts == 2) // When User Has Two Carts Probably One In Orders Another One For New Add To Carts { Cart cartOne = user_carts.ElementAt(0); Cart cartTwo = user_carts.ElementAt(1); if (cartOne.IsOrdered == false) // Check Wheather The First One Is Not In Orders To Add The New Book { CartBook cartbooks = new CartBook() { CartID = cartOne.CartID, BookID = bookID, }; // Get Book Into Object And Add The Book Rate Into The Cart Rate Book book = _context.Books.Where(b => b.BookID == bookID).SingleOrDefault(); cartOne.CartRate = cartOne.CartRate + book.Price; _context.CartBooks.Add(cartbooks); _context.SaveChanges(); return(cartOne.CartID); } else if (cartTwo.IsOrdered == false) //Or Else Check Wheather The First Ssecond Is Not In Orders To Add The New Book { CartBook cartbooks = new CartBook() { CartID = cartTwo.CartID, BookID = bookID, }; // Get Book Into Object And Add The Book Rate Into The Cart Rate Book book = _context.Books.Where(b => b.BookID == bookID).SingleOrDefault(); cartOne.CartRate = cartOne.CartRate + book.Price; _context.CartBooks.Add(cartbooks); _context.SaveChanges(); return(cartTwo.CartID); } } else if (no_of_carts == 1) // When The User Has One Cart That May Be In Orders Or In Carts To Add New Books { Cart cart = user_carts.ElementAt(0); if (cart.IsOrdered == false) // Check If the Cart Is Not Added Into the Orders { CartBook cartbooks = new CartBook() { CartID = cart.CartID, BookID = bookID, }; // Get Book Into Object And Add The Book Rate Into The Cart Rate Book book = _context.Books.Where(b => b.BookID == bookID).SingleOrDefault(); cart.CartRate = cart.CartRate + book.Price; _context.CartBooks.Add(cartbooks); _context.SaveChanges(); return(cart.CartID); } else if (cart.IsOrdered != false)// Check If The Cart Is Already Added In To The Orders { // Create A New Cart For The User Cart new_cart = new Cart() { UserID = userID, }; _context.Carts.Add(new_cart); _context.SaveChanges(); _cart = GetCartNotInOrder(userID); if (_cart != null) { CartBook cartbooks = new CartBook() { CartID = _cart.CartID, BookID = bookID, }; // Get Book Into Object And Add The Book Rate Into The Cart Rate Book book = _context.Books.Where(b => b.BookID == bookID).SingleOrDefault(); _cart.CartRate = _cart.CartRate + book.Price; _context.CartBooks.Add(cartbooks); _context.SaveChanges(); return(_cart.CartID); } } } else if (no_of_carts == 0) // Wheather The User Has No Carts { // Create A New Cart For The User Cart cart = new Cart() { UserID = userID, }; _context.Carts.Add(cart); _context.SaveChanges(); cart = GetCartNotInOrder(userID); using (var dbContextTransaction = _context.Database.BeginTransaction()) { CartBook cartbook = new CartBook() { CartID = cart.CartID, BookID = bookID, }; _context.CartBooks.Add(cartbook); // Get Book Into Object And Add The Book Rate Into The Cart Rate Book book = _context.Books.Where(b => b.BookID == bookID).SingleOrDefault(); cart.CartRate = book.Price + cart.CartRate; _context.SaveChanges(); dbContextTransaction.Commit(); return(cart.CartID); } } //foreach (Cart cart in user_carts) //{ // // Order order = _context.Orders.Where(o => o.CartID == cart.CartID).SingleOrDefault(); // if (cart.IsOrdered == false) // The Current Cart Is Not In The Orders Table // { // CartBook cartbooks = new CartBook() // { // CartID = cart.CartID, // BookID = bookID, // }; // // Get Book Into Object And Add The Book Rate Into The Cart Rate // Book book = _context.Books.Where(b => b.BookID == bookID).SingleOrDefault(); // cart.CartRate = book.Price + cart.CartRate; // _context.CartBooks.Add(cartbooks); // _context.SaveChanges(); // return cart.CartID; // } // else if (cart.IsOrdered != false) // The Cart Is Already Added In The Orders // { // Cart new_cart = new Cart() // { // UserID = userID, // }; // _context.Carts.Add(new_cart); // _context.SaveChanges(); // _cart = GetCartNotInOrder(userID); // if (_cart!=null) // { // CartBook cartbooks = new CartBook() // { // CartID = _cart.CartID, // BookID = bookID, // }; // _context.CartBooks.Add(cartbooks); // _context.SaveChanges(); // return cart.CartID; // } // } //} //if (user_carts.Count==0) //{ // Cart cart = new Cart() // { // UserID = userID, // }; // _context.Carts.Add(cart); // _context.SaveChanges(); // _cart = GetCartNotInOrder(userID); // CartBook cartbook = new CartBook() // { // CartID = _cart.CartID, // BookID = bookID, // }; // _context.CartBooks.Add(cartbook); // _context.SaveChanges(); // return cart.CartID; //} return(0); } }