3 namespace BookStack\Entities\Repos;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Tools\TrashCan;
9 use BookStack\Exceptions\NotFoundException;
10 use BookStack\Facades\Activity;
12 use Illuminate\Contracts\Pagination\LengthAwarePaginator;
13 use Illuminate\Support\Collection;
20 * BookshelfRepo constructor.
22 public function __construct(BaseRepo $baseRepo)
24 $this->baseRepo = $baseRepo;
28 * Get all bookshelves in a paginated format.
30 public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator
32 return Bookshelf::visible()
33 ->with(['visibleBooks', 'cover'])
34 ->orderBy($sort, $order)
39 * Get the bookshelves that were most recently viewed by this user.
41 public function getRecentlyViewed(int $count = 20): Collection
43 return Bookshelf::visible()->withLastView()
44 ->having('last_viewed_at', '>', 0)
45 ->orderBy('last_viewed_at', 'desc')
46 ->take($count)->get();
50 * Get the most popular bookshelves in the system.
52 public function getPopular(int $count = 20): Collection
54 return Bookshelf::visible()->withViewCount()
55 ->having('view_count', '>', 0)
56 ->orderBy('view_count', 'desc')
57 ->take($count)->get();
61 * Get the most recently created bookshelves from the system.
63 public function getRecentlyCreated(int $count = 20): Collection
65 return Bookshelf::visible()->orderBy('created_at', 'desc')
66 ->take($count)->get();
70 * Get a shelf by its slug.
72 public function getBySlug(string $slug): Bookshelf
74 $shelf = Bookshelf::visible()->where('slug', '=', $slug)->first();
76 if ($shelf === null) {
77 throw new NotFoundException(trans('errors.bookshelf_not_found'));
84 * Create a new shelf in the system.
86 public function create(array $input, array $bookIds): Bookshelf
88 $shelf = new Bookshelf();
89 $this->baseRepo->create($shelf, $input);
90 $this->baseRepo->updateCoverImage($shelf, $input['image'] ?? null);
91 $this->updateBooks($shelf, $bookIds);
92 Activity::add(ActivityType::BOOKSHELF_CREATE, $shelf);
98 * Update an existing shelf in the system using the given input.
100 public function update(Bookshelf $shelf, array $input, ?array $bookIds): Bookshelf
102 $this->baseRepo->update($shelf, $input);
104 if (!is_null($bookIds)) {
105 $this->updateBooks($shelf, $bookIds);
108 if (array_key_exists('image', $input)) {
109 $this->baseRepo->updateCoverImage($shelf, $input['image'], $input['image'] === null);
112 Activity::add(ActivityType::BOOKSHELF_UPDATE, $shelf);
118 * Update which books are assigned to this shelf by syncing the given book ids.
119 * Function ensures the books are visible to the current user and existing.
121 protected function updateBooks(Bookshelf $shelf, array $bookIds)
123 $numericIDs = collect($bookIds)->map(function ($id) {
127 $syncData = Book::visible()
128 ->whereIn('id', $bookIds)
130 ->mapWithKeys(function ($bookId) use ($numericIDs) {
131 return [$bookId => ['order' => $numericIDs->search($bookId)]];
134 $shelf->books()->sync($syncData);
138 * Remove a bookshelf from the system.
142 public function destroy(Bookshelf $shelf)
144 $trashCan = new TrashCan();
145 $trashCan->softDestroyShelf($shelf);
146 Activity::add(ActivityType::BOOKSHELF_DELETE, $shelf);
147 $trashCan->autoClearOld();