-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPublicWikiController.php
More file actions
77 lines (65 loc) · 2.15 KB
/
PublicWikiController.php
File metadata and controls
77 lines (65 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace App\Http\Controllers;
use App\Http\Resources\PublicWikiResource;
use App\Wiki;
use App\WikiSiteStats;
use Illuminate\Http\Request;
class PublicWikiController extends Controller {
private static $defaultParams = [
'sort' => 'sitename',
'direction' => 'asc',
];
private static $activeThresholdPageCount = 2;
/**
* Display a listing of the resource.
*/
public function index(Request $request) {
$request->validate([
'sort' => 'in:sitename,pages',
'direction' => 'in:desc,asc',
'is_featured' => 'boolean',
'is_active' => 'boolean',
'per_page' => 'numeric',
'page' => 'numeric',
]);
$params = array_merge(self::$defaultParams, $request->input());
$query = Wiki::query()->with('wikiLatestProfile');
if (array_key_exists('is_featured', $params)) {
$query = $query->where([
'is_featured' => boolval($params['is_featured']),
]);
}
if (array_key_exists('is_active', $params) && $params['is_active']) {
$query = $query->whereRelation(
'wikiSiteStats', 'pages', '>=', self::$activeThresholdPageCount
);
}
switch ($params['sort']) {
case 'sitename':
$query = $query->orderBy(
'sitename',
$params['direction']
);
break;
case 'pages':
$query = $query->orderBy(
WikiSiteStats::query()
->select('pages')
->whereColumn('wiki_site_stats.wiki_id', 'wikis.id'),
$params['direction']
);
break;
}
$perPage = null;
if (array_key_exists('per_page', $params)) {
$perPage = intval($params['per_page']);
}
return PublicWikiResource::collection($query->paginate($perPage));
}
/**
* Display the specified resource.
*/
public function show($id) {
return new PublicWikiResource(Wiki::findOrFail($id));
}
}