public IEnumerable <Book> GetBookByUserID(int userID)
 {
     using (BookShoppeDBContext _context = new BookShoppeDBContext())
     {
         return(_context.Books.Include("Genre").Include("Language").Where(m => m.UserID == userID).ToList());
     }
 }
        public IEnumerable <Book> GetOrderedBookBySellerID(int userID)
        {
            using (BookShoppeDBContext _context = new BookShoppeDBContext())
            {
                IList <Book> sellerBooks = _context.Books.Where(m => m.UserID == userID).ToList();

                IList <Book> orderedSellerBooks = new List <Book>();

                foreach (Book book in sellerBooks)
                {
                    IList <CartBook> cartBooks = _context.CartBooks.Where(m => m.BookID == book.BookID).ToList();

                    foreach (CartBook cartBook in cartBooks)
                    {
                        IList <Order> orders = _context.Orders.Where(m => m.CartID == cartBook.CartID).ToList();

                        foreach (Order order in orders)
                        {
                            if (order.CartID == cartBook.CartID)
                            {
                                Book orderedBook = _context.Books.Include("Genre").Include("Language").Where(m => m.BookID == cartBook.BookID).SingleOrDefault();

                                orderedSellerBooks.Add(orderedBook);
                            }
                        }
                    }
                }
                if (orderedSellerBooks == null)
                {
                    return(null);
                }
                return(orderedSellerBooks);
            }
        }
 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 IEnumerable <Book> GetBooksByLanguage(int id)
 {
     using (BookShoppeDBContext _context = new BookShoppeDBContext())
     {
         return(_context.Books.Include("Genre").Include("Language").Where(b => b.LanguageID == id).ToList());
     }
 }
 public IEnumerable <Book> GetAllBooks()
 {
     using (BookShoppeDBContext booksContext = new BookShoppeDBContext())
     {
         return(booksContext.Books.Include("Genre").Include("Language").ToList());
     }
 }
 public IEnumerable <Language> GetAllLanguages()
 {
     using (BookShoppeDBContext _context = new BookShoppeDBContext())
     {
         return(_context.Languages.ToList());
     }
 }
        public IEnumerable <Book> GetUserCartDetails(int id)
        {
            List <Book> books = new List <Book>();

            using (BookShoppeDBContext _context = new BookShoppeDBContext())
            {
                try
                {
                    Cart cart = new Cart();
                    cart = GetCartNotInOrder(id);
                    if (cart != null)
                    {
                        int cartID = cart.CartID;

                        List <CartBook> cartBooks = _context.CartBooks.Where(cb => cb.CartID == cartID).ToList();

                        foreach (var item in cartBooks)
                        {
                            Book book = _context.Books.Where(ID => ID.BookID == item.BookID).SingleOrDefault();
                            books.Add(book);
                        }
                        return(books);
                    }
                    else
                    {
                        return(null);
                    }
                }
                catch (Exception)
                {
                    return(null);
                }
            }
        }
 public Book GetBookDetails(int bookID)
 {
     using (BookShoppeDBContext _context = new BookShoppeDBContext())
     {
         return(_context.Books.Include("Genre").Where(ID => ID.BookID == bookID).SingleOrDefault());
     }
 }
 public IEnumerable <Genre> GetAllGenres()
 {
     using (BookShoppeDBContext _context = new BookShoppeDBContext())
     {
         return(_context.Genres.ToList());
     }
 }
 public Book GetBookByID(int bookID)
 {
     using (BookShoppeDBContext booksContext = new BookShoppeDBContext())
     {
         Book book = booksContext.Books.Include("Genre").Include("Language").SingleOrDefault(ID => ID.BookID == bookID);
         return(book);
     }
 }
        public User ValidateLogIn(string userName, string password)
        {
            User _user = null;
            BookShoppeDBContext _Context = new BookShoppeDBContext();

            _user = _Context.Users.Include("Role").Where(u => u.UserName == userName && u.Password == password).SingleOrDefault();
            return(_user);
        }
 public string GetLanguageByLanguageID(int id)
 {
     using (BookShoppeDBContext _context = new BookShoppeDBContext())
     {
         Language language = _context.Languages.Where(l => l.LanguageID == id).SingleOrDefault();
         return(language.LanguageName);
     }
 }
 public string GetGenreByGenreID(int id)
 {
     using (BookShoppeDBContext _context = new BookShoppeDBContext())
     {
         Genre genre = _context.Genres.Where(ID => ID.GenreID == id).SingleOrDefault();
         return(genre.GenreName);
     }
 }
 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 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 string Edit(Book book)
 {
     using (BookShoppeDBContext booksContext = new BookShoppeDBContext())
     {
         booksContext.Entry(book).State = EntityState.Modified;
         booksContext.SaveChanges();
         return(null);
     }
 }
 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 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 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 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 AddUser(User user)
        {
            BookShoppeDBContext _Context = new BookShoppeDBContext();

            _Context.Users.Add(user);

            _Context.SaveChanges();

            return(null);
        }
 public bool CheckBookInWishList(int userID, int bookID)
 {
     using (BookShoppeDBContext _context = new BookShoppeDBContext())
     {
         WishList wishlist = _context.WishList.Where(ID => ID.UserID == userID && ID.BookID == bookID).SingleOrDefault();
         if (wishlist == null)
         {
             return(false);
         }
         return(true);
     }
 }
        public bool CheckCartInOrders(int id)
        {
            using (BookShoppeDBContext _context = new BookShoppeDBContext())
            {
                Cart  cart  = _context.Carts.Where(c => c.UserID == id).SingleOrDefault();
                Order order = _context.Orders.Where(o => o.CartID == cart.CartID).Single();

                if (order == null)
                {
                    return(false); // Return false when the Cart is not in the orders
                }
                return(true);      // Return true when the cart is available in the orders
            }
        }
 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 int GetCartRate(int cartID)
        {
            using (BookShoppeDBContext _context = new BookShoppeDBContext())
            {
                int             cartRate  = 0;
                List <CartBook> cartBooks = _context.CartBooks.Where(cb => cb.CartID == cartID).ToList();

                foreach (var item in cartBooks)
                {
                    Book book = _context.Books.Where(ID => ID.BookID == item.BookID).SingleOrDefault();
                    cartRate = cartRate + book.Price;
                }
                return(cartRate);
            }
        }
        public IEnumerable <Book> GetUserWishlist(int id)
        {
            List <Book> books = new List <Book>();

            using (BookShoppeDBContext _context = new BookShoppeDBContext())
            {
                List <WishList> wishlist = _context.WishList.Where(ID => ID.UserID == id).ToList();

                foreach (var item in wishlist)
                {
                    Book book = _context.Books.Where(ID => ID.BookID == item.BookID).SingleOrDefault();
                    books.Add(book);
                }
                return(books);
            }
        }
 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);
     }
 }
        // Not Used In Function
        private Cart CheckCartInOrder(int cartID)
        {
            using (BookShoppeDBContext _context = new BookShoppeDBContext())
            {
                Cart cart = _context.Carts.Where(c => c.UserID == cartID).SingleOrDefault();

                Order order = _context.Orders.Where(o => o.CartID == cartID).SingleOrDefault();

                if (order == null)
                {
                    return(cart);
                }

                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);
            }
        }