コード例 #1
0
 public Cart GetById(int bookId, int buyerId)
 {
     using (EfDatabaseContext db = new EfDatabaseContext())
     {
         return(db.Carts.SingleOrDefault(c => c.BookId == bookId && c.BuyerId == buyerId));
     }
 }
コード例 #2
0
 public Book GetById(int bookId)
 {
     using (EfDatabaseContext db = new EfDatabaseContext())
     {
         return(db.Books.SingleOrDefault(b => b.Id == bookId));
     }
 }
コード例 #3
0
        public bool Add(Distributor distributor)
        {
            if (distributor == null)                //cannot insert null entity
            {
                return(false);
            }

            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                if (db.Distributors.Any(d => d.Email == distributor.Email))                 //entity with duplicate email found
                {
                    return(false);
                }

                db.Distributors.Add(distributor);

                try
                {
                    db.SaveChanges();
                    return(true);
                }
                catch (DbEntityValidationException)                 //entity doesn't have valid properties
                {
                    return(false);
                }
            }
        }
コード例 #4
0
        public IEnumerable <Book> GetAll(BookOrder order, int pageNum, int pageSize)
        {
            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                IEnumerable <Book> books = db.Books.Skip((pageNum - 1) * pageSize).Take(pageSize);

                switch (order)
                {
                case BookOrder.DateDesc:
                    books = books.OrderByDescending(b => b.AddedAt);
                    break;

                case BookOrder.Date:
                    books = books.OrderBy(b => b.AddedAt);
                    break;

                case BookOrder.PriceDesc:
                    books = books.OrderByDescending(b => b.Price);
                    break;

                case BookOrder.Price:
                    books = books.OrderBy(b => b.Price);
                    break;
                    //case BookOrder.RatingDesc:
                    //	books = books.OrderByDescending(b => b.Rating);
                    //	break;
                    //case BookOrder.Rating:
                    //	books = books.OrderBy(b => b.Rating);
                    //	break;
                }

                return(books);
            }
        }
コード例 #5
0
 public IEnumerable <Review> GetByBookId(int bookId)
 {
     using (EfDatabaseContext db = new EfDatabaseContext())
     {
         return(db.Reviews.Where(r => r.BookId == bookId).ToArray());
     }
 }
コード例 #6
0
 public IEnumerable <Cart> GetByBuyerId(int buyerId)
 {
     using (EfDatabaseContext db = new EfDatabaseContext())
     {
         return(db.Carts.Where(c => c.BuyerId == buyerId).ToArray());
     }
 }
コード例 #7
0
        public bool Update(Buyer buyer)
        {
            if (buyer == null)              //cannot update with null entity
            {
                return(false);
            }

            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                Buyer trackedBuyer = db.Buyers.SingleOrDefault(b => b.Id == buyer.Id);

                if (trackedBuyer == null)                   //no entity found for given id
                {
                    return(false);
                }

                if (buyer.Name != null && buyer.Name.Trim().Length != 0)
                {
                    trackedBuyer.Name = buyer.Name.Trim();
                }
                if (buyer.Email != null && buyer.Email.Trim().Length != 0)
                {
                    trackedBuyer.Email = buyer.Email.Trim();
                }
                if (buyer.Password != null && buyer.Password.Trim().Length != 0)
                {
                    trackedBuyer.Password = buyer.Password.Trim();
                }

                db.SaveChanges();
                return(true);
            }
        }
コード例 #8
0
 public Buyer GetById(int buyerId)
 {
     using (EfDatabaseContext db = new EfDatabaseContext())
     {
         return(db.Buyers.SingleOrDefault(b => b.Id == buyerId));
     }
 }
コード例 #9
0
 public Distributor GetById(int distributorId)
 {
     using (EfDatabaseContext db = new EfDatabaseContext())
     {
         return(db.Distributors.SingleOrDefault(d => d.Id == distributorId));
     }
 }
コード例 #10
0
        public bool Update(Distributor distributor)
        {
            if (distributor == null)                //cannot update with null entity
            {
                return(false);
            }

            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                Distributor trackedDistributor = db.Distributors.SingleOrDefault(d => d.Id == distributor.Id);

                if (trackedDistributor == null)                 //no entity found for given id
                {
                    return(false);
                }

                if (distributor.Name != null && distributor.Name.Trim().Length != 0)
                {
                    trackedDistributor.Name = distributor.Name.Trim();
                }
                if (distributor.Email != null && distributor.Email.Trim().Length != 0)
                {
                    trackedDistributor.Email = distributor.Email.Trim();
                }
                if (distributor.Password != null && distributor.Password.Trim().Length != 0)
                {
                    trackedDistributor.Password = distributor.Password.Trim();
                }

                db.SaveChanges();
                return(true);
            }
        }
コード例 #11
0
 public Review GetById(int reviewId)
 {
     using (EfDatabaseContext db = new EfDatabaseContext())
     {
         return(db.Reviews.SingleOrDefault(r => r.Id == reviewId));
     }
 }
コード例 #12
0
 public IEnumerable <Review> GetByBuyerId(int buyerId)
 {
     using (EfDatabaseContext db = new EfDatabaseContext())
     {
         return(db.Reviews.Where(r => r.BuyerId == buyerId));
     }
 }
コード例 #13
0
        public bool Update(Book book)
        {
            if (book == null)                   //cannot update with null entity
            {
                return(false);
            }

            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                Book trackedBook = db.Books.SingleOrDefault(b => b.Id == book.Id);

                if (trackedBook == null)                    //no entity found for given id
                {
                    return(false);
                }

                if (book.Name != null && book.Name.Trim().Length > 0)
                {
                    trackedBook.Name = book.Name.Trim();
                }
                if (book.Description != null && book.Description.Trim().Length > 0)
                {
                    trackedBook.Description = book.Description.Trim();
                }
                if (book.DistributorId > 0)
                {
                    trackedBook.DistributorId = book.DistributorId;
                }
                if (book.Author != null && book.Author.Trim().Length > 0)
                {
                    trackedBook.Author = book.Author.Trim();
                }
                if (book.Publisher != null && book.Publisher.Trim().Length > 0)
                {
                    trackedBook.Publisher = book.Publisher.Trim();
                }
                if (book.Genre != null && book.Genre.Trim().Length > 0)
                {
                    trackedBook.Genre = book.Genre.Trim();
                }
                if (book.Language != null && book.Language.Trim().Length > 0)
                {
                    trackedBook.Language = book.Language.Trim();
                }
                if (book.Price > -1)
                {
                    trackedBook.Price = book.Price;
                }
                if (book.AddedAt != null && book.AddedAt.CompareTo(DateTime.Now) <= 0)
                {
                    trackedBook.AddedAt = book.AddedAt;
                }

                db.SaveChanges();
                return(true);
            }
        }
コード例 #14
0
        public Distributor GetByCredentials(string email, string password)
        {
            if (email == null || password == null)              //cannot search for null credentials
            {
                return(null);
            }

            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                return(db.Distributors.SingleOrDefault(d => d.Email == email && d.Password == password));
            }
        }
コード例 #15
0
        public IEnumerable <Book> FindByDistributor(string distributor, int pageNum, int pageSize)
        {
            if (distributor == null)                //cannot search for null distributor
            {
                return(null);
            }

            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                distributor = distributor.Trim().ToLower();
                return(db.Books
                       .Where(b => b.Distributor.Name.Trim().ToLower() == distributor)
                       .Skip((pageNum - 1) * pageSize)
                       .Take(pageSize));
            }
        }
コード例 #16
0
        public IEnumerable <Book> FindByAuthor(string author, int pageNum, int pageSize)
        {
            if (author == null)             //cannot search for null author
            {
                return(null);
            }

            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                author = author.Trim().ToLower();
                return(db.Books
                       .Where(b => b.Author.Trim().ToLower() == author)
                       .Skip((pageNum - 1) * pageSize)
                       .Take(pageSize));
            }
        }
コード例 #17
0
        public IEnumerable <Book> FindByGenre(string genre, int pageNum, int pageSize)
        {
            if (genre == null)                  //cannot search for null genre
            {
                return(null);
            }

            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                genre = genre.Trim().ToLower();
                return(db.Books
                       .Where(b => b.Genre.Trim().ToLower() == genre)
                       .Skip((pageNum - 1) * pageSize)
                       .Take(pageSize));
            }
        }
コード例 #18
0
        public IEnumerable <Book> FindByPublisher(string publisher, int pageNum, int pageSize)
        {
            if (publisher == null)              //cannot search for null publisher
            {
                return(null);
            }

            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                publisher = publisher.Trim().ToLower();
                return(db.Books
                       .Where(b => b.Genre.Trim().ToLower() == publisher)
                       .Skip((pageNum - 1) * pageSize)
                       .Take(pageSize));
            }
        }
コード例 #19
0
        public bool Delete(int buyerId)
        {
            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                Buyer buyer = db.Buyers.SingleOrDefault(b => b.Id == buyerId);

                if (buyer == null)                  //no entity found for given id
                {
                    return(false);
                }

                db.Buyers.Remove(buyer);
                db.SaveChanges();
                return(true);
            }
        }
コード例 #20
0
        public bool Delete(int bookId)
        {
            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                Book book = db.Books.SingleOrDefault(b => b.Id == bookId);

                if (book == null)                   //no entity found for the given id
                {
                    return(false);
                }

                db.Books.Remove(book);
                db.SaveChanges();
                return(true);
            }
        }
コード例 #21
0
        public bool Delete(int bookId, int buyerId)
        {
            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                Cart cart = db.Carts.SingleOrDefault(c => c.BookId == bookId && c.BuyerId == buyerId);

                if (cart == null)                   //no entity found for given ids
                {
                    return(false);
                }

                db.Carts.Remove(cart);
                db.SaveChanges();
                return(true);
            }
        }
コード例 #22
0
        public bool Delete(int reviewId)
        {
            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                Review review = db.Reviews.SingleOrDefault(r => r.Id == reviewId);

                if (review == null)                 //no entity found by the given id
                {
                    return(false);
                }

                db.Reviews.Remove(review);
                db.SaveChanges();
                return(true);
            }
        }
コード例 #23
0
        public bool Delete(int distributorId)
        {
            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                Distributor distributor = db.Distributors.SingleOrDefault(d => d.Id == distributorId);

                if (distributor == null)                    //no entity found for given id
                {
                    return(false);
                }

                db.Distributors.Remove(distributor);
                db.SaveChanges();
                return(true);
            }
        }
コード例 #24
0
        public IEnumerable <Book> FindByKeyword(string keyword, int pageNum, int pageSize)
        {
            if (keyword == null)                    //cannot search for null keyword
            {
                return(null);
            }

            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                keyword = keyword.Trim().ToLower();
                return(db.Books
                       .Where(b =>
                              b.Name.Trim().ToLower().Contains(keyword) ||
                              b.Author.Trim().ToLower().Contains(keyword) ||
                              b.Publisher.Trim().ToLower().Contains(keyword)
                              )
                       .Skip((pageNum - 1) * pageSize)
                       .Take(pageSize));
            }
        }
コード例 #25
0
        public bool Add(Review review)
        {
            if (review == null)             //cannot insert null entity into db
            {
                return(false);
            }

            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                try
                {
                    db.Reviews.Add(review);
                    db.SaveChanges();
                    return(true);
                }
                catch (DbEntityValidationException)                 //entity doesn't have valid properties
                {
                    return(false);
                }
            }
        }
コード例 #26
0
        public bool Add(Book book)
        {
            if (book == null)               //cannot insert null entity
            {
                return(false);
            }

            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                db.Books.Add(book);
                try
                {
                    db.SaveChanges();
                    return(true);
                }
                catch (DbEntityValidationException)                 //entity doesn't have valid properties
                {
                    return(false);
                }
            }
        }
コード例 #27
0
        public bool Update(Review review)
        {
            if (review == null)             //cannot update with null entity
            {
                return(false);
            }

            using (EfDatabaseContext db = new EfDatabaseContext())
            {
                Review trackedReview = db.Reviews.SingleOrDefault(r => r.Id == review.Id);

                if (trackedReview == null)                  //no entity found by the given id
                {
                    return(false);
                }

                if (review.BookId > 0)
                {
                    trackedReview.BookId = review.BookId;
                }
                if (review.BuyerId > 0)
                {
                    trackedReview.BuyerId = review.BuyerId;
                }
                if (review.Rating != trackedReview.Rating)
                {
                    trackedReview.Rating = review.Rating;
                }
                if (review.Comment != trackedReview.Comment)
                {
                    trackedReview.Comment = review.Comment;
                }

                db.SaveChanges();
                return(true);
            }
        }