|
9 | 9 | use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; |
10 | 10 | use Binaryk\LaravelRestify\Repositories\Repository; |
11 | 11 | use Binaryk\LaravelRestify\Services\Search\SearchService; |
12 | | -use Binaryk\LaravelRestify\Traits\PerformsQueries; |
13 | 12 | use Illuminate\Config\Repository as Config; |
14 | 13 | use Illuminate\Container\Container; |
15 | 14 | use Illuminate\Contracts\Auth\Access\Gate; |
|
22 | 21 | use Illuminate\Foundation\Validation\ValidatesRequests; |
23 | 22 | use Illuminate\Http\JsonResponse; |
24 | 23 | use Illuminate\Routing\Controller as BaseController; |
| 24 | +use Illuminate\Support\Arr; |
25 | 25 | use Illuminate\Support\Facades\Password; |
| 26 | +use Throwable; |
26 | 27 |
|
27 | 28 | /** |
28 | 29 | * Abstract Class RestController. |
|
35 | 36 | */ |
36 | 37 | abstract class RestController extends BaseController |
37 | 38 | { |
38 | | - use AuthorizesRequests, DispatchesJobs, ValidatesRequests, PerformsQueries; |
| 39 | + use AuthorizesRequests, DispatchesJobs, ValidatesRequests; |
39 | 40 |
|
40 | 41 | /** |
41 | 42 | * @var RestResponse |
@@ -132,30 +133,46 @@ protected function response($data = null, $status = 200, array $headers = []) |
132 | 133 | * @return array |
133 | 134 | * @throws BindingResolutionException |
134 | 135 | * @throws InstanceOfException |
| 136 | + * @throws Throwable |
135 | 137 | */ |
136 | 138 | public function search($modelClass, $filters = []) |
137 | 139 | { |
138 | | - $results = SearchService::instance() |
139 | | - ->setPredefinedFilters($filters) |
140 | | - ->search($this->request(), $modelClass instanceof Repository ? $modelClass->model() : new $modelClass); |
| 140 | + $paginator = $this->paginator($modelClass, $filters); |
141 | 141 |
|
142 | | - $results->tap(function ($query) { |
143 | | - static::indexQuery($this->request(), $query); |
144 | | - }); |
145 | | - |
146 | | - /** |
147 | | - * @var \Illuminate\Pagination\Paginator |
148 | | - */ |
149 | | - $paginator = $results->paginate($this->request()->get('perPage') ?? ($modelClass::$defaultPerPage ?? RestifySearchable::DEFAULT_PER_PAGE)); |
150 | 142 | if ($modelClass instanceof Repository) { |
151 | | - $items = $paginator->getCollection()->mapInto(get_class($modelClass))->map->serializeForIndex($this->request()); |
| 143 | + $items = $paginator->getCollection()->mapInto(get_class($modelClass))->map->toArray($this->request()); |
152 | 144 | } else { |
153 | | - $items = $paginator->getCollection()->map->serializeForIndex($this->request()); |
| 145 | + $items = $paginator->getCollection(); |
154 | 146 | } |
155 | 147 |
|
156 | | - return array_merge($paginator->toArray(), [ |
| 148 | + return [ |
| 149 | + 'meta' => Arr::except($paginator->toArray(), ['data', 'next_page_url', 'last_page_url', 'first_page_url', 'prev_page_url', 'path']), |
| 150 | + 'links' => Arr::only($paginator->toArray(), ['next_page_url', 'last_page_url', 'first_page_url', 'prev_page_url', 'path']), |
157 | 151 | 'data' => $items, |
158 | | - ]); |
| 152 | + ]; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * @param $modelClass |
| 157 | + * @param array $filters |
| 158 | + * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator |
| 159 | + * @throws BindingResolutionException |
| 160 | + * @throws InstanceOfException |
| 161 | + * @throws Throwable |
| 162 | + */ |
| 163 | + public function paginator($modelClass, $filters = []) |
| 164 | + { |
| 165 | + $results = SearchService::instance() |
| 166 | + ->setPredefinedFilters($filters) |
| 167 | + ->search($this->request(), $modelClass instanceof Repository ? $modelClass->model() : new $modelClass); |
| 168 | + |
| 169 | + $results->tap(function ($query) use ($modelClass) { |
| 170 | + if ($modelClass instanceof Repository) { |
| 171 | + $modelClass::indexQuery($this->request(), $query); |
| 172 | + } |
| 173 | + }); |
| 174 | + |
| 175 | + return $results->paginate($this->request()->get('perPage') ?? ($modelClass::$defaultPerPage ?? RestifySearchable::DEFAULT_PER_PAGE)); |
159 | 176 | } |
160 | 177 |
|
161 | 178 | /** |
|
0 commit comments