Example #1
0
 public Book GetBookById(int?Id, BooksDBContext context)
 {
     if (!Id.HasValue)
     {
         return(null);
     }
     return(context.Books.Include(b => b.Reviews).FirstOrDefault(b => b.Id == Id.Value));
 }
Example #2
0
 public IEnumerable <Book> GetBooks()
 {
     using (var context = new BooksDBContext(dbOptions))
     {
         return(context.Books
                .Include(b => b.Reviews)
                .ToList());
     }
 }
Example #3
0
 public Book GetBookById(int?Id)
 {
     using (var context = new BooksDBContext(dbOptions))
     {
         if (!Id.HasValue)
         {
             return(null);
         }
         return(context.Books.Include(b => b.Reviews).FirstOrDefault(b => b.Id == Id.Value));
     }
 }
Example #4
0
 public BookReview UpdateBookReview(BookReview bookReview)
 {
     using (var context = new BooksDBContext(dbOptions))
     {
         var bookReviewDb = GetBookReviewById(bookReview.Id, context);
         bookReviewDb.Title  = bookReview.Title;
         bookReviewDb.Review = bookReview.Review;
         bookReviewDb.Rating = bookReview.Rating;
         context.SaveChanges();
         return(bookReviewDb);
     }
 }
Example #5
0
 public void DeleteBook(int Id)
 {
     using (var context = new BooksDBContext(dbOptions))
     {
         var book = context.Books.FirstOrDefault(b => b.Id == Id);
         if (book == null)
         {
             throw new DataException($"No book found with id {Id}");
         }
         context.Books.Remove(book);
         context.SaveChanges();
     }
 }
Example #6
0
 public void DeleteBookReview(int id)
 {
     using (var context = new BooksDBContext(dbOptions))
     {
         BookReview bookReview = GetBookReviewById(id);
         if (bookReview == null)
         {
             throw new DataException($"No BookReview found with id {id}");
         }
         context.BookReviews.Remove(bookReview);
         context.SaveChanges();
     }
 }
Example #7
0
 public Book UpsertBook(Book book)
 {
     using (var context = new BooksDBContext(dbOptions))
     {
         if (book.Id <= 0)
         {
             context.Books.Add(book);
         }
         else
         {
             context.Entry(book).State = EntityState.Modified;
         }
         context.SaveChanges();
         return(book);
     }
 }
Example #8
0
        public BookReview AddReviewToBook(int id, BookReview bookReview)
        {
            using (var context = new BooksDBContext(dbOptions))
            {
                if (bookReview.Id > 0 || bookReview.Book != null)
                {
                    throw new DataException("The bookreview id must be 0 for new reviews");
                }

                var book = GetBookById(id, context);
                if (book == null)
                {
                    throw new DataException($"No book found with id {id}");
                }
                book.Reviews.Add(bookReview);
                context.SaveChanges();
                return(bookReview);
            }
        }
 public BookRepository(BooksDBContext context)
 {
     this.context = context;
 }