X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/60d0f96cd754a85c637a5f34dcc0341f4ef72a46..refs/pull/2515/head:/app/Entities/Repos/BookRepo.php diff --git a/app/Entities/Repos/BookRepo.php b/app/Entities/Repos/BookRepo.php index c82780fea..27d0b4075 100644 --- a/app/Entities/Repos/BookRepo.php +++ b/app/Entities/Repos/BookRepo.php @@ -1,63 +1,129 @@ -baseRepo = $baseRepo; + $this->tagRepo = $tagRepo; + $this->imageRepo = $imageRepo; + } -class BookRepo extends EntityRepo -{ + /** + * Get all books in a paginated format. + */ + public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator + { + return Book::visible()->with('cover')->orderBy($sort, $order)->paginate($count); + } /** - * Fetch a book by its slug. - * @param string $slug - * @return Book - * @throws NotFoundException + * Get the books that were most recently viewed by this user. */ - public function getBySlug(string $slug): Book + public function getRecentlyViewed(int $count = 20): Collection { - /** @var Book $book */ - $book = $this->getEntityBySlug('book', $slug); - return $book; + return Book::visible()->withLastView() + ->having('last_viewed_at', '>', 0) + ->orderBy('last_viewed_at', 'desc') + ->take($count)->get(); } /** - * Append a Book to a BookShelf. - * @param Bookshelf $shelf - * @param Book $book + * Get the most popular books in the system. */ - public function appendBookToShelf(Bookshelf $shelf, Book $book) + public function getPopular(int $count = 20): Collection { - if ($shelf->contains($book)) { - return; - } + return Book::visible()->withViewCount() + ->having('view_count', '>', 0) + ->orderBy('view_count', 'desc') + ->take($count)->get(); + } - $maxOrder = $shelf->books()->max('order'); - $shelf->books()->attach($book->id, ['order' => $maxOrder + 1]); + /** + * Get the most recently created books from the system. + */ + public function getRecentlyCreated(int $count = 20): Collection + { + return Book::visible()->orderBy('created_at', 'desc') + ->take($count)->get(); } /** - * Destroy the provided book and all its child entities. - * @param Book $book - * @throws NotifyException - * @throws \Throwable + * Get a book by its slug. */ - public function destroyBook(Book $book) + public function getBySlug(string $slug): Book { - foreach ($book->pages as $page) { - $this->destroyPage($page); - } + $book = Book::visible()->where('slug', '=', $slug)->first(); - foreach ($book->chapters as $chapter) { - $this->destroyChapter($chapter); + if ($book === null) { + throw new NotFoundException(trans('errors.book_not_found')); } - $this->destroyEntityCommonRelations($book); - $book->delete(); + return $book; + } + + /** + * Create a new book in the system + */ + public function create(array $input): Book + { + $book = new Book(); + $this->baseRepo->create($book, $input); + Activity::addForEntity($book, ActivityType::BOOK_CREATE); + return $book; + } + + /** + * Update the given book. + */ + public function update(Book $book, array $input): Book + { + $this->baseRepo->update($book, $input); + Activity::addForEntity($book, ActivityType::BOOK_UPDATE); + return $book; + } + + /** + * Update the given book's cover image, or clear it. + * @throws ImageUploadException + * @throws Exception + */ + public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false) + { + $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage); } -} \ No newline at end of file + /** + * Remove a book from the system. + * @throws Exception + */ + public function destroy(Book $book) + { + $trashCan = new TrashCan(); + $trashCan->softDestroyBook($book); + Activity::addForEntity($book, ActivityType::BOOK_DELETE); + + $trashCan->autoClearOld(); + } +}