]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Repos/BookRepo.php
Perms: Fixed some issues made when adding transactions
[bookstack] / app / Entities / Repos / BookRepo.php
index 27d0b407541d02857d8aec46cadbeb71cf7bc840..6d28d5d6aabe1796a0f219d641dc5abf816badd3 100644 (file)
-<?php namespace BookStack\Entities\Repos;
+<?php
 
-use BookStack\Actions\ActivityType;
-use BookStack\Actions\TagRepo;
+namespace BookStack\Entities\Repos;
+
+use BookStack\Activity\ActivityType;
+use BookStack\Activity\TagRepo;
 use BookStack\Entities\Models\Book;
 use BookStack\Entities\Tools\TrashCan;
 use BookStack\Exceptions\ImageUploadException;
-use BookStack\Exceptions\NotFoundException;
 use BookStack\Facades\Activity;
+use BookStack\Sorting\SortRule;
 use BookStack\Uploads\ImageRepo;
+use BookStack\Util\DatabaseTransaction;
 use Exception;
-use Illuminate\Contracts\Pagination\LengthAwarePaginator;
 use Illuminate\Http\UploadedFile;
-use Illuminate\Support\Collection;
 
 class BookRepo
 {
-
-    protected $baseRepo;
-    protected $tagRepo;
-    protected $imageRepo;
-
-    /**
-     * BookRepo constructor.
-     */
-    public function __construct(BaseRepo $baseRepo, TagRepo $tagRepo, ImageRepo $imageRepo)
-    {
-        $this->baseRepo = $baseRepo;
-        $this->tagRepo = $tagRepo;
-        $this->imageRepo = $imageRepo;
-    }
-
-    /**
-     * Get all books in a paginated format.
-     */
-    public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator
-    {
-        return Book::visible()->with('cover')->orderBy($sort, $order)->paginate($count);
-    }
-
-    /**
-     * Get the books that were most recently viewed by this user.
-     */
-    public function getRecentlyViewed(int $count = 20): Collection
-    {
-        return Book::visible()->withLastView()
-            ->having('last_viewed_at', '>', 0)
-            ->orderBy('last_viewed_at', 'desc')
-            ->take($count)->get();
-    }
-
-    /**
-     * Get the most popular books in the system.
-     */
-    public function getPopular(int $count = 20): Collection
-    {
-        return Book::visible()->withViewCount()
-            ->having('view_count', '>', 0)
-            ->orderBy('view_count', 'desc')
-            ->take($count)->get();
+    public function __construct(
+        protected BaseRepo $baseRepo,
+        protected TagRepo $tagRepo,
+        protected ImageRepo $imageRepo,
+        protected TrashCan $trashCan,
+    ) {
     }
 
     /**
-     * Get the most recently created books from the system.
+     * Create a new book in the system.
      */
-    public function getRecentlyCreated(int $count = 20): Collection
+    public function create(array $input): Book
     {
-        return Book::visible()->orderBy('created_at', 'desc')
-            ->take($count)->get();
+        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();
     }
 
     /**
-     * Get a book by its slug.
+     * Update the given book.
      */
-    public function getBySlug(string $slug): Book
+    public function update(Book $book, array $input): Book
     {
-        $book = Book::visible()->where('slug', '=', $slug)->first();
+        $this->baseRepo->update($book, $input);
 
-        if ($book === null) {
-            throw new NotFoundException(trans('errors.book_not_found'));
+        if (array_key_exists('default_template_id', $input)) {
+            $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id']));
         }
 
-        return $book;
-    }
+        if (array_key_exists('image', $input)) {
+            $this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null);
+        }
 
-    /**
-     * Create a new book in the system
-     */
-    public function create(array $input): Book
-    {
-        $book = new Book();
-        $this->baseRepo->create($book, $input);
-        Activity::addForEntity($book, ActivityType::BOOK_CREATE);
-        return $book;
-    }
+        Activity::add(ActivityType::BOOK_UPDATE, $book);
 
-    /**
-     * Update the given book.
-     */
-    public function update(Book $book, array $input): Book
-    {
-        $this->baseRepo->update($book, $input);
-        Activity::addForEntity($book, ActivityType::BOOK_UPDATE);
         return $book;
     }
 
     /**
      * Update the given book's cover image, or clear it.
+     *
      * @throws ImageUploadException
      * @throws Exception
      */
@@ -116,14 +80,14 @@ class BookRepo
 
     /**
      * Remove a book from the system.
+     *
      * @throws Exception
      */
     public function destroy(Book $book)
     {
-        $trashCan = new TrashCan();
-        $trashCan->softDestroyBook($book);
-        Activity::addForEntity($book, ActivityType::BOOK_DELETE);
+        $this->trashCan->softDestroyBook($book);
+        Activity::add(ActivityType::BOOK_DELETE, $book);
 
-        $trashCan->autoClearOld();
+        $this->trashCan->autoClearOld();
     }
 }