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