5
5
use Illuminate \Database \Eloquent \Model ;
6
6
use Illuminate \Http \Request ;
7
7
use Illuminate \Http \JsonResponse ;
8
- use Illuminate \ Contracts \Pagination \LengthAwarePaginator ;
8
+ use \ Illuminate \Pagination \LengthAwarePaginator ;
9
9
10
10
use App \Media ;
11
11
@@ -15,20 +15,27 @@ class MediaController extends Controller
15
15
16
16
public function __construct (Request $ request )
17
17
{
18
- // Set limit
18
+ // Set default limit
19
19
$ this ->limit = $ this ->page_limit ($ request );
20
20
}
21
21
22
22
/**
23
23
* API Resource for Media Model
24
- * @return LengthAwarePaginator|void
24
+ * @param Request $request
25
+ * @return LengthAwarePaginator
25
26
*/
26
- public function index ()
27
+ public function index (Request $ request )
27
28
{
28
- // Default model listing
29
- $ data = Media::search ('* ' )->paginate (50 );
30
- return $ data ->isNotEmpty () ? $ data : abort (404 );
29
+ // Default documents listing
30
+ $ matches = Media::matchAllSearch ()->size ($ this ->limit )->execute ();
31
+ $ docs = $ matches ->documents ();
32
+ $ res = [];
33
+ foreach ($ docs as $ media ) {
34
+ $ res [] = array_merge (['id ' => (int ) $ media ->getId ()], $ media ->getContent ());
35
+ }
36
+ return new LengthAwarePaginator ($ res , $ matches ->total (), $ this ->limit , $ request ->input ('page ' ) ?? 1 );
31
37
}
38
+
32
39
// public function index(Request $request)
33
40
// {
34
41
//
@@ -119,7 +126,6 @@ public function get($id) {
119
126
120
127
/**
121
128
* Return top models by week
122
- *
123
129
* @return LengthAwarePaginator
124
130
*/
125
131
public function best ()
@@ -135,7 +141,6 @@ public function best()
135
141
136
142
/**
137
143
* Increment likes column on
138
- *
139
144
* @param $id
140
145
* @return JsonResponse
141
146
*/
@@ -149,7 +154,6 @@ public function like($id)
149
154
150
155
/**
151
156
* Increment dislikes column
152
- *
153
157
* @param $id
154
158
* @return JsonResponse
155
159
*/
@@ -161,8 +165,10 @@ public function dislike($id)
161
165
return response ()->json (['success ' => false ], 404 );
162
166
}
163
167
164
- /*
165
- * Set page limit (respectively query limit)
168
+ /**
169
+ * Set default page limit
170
+ * @param $request
171
+ * @return int
166
172
*/
167
173
public function page_limit ($ request ) {
168
174
return ($ request ->has ('limit ' ) && $ request ->input ('limit ' ) < 50 ) ? (int ) $ request ->input ('limit ' ) : 50 ;
0 commit comments