4 namespace BookStack\Entities\Repos;
7 use BookStack\Entities\Book;
8 use BookStack\Entities\Bookshelf;
9 use BookStack\Exceptions\NotFoundException;
10 use BookStack\Exceptions\NotifyException;
12 class BookRepo extends EntityRepo
16 * Fetch a book by its slug.
19 * @throws NotFoundException
21 public function getBySlug(string $slug): Book
23 /** @var Book $book */
24 $book = $this->getEntityBySlug('book', $slug);
29 * Append a Book to a BookShelf.
30 * @param Bookshelf $shelf
33 public function appendBookToShelf(Bookshelf $shelf, Book $book)
35 if ($shelf->contains($book)) {
39 $maxOrder = $shelf->books()->max('order');
40 $shelf->books()->attach($book->id, ['order' => $maxOrder + 1]);
44 * Destroy the provided book and all its child entities.
46 * @throws NotifyException
49 public function destroyBook(Book $book)
51 foreach ($book->pages as $page) {
52 $this->destroyPage($page);
55 foreach ($book->chapters as $chapter) {
56 $this->destroyChapter($chapter);
59 $this->destroyEntityCommonRelations($book);