X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/7ebe7d4e58f4555d6a9a253f976e22af9add7dec..refs/pull/5721/head:/app/Entities/Repos/BookRepo.php diff --git a/app/Entities/Repos/BookRepo.php b/app/Entities/Repos/BookRepo.php index b46218fe0..6d28d5d6a 100644 --- a/app/Entities/Repos/BookRepo.php +++ b/app/Entities/Repos/BookRepo.php @@ -5,90 +5,46 @@ namespace BookStack\Entities\Repos; use BookStack\Activity\ActivityType; use BookStack\Activity\TagRepo; use BookStack\Entities\Models\Book; -use BookStack\Entities\Models\Page; use BookStack\Entities\Tools\TrashCan; use BookStack\Exceptions\ImageUploadException; -use BookStack\Exceptions\NotFoundException; use BookStack\Facades\Activity; +use BookStack\Sorting\SortRule; use BookStack\Uploads\ImageRepo; +use BookStack\Util\DatabaseTransaction; use Exception; -use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Http\UploadedFile; -use Illuminate\Support\Collection; class BookRepo { public function __construct( protected BaseRepo $baseRepo, protected TagRepo $tagRepo, - protected ImageRepo $imageRepo + protected ImageRepo $imageRepo, + protected TrashCan $trashCan, ) { } - /** - * 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); - } - - /** - * Get the books that were most recently viewed by this user. - */ - public function getRecentlyViewed(int $count = 20): Collection - { - return Book::visible()->withLastView() - ->having('last_viewed_at', '>', 0) - ->orderBy('last_viewed_at', 'desc') - ->take($count)->get(); - } - - /** - * Get the most popular books in the system. - */ - public function getPopular(int $count = 20): Collection - { - return Book::visible()->withViewCount() - ->having('view_count', '>', 0) - ->orderBy('view_count', 'desc') - ->take($count)->get(); - } - - /** - * 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(); - } - - /** - * Get a book by its slug. - */ - public function getBySlug(string $slug): Book - { - $book = Book::visible()->where('slug', '=', $slug)->first(); - - if ($book === null) { - throw new NotFoundException(trans('errors.book_not_found')); - } - - return $book; - } - /** * Create a new book in the system. */ public function create(array $input): Book { - $book = new Book(); - $this->baseRepo->create($book, $input); - $this->baseRepo->updateCoverImage($book, $input['image'] ?? null); - Activity::add(ActivityType::BOOK_CREATE, $book); - - return $book; + return (new DatabaseTransaction(function () use ($input) { + $book = new Book(); + + $this->baseRepo->create($book, $input); + $this->baseRepo->updateCoverImage($book, $input['image'] ?? null); + $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null)); + Activity::add(ActivityType::BOOK_CREATE, $book); + + $defaultBookSortSetting = intval(setting('sorting-book-default', '0')); + if ($defaultBookSortSetting && SortRule::query()->find($defaultBookSortSetting)) { + $book->sort_rule_id = $defaultBookSortSetting; + $book->save(); + } + + return $book; + }))->run(); } /** @@ -98,8 +54,8 @@ class BookRepo { $this->baseRepo->update($book, $input); - if (array_key_exists('default_template', $input)) { - $this->updateBookDefaultTemplate($book, intval($input['default_template'])); + if (array_key_exists('default_template_id', $input)) { + $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'])); } if (array_key_exists('image', $input)) { @@ -111,33 +67,6 @@ class BookRepo return $book; } - /** - * Update the default page template used for this book. - * Checks that, if changing, the provided value is a valid template and the user - * has visibility of the provided page template id. - */ - protected function updateBookDefaultTemplate(Book $book, int $templateId): void - { - $changing = $templateId !== intval($book->default_template); - if (!$changing) { - return; - } - - if ($templateId === 0) { - $book->default_template = null; - $book->save(); - return; - } - - $templateExists = Page::query()->visible() - ->where('template', '=', true) - ->where('id', '=', $templateId) - ->exists(); - - $book->default_template = $templateExists ? $templateId : null; - $book->save(); - } - /** * Update the given book's cover image, or clear it. * @@ -156,10 +85,9 @@ class BookRepo */ public function destroy(Book $book) { - $trashCan = new TrashCan(); - $trashCan->softDestroyBook($book); + $this->trashCan->softDestroyBook($book); Activity::add(ActivityType::BOOK_DELETE, $book); - $trashCan->autoClearOld(); + $this->trashCan->autoClearOld(); } }