3 namespace BookStack\Entities\Repos;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Actions\TagRepo;
7 use BookStack\Entities\Models\Book;
8 use BookStack\Entities\Tools\TrashCan;
9 use BookStack\Exceptions\ImageUploadException;
10 use BookStack\Exceptions\NotFoundException;
11 use BookStack\Facades\Activity;
12 use BookStack\Uploads\ImageRepo;
14 use Illuminate\Contracts\Pagination\LengthAwarePaginator;
15 use Illuminate\Http\UploadedFile;
16 use Illuminate\Support\Collection;
25 * BookRepo constructor.
27 public function __construct(BaseRepo $baseRepo, TagRepo $tagRepo, ImageRepo $imageRepo)
29 $this->baseRepo = $baseRepo;
30 $this->tagRepo = $tagRepo;
31 $this->imageRepo = $imageRepo;
35 * Get all books in a paginated format.
37 public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator
39 return Book::visible()->with('cover')->orderBy($sort, $order)->paginate($count);
43 * Get the books that were most recently viewed by this user.
45 public function getRecentlyViewed(int $count = 20): Collection
47 return Book::visible()->withLastView()
48 ->having('last_viewed_at', '>', 0)
49 ->orderBy('last_viewed_at', 'desc')
50 ->take($count)->get();
54 * Get the most popular books in the system.
56 public function getPopular(int $count = 20): Collection
58 return Book::visible()->withViewCount()
59 ->having('view_count', '>', 0)
60 ->orderBy('view_count', 'desc')
61 ->take($count)->get();
65 * Get the most recently created books from the system.
67 public function getRecentlyCreated(int $count = 20): Collection
69 return Book::visible()->orderBy('created_at', 'desc')
70 ->take($count)->get();
74 * Get a book by its slug.
76 public function getBySlug(string $slug): Book
78 $book = Book::visible()->where('slug', '=', $slug)->first();
81 throw new NotFoundException(trans('errors.book_not_found'));
88 * Create a new book in the system.
90 public function create(array $input): Book
93 $this->baseRepo->create($book, $input);
94 Activity::add(ActivityType::BOOK_CREATE, $book);
100 * Update the given book.
102 public function update(Book $book, array $input): Book
104 $this->baseRepo->update($book, $input);
105 Activity::add(ActivityType::BOOK_UPDATE, $book);
111 * Update the given book's cover image, or clear it.
113 * @throws ImageUploadException
116 public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
118 $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
122 * Remove a book from the system.
126 public function destroy(Book $book)
128 $trashCan = new TrashCan();
129 $trashCan->softDestroyBook($book);
130 Activity::add(ActivityType::BOOK_DELETE, $book);
132 $trashCan->autoClearOld();