1 <?php namespace BookStack\Entities\Repos;
3 use BookStack\Actions\TagRepo;
4 use BookStack\Entities\Book;
5 use BookStack\Entities\Managers\TrashCan;
6 use BookStack\Exceptions\ImageUploadException;
7 use BookStack\Exceptions\NotFoundException;
8 use BookStack\Exceptions\NotifyException;
9 use BookStack\Uploads\ImageRepo;
11 use Illuminate\Contracts\Container\BindingResolutionException;
12 use Illuminate\Contracts\Pagination\LengthAwarePaginator;
13 use Illuminate\Http\UploadedFile;
14 use Illuminate\Support\Collection;
24 * 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()->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);
98 * Update the given book.
100 public function update(Book $book, array $input): Book
102 $this->baseRepo->update($book, $input);
107 * Update the given book's cover image, or clear it.
108 * @throws ImageUploadException
111 public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
113 $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
117 * Update the permissions of a book.
119 public function updatePermissions(Book $book, bool $restricted, Collection $permissions = null)
121 $this->baseRepo->updatePermissions($book, $restricted, $permissions);
125 * Remove a book from the system.
126 * @throws NotifyException
127 * @throws BindingResolutionException
129 public function destroy(Book $book)
131 $trashCan = new TrashCan();
132 $trashCan->destroyBook($book);