-
app/Entities/Repos/BookRepo.php
Open in GitHubclass BookRepo { // public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator { return Book::visible()->with('cover')->orderBy($sort, $order)->paginate($count); } public function getRecentlyViewed(int $count = 20): Collection { return Book::visible()->withLastView() ->having('last_viewed_at', '>', 0) ->orderBy('last_viewed_at', 'desc') ->take($count)->get(); } public function getPopular(int $count = 20): Collection { return Book::visible()->withViewCount() ->having('view_count', '>', 0) ->orderBy('view_count', 'desc') ->take($count)->get(); } public function getRecentlyCreated(int $count = 20): Collection { return Book::visible()->orderBy('created_at', 'desc') ->take($count)->get(); } // }
-
app/Http/Controllers/BookController.php
Open in GitHubuse BookStack\Entities\Repos\BookRepo; use BookStack\Entities\Tools\ShelfContext; class BookController extends Controller { protected $bookRepo; protected $entityContextManager; public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo) { $this->bookRepo = $bookRepo; $this->entityContextManager = $entityContextManager; } public function index() { $view = setting()->getForCurrentUser('books_view_type'); $sort = setting()->getForCurrentUser('books_sort', 'name'); $order = setting()->getForCurrentUser('books_sort_order', 'asc'); $books = $this->bookRepo->getAllPaginated(18, $sort, $order); $recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed(4) : false; $popular = $this->bookRepo->getPopular(4); $new = $this->bookRepo->getRecentlyCreated(4); $this->entityContextManager->clearShelfContext(); $this->setPageTitle(trans('entities.books')); return view('books.index', [ 'books' => $books, 'recents' => $recents, 'popular' => $popular, 'new' => $new, 'view' => $view, 'sort' => $sort, 'order' => $order, ]); } // }