3 namespace BookStack\Entities\Repos;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Entities\Models\Bookshelf;
7 use BookStack\Entities\Queries\BookQueries;
8 use BookStack\Entities\Tools\TrashCan;
9 use BookStack\Facades\Activity;
10 use BookStack\Util\DatabaseTransaction;
15 public function __construct(
16 protected BaseRepo $baseRepo,
17 protected BookQueries $bookQueries,
18 protected TrashCan $trashCan,
23 * Create a new shelf in the system.
25 public function create(array $input, array $bookIds): Bookshelf
27 return (new DatabaseTransaction(function () use ($input, $bookIds) {
28 $shelf = new Bookshelf();
29 $this->baseRepo->create($shelf, $input);
30 $this->baseRepo->updateCoverImage($shelf, $input['image'] ?? null);
31 $this->updateBooks($shelf, $bookIds);
32 Activity::add(ActivityType::BOOKSHELF_CREATE, $shelf);
38 * Update an existing shelf in the system using the given input.
40 public function update(Bookshelf $shelf, array $input, ?array $bookIds): Bookshelf
42 $this->baseRepo->update($shelf, $input);
44 if (!is_null($bookIds)) {
45 $this->updateBooks($shelf, $bookIds);
48 if (array_key_exists('image', $input)) {
49 $this->baseRepo->updateCoverImage($shelf, $input['image'], $input['image'] === null);
52 Activity::add(ActivityType::BOOKSHELF_UPDATE, $shelf);
58 * Update which books are assigned to this shelf by syncing the given book ids.
59 * Function ensures the books are visible to the current user and existing.
61 protected function updateBooks(Bookshelf $shelf, array $bookIds)
63 $numericIDs = collect($bookIds)->map(function ($id) {
67 $syncData = $this->bookQueries->visibleForList()
68 ->whereIn('id', $bookIds)
70 ->mapWithKeys(function ($bookId) use ($numericIDs) {
71 return [$bookId => ['order' => $numericIDs->search($bookId)]];
74 $shelf->books()->sync($syncData);
78 * Remove a bookshelf from the system.
82 public function destroy(Bookshelf $shelf)
84 $this->trashCan->softDestroyShelf($shelf);
85 Activity::add(ActivityType::BOOKSHELF_DELETE, $shelf);
86 $this->trashCan->autoClearOld();