]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BookRepo.php
Added testing for our request method overrides
[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         Activity::add(ActivityType::BOOK_CREATE, $book);
95
96         return $book;
97     }
98
99     /**
100      * Update the given book.
101      */
102     public function update(Book $book, array $input): Book
103     {
104         $this->baseRepo->update($book, $input);
105         Activity::add(ActivityType::BOOK_UPDATE, $book);
106
107         return $book;
108     }
109
110     /**
111      * Update the given book's cover image, or clear it.
112      *
113      * @throws ImageUploadException
114      * @throws Exception
115      */
116     public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
117     {
118         $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
119     }
120
121     /**
122      * Remove a book from the system.
123      *
124      * @throws Exception
125      */
126     public function destroy(Book $book)
127     {
128         $trashCan = new TrashCan();
129         $trashCan->softDestroyBook($book);
130         Activity::add(ActivityType::BOOK_DELETE, $book);
131
132         $trashCan->autoClearOld();
133     }
134 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.