3 namespace BookStack\Entities\Repos;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\TagRepo;
7 use BookStack\Entities\Models\Book;
8 use BookStack\Entities\Tools\TrashCan;
9 use BookStack\Exceptions\ImageUploadException;
10 use BookStack\Facades\Activity;
11 use BookStack\Sorting\SortRule;
12 use BookStack\Uploads\ImageRepo;
14 use Illuminate\Http\UploadedFile;
18 public function __construct(
19 protected BaseRepo $baseRepo,
20 protected TagRepo $tagRepo,
21 protected ImageRepo $imageRepo,
22 protected TrashCan $trashCan,
27 * Create a new book in the system.
29 public function create(array $input): Book
32 $this->baseRepo->create($book, $input);
33 $this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
34 $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null));
35 Activity::add(ActivityType::BOOK_CREATE, $book);
37 $defaultBookSortSetting = intval(setting('sorting-book-default', '0'));
38 if ($defaultBookSortSetting && SortRule::query()->find($defaultBookSortSetting)) {
39 $book->sort_rule_id = $defaultBookSortSetting;
47 * Update the given book.
49 public function update(Book $book, array $input): Book
51 $this->baseRepo->update($book, $input);
53 if (array_key_exists('default_template_id', $input)) {
54 $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id']));
57 if (array_key_exists('image', $input)) {
58 $this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null);
61 Activity::add(ActivityType::BOOK_UPDATE, $book);
67 * Update the given book's cover image, or clear it.
69 * @throws ImageUploadException
72 public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
74 $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
78 * Remove a book from the system.
82 public function destroy(Book $book)
84 $this->trashCan->softDestroyBook($book);
85 Activity::add(ActivityType::BOOK_DELETE, $book);
87 $this->trashCan->autoClearOld();