Example #1
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);
                }
            }
        }
Example #2
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);
            }
        }
Example #3
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);
            }
        }
Example #4
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);
            }
        }
Example #5
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);
            }
        }
Example #6
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);
            }
        }
Example #7
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);
            }
        }
Example #8
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);
            }
        }
Example #9
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);
            }
        }
Example #10
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);
                }
            }
        }
Example #11
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);
                }
            }
        }
Example #12
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);
            }
        }