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)); }
public IEnumerable <Book> GetBooks() { using (var context = new BooksDBContext(dbOptions)) { return(context.Books .Include(b => b.Reviews) .ToList()); } }
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)); } }
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); } }
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(); } }
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(); } }
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); } }
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; }