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\Uploads\ImageRepo;
13 use Illuminate\Http\UploadedFile;
17 public function __construct(
18 protected BaseRepo $baseRepo,
19 protected TagRepo $tagRepo,
20 protected ImageRepo $imageRepo
25 * Create a new book in the system.
27 public function create(array $input): Book
30 $this->baseRepo->create($book, $input);
31 $this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
32 $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null));
33 Activity::add(ActivityType::BOOK_CREATE, $book);
39 * Update the given book.
41 public function update(Book $book, array $input): Book
43 $this->baseRepo->update($book, $input);
45 if (array_key_exists('default_template_id', $input)) {
46 $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id']));
49 if (array_key_exists('image', $input)) {
50 $this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null);
53 Activity::add(ActivityType::BOOK_UPDATE, $book);
59 * Update the given book's cover image, or clear it.
61 * @throws ImageUploadException
64 public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
66 $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
70 * Remove a book from the system.
74 public function destroy(Book $book)
76 $trashCan = new TrashCan();
77 $trashCan->softDestroyBook($book);
78 Activity::add(ActivityType::BOOK_DELETE, $book);
80 $trashCan->autoClearOld();