X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/60d0f96cd754a85c637a5f34dcc0341f4ef72a46..refs/pull/5721/head:/app/Entities/Repos/BookRepo.php diff --git a/app/Entities/Repos/BookRepo.php b/app/Entities/Repos/BookRepo.php index c82780fea..6d28d5d6a 100644 --- a/app/Entities/Repos/BookRepo.php +++ b/app/Entities/Repos/BookRepo.php @@ -1,63 +1,93 @@ getEntityBySlug('book', $slug); - return $book; + return (new DatabaseTransaction(function () use ($input) { + $book = new Book(); + + $this->baseRepo->create($book, $input); + $this->baseRepo->updateCoverImage($book, $input['image'] ?? null); + $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null)); + Activity::add(ActivityType::BOOK_CREATE, $book); + + $defaultBookSortSetting = intval(setting('sorting-book-default', '0')); + if ($defaultBookSortSetting && SortRule::query()->find($defaultBookSortSetting)) { + $book->sort_rule_id = $defaultBookSortSetting; + $book->save(); + } + + return $book; + }))->run(); } /** - * Append a Book to a BookShelf. - * @param Bookshelf $shelf - * @param Book $book + * Update the given book. */ - public function appendBookToShelf(Bookshelf $shelf, Book $book) + public function update(Book $book, array $input): Book { - if ($shelf->contains($book)) { - return; + $this->baseRepo->update($book, $input); + + if (array_key_exists('default_template_id', $input)) { + $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'])); + } + + if (array_key_exists('image', $input)) { + $this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null); } - $maxOrder = $shelf->books()->max('order'); - $shelf->books()->attach($book->id, ['order' => $maxOrder + 1]); + Activity::add(ActivityType::BOOK_UPDATE, $book); + + return $book; } /** - * Destroy the provided book and all its child entities. - * @param Book $book - * @throws NotifyException - * @throws \Throwable + * Update the given book's cover image, or clear it. + * + * @throws ImageUploadException + * @throws Exception */ - public function destroyBook(Book $book) + public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false) { - foreach ($book->pages as $page) { - $this->destroyPage($page); - } + $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage); + } - foreach ($book->chapters as $chapter) { - $this->destroyChapter($chapter); - } + /** + * Remove a book from the system. + * + * @throws Exception + */ + public function destroy(Book $book) + { + $this->trashCan->softDestroyBook($book); + Activity::add(ActivityType::BOOK_DELETE, $book); - $this->destroyEntityCommonRelations($book); - $book->delete(); + $this->trashCan->autoClearOld(); } - -} \ No newline at end of file +}