mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-08-07 23:03:00 +03:00
Added Popular books list with relevant tests
This commit is contained in:
@@ -42,8 +42,9 @@ class BookController extends Controller
|
||||
public function index()
|
||||
{
|
||||
$books = $this->bookRepo->getAllPaginated(10);
|
||||
$recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(10, 0) : false;
|
||||
return view('books/index', ['books' => $books, 'recents' => $recents]);
|
||||
$recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(4, 0) : false;
|
||||
$popular = $this->bookRepo->getPopular(4, 0);
|
||||
return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -77,6 +77,11 @@ class BookRepo
|
||||
return Views::getUserRecentlyViewed($count, $page, $this->book);
|
||||
}
|
||||
|
||||
public function getPopular($count = 10, $page = 0)
|
||||
{
|
||||
return Views::getPopular($count, $page, $this->book);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a book by slug
|
||||
* @param $slug
|
||||
|
@@ -44,6 +44,29 @@ class ViewService
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the entities with the most views.
|
||||
* @param int $count
|
||||
* @param int $page
|
||||
* @param bool|false $filterModel
|
||||
*/
|
||||
public function getPopular($count = 10, $page = 0, $filterModel = false)
|
||||
{
|
||||
$skipCount = $count * $page;
|
||||
$query = $this->view->select('id', 'viewable_id', 'viewable_type', \DB::raw('SUM(views) as view_count'))
|
||||
->groupBy('viewable_id', 'viewable_type')
|
||||
->orderBy('view_count', 'desc');
|
||||
|
||||
if($filterModel) $query->where('viewable_type', '=', get_class($filterModel));
|
||||
|
||||
$views = $query->with('viewable')->skip($skipCount)->take($count)->get();
|
||||
$viewedEntities = $views->map(function ($item) {
|
||||
return $item->viewable()->getResults();
|
||||
});
|
||||
return $viewedEntities;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all recently viewed entities for the current user.
|
||||
* @param int $count
|
||||
|
Reference in New Issue
Block a user