]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BookRepo.php
Removed bad trailing comma in method
[bookstack] / app / Entities / Repos / BookRepo.php
1 <?php
2
3 namespace BookStack\Entities\Repos;
4
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;
13 use Exception;
14 use Illuminate\Contracts\Pagination\LengthAwarePaginator;
15 use Illuminate\Http\UploadedFile;
16 use Illuminate\Support\Collection;
17
18 class BookRepo
19 {
20     protected $baseRepo;
21     protected $tagRepo;
22     protected $imageRepo;
23
24     /**
25      * BookRepo constructor.
26      */
27     public function __construct(BaseRepo $baseRepo, TagRepo $tagRepo, ImageRepo $imageRepo)
28     {
29         $this->baseRepo = $baseRepo;
30         $this->tagRepo = $tagRepo;
31         $this->imageRepo = $imageRepo;
32     }
33
34     /**
35      * Get all books in a paginated format.
36      */
37     public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator
38     {
39         return Book::visible()->with('cover')->orderBy($sort, $order)->paginate($count);
40     }
41
42     /**
43      * Get the books that were most recently viewed by this user.
44      */
45     public function getRecentlyViewed(int $count = 20): Collection
46     {
47         return Book::visible()->withLastView()
48             ->having('last_viewed_at', '>', 0)
49             ->orderBy('last_viewed_at', 'desc')
50             ->take($count)->get();
51     }
52
53     /**
54      * Get the most popular books in the system.
55      */
56     public function getPopular(int $count = 20): Collection
57     {
58         return Book::visible()->withViewCount()
59             ->having('view_count', '>', 0)
60             ->orderBy('view_count', 'desc')
61             ->take($count)->get();
62     }
63
64     /**
65      * Get the most recently created books from the system.
66      */
67     public function getRecentlyCreated(int $count = 20): Collection
68     {
69         return Book::visible()->orderBy('created_at', 'desc')
70             ->take($count)->get();
71     }
72
73     /**
74      * Get a book by its slug.
75      */
76     public function getBySlug(string $slug): Book
77     {
78         $book = Book::visible()->where('slug', '=', $slug)->first();
79
80         if ($book === null) {
81             throw new NotFoundException(trans('errors.book_not_found'));
82         }
83
84         return $book;
85     }
86
87     /**
88      * Create a new book in the system.
89      */
90     public function create(array $input): Book
91     {
92         $book = new Book();
93         $this->baseRepo->create($book, $input);
94         $this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
95         Activity::add(ActivityType::BOOK_CREATE, $book);
96
97         return $book;
98     }
99
100     /**
101      * Update the given book.
102      */
103     public function update(Book $book, array $input): Book
104     {
105         $this->baseRepo->update($book, $input);
106
107         if (array_key_exists('image', $input)) {
108             $this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null);
109         }
110
111         Activity::add(ActivityType::BOOK_UPDATE, $book);
112
113         return $book;
114     }
115
116     /**
117      * Update the given book's cover image, or clear it.
118      *
119      * @throws ImageUploadException
120      * @throws Exception
121      */
122     public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
123     {
124         $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
125     }
126
127     /**
128      * Remove a book from the system.
129      *
130      * @throws Exception
131      */
132     public function destroy(Book $book)
133     {
134         $trashCan = new TrashCan();
135         $trashCan->softDestroyBook($book);
136         Activity::add(ActivityType::BOOK_DELETE, $book);
137
138         $trashCan->autoClearOld();
139     }
140 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.