X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/56be10f1cd65386063ae3d10da82a81b0aefbf15..refs/pull/5721/head:/app/Entities/Repos/BookshelfRepo.php diff --git a/app/Entities/Repos/BookshelfRepo.php b/app/Entities/Repos/BookshelfRepo.php index 25fa97dae..8e60f58c4 100644 --- a/app/Entities/Repos/BookshelfRepo.php +++ b/app/Entities/Repos/BookshelfRepo.php @@ -1,106 +1,61 @@ -baseRepo = $baseRepo; - } - - /** - * Get all bookshelves in a paginated format. - */ - public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator - { - return Bookshelf::visible()->with('visibleBooks') - ->orderBy($sort, $order)->paginate($count); - } - - /** - * Get the bookshelves that were most recently viewed by this user. - */ - public function getRecentlyViewed(int $count = 20): Collection - { - return Bookshelf::visible()->withLastView() - ->having('last_viewed_at', '>', 0) - ->orderBy('last_viewed_at', 'desc') - ->take($count)->get(); + public function __construct( + protected BaseRepo $baseRepo, + protected BookQueries $bookQueries, + protected TrashCan $trashCan, + ) { } /** - * Get the most popular bookshelves in the system. + * Create a new shelf in the system. */ - public function getPopular(int $count = 20): Collection + public function create(array $input, array $bookIds): Bookshelf { - return Bookshelf::visible()->withViewCount() - ->having('view_count', '>', 0) - ->orderBy('view_count', 'desc') - ->take($count)->get(); + return (new DatabaseTransaction(function () use ($input, $bookIds) { + $shelf = new Bookshelf(); + $this->baseRepo->create($shelf, $input); + $this->baseRepo->updateCoverImage($shelf, $input['image'] ?? null); + $this->updateBooks($shelf, $bookIds); + Activity::add(ActivityType::BOOKSHELF_CREATE, $shelf); + return $shelf; + }))->run(); } /** - * Get the most recently created bookshelves from the system. + * Update an existing shelf in the system using the given input. */ - public function getRecentlyCreated(int $count = 20): Collection + public function update(Bookshelf $shelf, array $input, ?array $bookIds): Bookshelf { - return Bookshelf::visible()->orderBy('created_at', 'desc') - ->take($count)->get(); - } + $this->baseRepo->update($shelf, $input); - /** - * Get a shelf by its slug. - */ - public function getBySlug(string $slug): Bookshelf - { - $shelf = Bookshelf::visible()->where('slug', '=', $slug)->first(); + if (!is_null($bookIds)) { + $this->updateBooks($shelf, $bookIds); + } - if ($shelf === null) { - throw new NotFoundException(trans('errors.bookshelf_not_found')); + if (array_key_exists('image', $input)) { + $this->baseRepo->updateCoverImage($shelf, $input['image'], $input['image'] === null); } - return $shelf; - } + Activity::add(ActivityType::BOOKSHELF_UPDATE, $shelf); - /** - * Create a new shelf in the system. - */ - public function create(array $input, array $bookIds): Bookshelf - { - $shelf = new Bookshelf(); - $this->baseRepo->create($shelf, $input); - $this->updateBooks($shelf, $bookIds); - return $shelf; - } - - /** - * Create a new shelf in the system. - */ - public function update(Bookshelf $shelf, array $input, array $bookIds): Bookshelf - { - $this->baseRepo->update($shelf, $input); - $this->updateBooks($shelf, $bookIds); return $shelf; } /** - * Update which books are assigned to this shelf by - * syncing the given book ids. + * Update which books are assigned to this shelf by syncing the given book ids. * Function ensures the books are visible to the current user and existing. */ protected function updateBooks(Bookshelf $shelf, array $bookIds) @@ -109,65 +64,25 @@ class BookshelfRepo return intval($id); }); - $syncData = Book::visible() + $syncData = $this->bookQueries->visibleForList() ->whereIn('id', $bookIds) - ->get(['id'])->pluck('id')->mapWithKeys(function ($bookId) use ($numericIDs) { + ->pluck('id') + ->mapWithKeys(function ($bookId) use ($numericIDs) { return [$bookId => ['order' => $numericIDs->search($bookId)]]; }); $shelf->books()->sync($syncData); } - /** - * Update the given shelf cover image, or clear it. - * @throws ImageUploadException - * @throws Exception - */ - public function updateCoverImage(Bookshelf $shelf, ?UploadedFile $coverImage, bool $removeImage = false) - { - $this->baseRepo->updateCoverImage($shelf, $coverImage, $removeImage); - } - - /** - * Update the permissions of a bookshelf. - */ - public function updatePermissions(Bookshelf $shelf, bool $restricted, Collection $permissions = null) - { - $this->baseRepo->updatePermissions($shelf, $restricted, $permissions); - } - - /** - * Copy down the permissions of the given shelf to all child books. - */ - public function copyDownPermissions(Bookshelf $shelf, $checkUserPermissions = true): int - { - $shelfPermissions = $shelf->permissions()->get(['role_id', 'action'])->toArray(); - $shelfBooks = $shelf->books()->get(['id', 'restricted']); - $updatedBookCount = 0; - - /** @var Book $book */ - foreach ($shelfBooks as $book) { - if ($checkUserPermissions && !userCan('restrictions-manage', $book)) { - continue; - } - $book->permissions()->delete(); - $book->restricted = $shelf->restricted; - $book->permissions()->createMany($shelfPermissions); - $book->save(); - $book->rebuildPermissions(); - $updatedBookCount++; - } - - return $updatedBookCount; - } - /** * Remove a bookshelf from the system. + * * @throws Exception */ public function destroy(Bookshelf $shelf) { - $trashCan = new TrashCan(); - $trashCan->destroyShelf($shelf); + $this->trashCan->softDestroyShelf($shelf); + Activity::add(ActivityType::BOOKSHELF_DELETE, $shelf); + $this->trashCan->autoClearOld(); } }