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;
13 use BookStack\Util\DatabaseTransaction;
15 use Illuminate\Http\UploadedFile;
19 public function __construct(
20 protected BaseRepo $baseRepo,
21 protected TagRepo $tagRepo,
22 protected ImageRepo $imageRepo,
23 protected TrashCan $trashCan,
28 * Create a new book in the system.
30 public function create(array $input): Book
32 return (new DatabaseTransaction(function () use ($input) {
35 $this->baseRepo->create($book, $input);
36 $this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
37 $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null));
38 Activity::add(ActivityType::BOOK_CREATE, $book);
40 $defaultBookSortSetting = intval(setting('sorting-book-default', '0'));
41 if ($defaultBookSortSetting && SortRule::query()->find($defaultBookSortSetting)) {
42 $book->sort_rule_id = $defaultBookSortSetting;
51 * Update the given book.
53 public function update(Book $book, array $input): Book
55 $this->baseRepo->update($book, $input);
57 if (array_key_exists('default_template_id', $input)) {
58 $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id']));
61 if (array_key_exists('image', $input)) {
62 $this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null);
65 Activity::add(ActivityType::BOOK_UPDATE, $book);
71 * Update the given book's cover image, or clear it.
73 * @throws ImageUploadException
76 public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
78 $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
82 * Remove a book from the system.
86 public function destroy(Book $book)
88 $this->trashCan->softDestroyBook($book);
89 Activity::add(ActivityType::BOOK_DELETE, $book);
91 $this->trashCan->autoClearOld();