]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Repos/BookRepo.php
ExportFormatter: Add book description and check for empty book and chapter descriptio...
[bookstack] / app / Entities / Repos / BookRepo.php
index 91bc9a1b41eebafb74cfe8aeaae1ff0344528d86..19d159eb1e7e9cc539a86f6031ef8004a20e70ef 100644 (file)
@@ -1,46 +1,82 @@
 <?php
 
-
 namespace BookStack\Entities\Repos;
 
-use BookStack\Entities\Book;
-use BookStack\Exceptions\NotFoundException;
-use BookStack\Exceptions\NotifyException;
+use BookStack\Activity\ActivityType;
+use BookStack\Activity\TagRepo;
+use BookStack\Entities\Models\Book;
+use BookStack\Entities\Tools\TrashCan;
+use BookStack\Exceptions\ImageUploadException;
+use BookStack\Facades\Activity;
+use BookStack\Uploads\ImageRepo;
+use Exception;
+use Illuminate\Http\UploadedFile;
 
-class BookRepo extends EntityRepo
+class BookRepo
 {
+    public function __construct(
+        protected BaseRepo $baseRepo,
+        protected TagRepo $tagRepo,
+        protected ImageRepo $imageRepo,
+        protected TrashCan $trashCan,
+    ) {
+    }
 
     /**
-     * Fetch a book by its slug.
-     * @param string $slug
-     * @return Book
-     * @throws NotFoundException
+     * Create a new book in the system.
      */
-    public function getBySlug(string $slug): Book
+    public function create(array $input): Book
     {
-        /** @var Book $book */
-        $book = $this->getEntityBySlug('book', $slug);
+        $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);
+
         return $book;
     }
 
     /**
-     * Destroy the provided book and all its child entities.
-     * @param Book $book
-     * @throws NotifyException
-     * @throws \Throwable
+     * Update the given book.
      */
-    public function destroyBook(Book $book)
+    public function update(Book $book, array $input): Book
     {
-        foreach ($book->pages as $page) {
-            $this->destroyPage($page);
+        $this->baseRepo->update($book, $input);
+
+        if (array_key_exists('default_template_id', $input)) {
+            $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id']));
         }
 
-        foreach ($book->chapters as $chapter) {
-            $this->destroyChapter($chapter);
+        if (array_key_exists('image', $input)) {
+            $this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null);
         }
 
-        $this->destroyEntityCommonRelations($book);
-        $book->delete();
+        Activity::add(ActivityType::BOOK_UPDATE, $book);
+
+        return $book;
+    }
+
+    /**
+     * Update the given book's cover image, or clear it.
+     *
+     * @throws ImageUploadException
+     * @throws Exception
+     */
+    public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
+    {
+        $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
     }
 
-}
\ No newline at end of file
+    /**
+     * Remove a book from the system.
+     *
+     * @throws Exception
+     */
+    public function destroy(Book $book)
+    {
+        $this->trashCan->softDestroyBook($book);
+        Activity::add(ActivityType::BOOK_DELETE, $book);
+
+        $this->trashCan->autoClearOld();
+    }
+}