Skip to content

Commit c1e050a

Browse files
author
Lupacescu Eduard
authored
Merge pull request #65 from binaryk/field
Field and JSON:API support
2 parents b6dbf6c + eafffc5 commit c1e050a

38 files changed

+1599
-153
lines changed

routes/api.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
<?php
22

33
Route::get('/{repository}', 'RepositoryIndexController@handle');
4+
Route::post('/{repository}', 'RepositoryStoreController@handle');
5+
Route::get('/{repository}/{repositoryId}', 'RepositoryShowController@handle');
6+
Route::patch('/{repository}/{repositoryId}', 'RepositoryUpdateController@handle');
7+
Route::delete('/{repository}/{repositoryId}', 'RepositoryDestroyController@handle');

src/Commands/CheckPassport.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ public function handle()
6262
{
6363
$userClass = $this->config->get('auth.providers.users.model');
6464

65-
if (false === $this->provider()) {
65+
if (false === $this->hasProvider()) {
6666
return;
6767
}
6868

69-
if (false === $this->passportable($userClass)) {
69+
if (false === $this->isPassportable($userClass)) {
7070
return;
7171
}
7272

73-
if (false === $this->passportClient()) {
73+
if (false === $this->hasPassportClient()) {
7474
return;
7575
}
7676

@@ -87,7 +87,7 @@ public function handle()
8787
* @param $userClass
8888
* @return bool
8989
*/
90-
public function passportable($userClass = null): bool
90+
public function isPassportable($userClass = null): bool
9191
{
9292
try {
9393
$userInstance = $this->container->get($userClass);
@@ -116,7 +116,7 @@ public function passportable($userClass = null): bool
116116
/**
117117
* @return bool
118118
*/
119-
public function provider(): bool
119+
public function hasProvider(): bool
120120
{
121121
$provider = $this->app->getProviders('Laravel\\Passport\\PassportServiceProvider');
122122

@@ -132,7 +132,7 @@ public function provider(): bool
132132
/**
133133
* @return bool
134134
*/
135-
public function passportClient(): bool
135+
public function hasPassportClient(): bool
136136
{
137137
try {
138138
/**

src/Commands/stubs/RestifyServiceProvider.stub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace App\Providers;
44

55
use Illuminate\Support\Facades\Gate;
66
use Binaryk\LaravelRestify\Restify;
7+
use Illuminate\Http\Resources\Json\Resource;
78
use Binaryk\LaravelRestify\RestifyApplicationServiceProvider;
89

910
class RestifyServiceProvider extends RestifyApplicationServiceProvider
@@ -16,6 +17,8 @@ class RestifyServiceProvider extends RestifyApplicationServiceProvider
1617
public function boot()
1718
{
1819
parent::boot();
20+
21+
Resource::withoutWrapping();
1922
}
2023

2124
/**

src/Commands/stubs/repository.stub

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace DummyNamespace;
44

5-
use Illuminate\Http\Request;
5+
use Binaryk\LaravelRestify\Fields\Field;
6+
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
67
use Binaryk\LaravelRestify\Repositories\Repository;
8+
use Illuminate\Contracts\Pagination\Paginator;
79

810
class DummyClass extends Repository
911
{
@@ -13,4 +15,67 @@ class DummyClass extends Repository
1315
* @var string
1416
*/
1517
public static $model = 'DummyFullModel';
18+
19+
/**
20+
* @param RestifyRequest $request
21+
* @return array
22+
*/
23+
public function fields(RestifyRequest $request)
24+
{
25+
return [
26+
// Field::make('title')->storingRules('required')->messages([
27+
// 'required' => 'This field is required bro.',
28+
// ]),
29+
];
30+
31+
}
32+
33+
/**
34+
* @param RestifyRequest $request
35+
* @param Paginator $paginated
36+
* @return \Illuminate\Http\JsonResponse
37+
*/
38+
public function index(RestifyRequest $request, Paginator $paginated)
39+
{
40+
return parent::index($request, $paginated);
41+
}
42+
43+
/**
44+
* @param RestifyRequest $request
45+
* @return \Illuminate\Http\JsonResponse
46+
* @throws \Illuminate\Auth\Access\AuthorizationException
47+
* @throws \Throwable
48+
*/
49+
public function show(RestifyRequest $request)
50+
{
51+
return parent::show($request);
52+
}
53+
54+
/**
55+
* @param RestifyRequest $request
56+
* @return \Illuminate\Http\JsonResponse
57+
*/
58+
public function store(RestifyRequest $request)
59+
{
60+
return parent::store($request);
61+
}
62+
63+
/**
64+
* @param RestifyRequest $request
65+
* @param $model
66+
* @return \Illuminate\Http\JsonResponse|void
67+
*/
68+
public function update(RestifyRequest $request, $model)
69+
{
70+
return parent::update($request, $model);
71+
}
72+
73+
/**
74+
* @param RestifyRequest $request
75+
* @return \Illuminate\Http\JsonResponse
76+
*/
77+
public function destroy(RestifyRequest $request)
78+
{
79+
return parent::destroy($request);
80+
}
1681
}

src/Contracts/RestifySearchable.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Binaryk\LaravelRestify\Contracts;
44

5-
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
6-
75
/**
86
* @author Eduard Lupacescu <[email protected]>
97
*/
@@ -15,13 +13,6 @@ interface RestifySearchable
1513
const MATCH_BOOL = 'bool';
1614
const MATCH_INTEGER = 'integer';
1715

18-
/**
19-
* @param RestifyRequest $request
20-
* @param array $fields
21-
* @return array
22-
*/
23-
public function serializeForIndex(RestifyRequest $request, array $fields = []);
24-
2516
/**
2617
* @return array
2718
*/

src/Controllers/RestController.php

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
1010
use Binaryk\LaravelRestify\Repositories\Repository;
1111
use Binaryk\LaravelRestify\Services\Search\SearchService;
12-
use Binaryk\LaravelRestify\Traits\PerformsQueries;
1312
use Illuminate\Config\Repository as Config;
1413
use Illuminate\Container\Container;
1514
use Illuminate\Contracts\Auth\Access\Gate;
@@ -22,7 +21,9 @@
2221
use Illuminate\Foundation\Validation\ValidatesRequests;
2322
use Illuminate\Http\JsonResponse;
2423
use Illuminate\Routing\Controller as BaseController;
24+
use Illuminate\Support\Arr;
2525
use Illuminate\Support\Facades\Password;
26+
use Throwable;
2627

2728
/**
2829
* Abstract Class RestController.
@@ -35,7 +36,7 @@
3536
*/
3637
abstract class RestController extends BaseController
3738
{
38-
use AuthorizesRequests, DispatchesJobs, ValidatesRequests, PerformsQueries;
39+
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
3940

4041
/**
4142
* @var RestResponse
@@ -132,30 +133,46 @@ protected function response($data = null, $status = 200, array $headers = [])
132133
* @return array
133134
* @throws BindingResolutionException
134135
* @throws InstanceOfException
136+
* @throws Throwable
135137
*/
136138
public function search($modelClass, $filters = [])
137139
{
138-
$results = SearchService::instance()
139-
->setPredefinedFilters($filters)
140-
->search($this->request(), $modelClass instanceof Repository ? $modelClass->model() : new $modelClass);
140+
$paginator = $this->paginator($modelClass, $filters);
141141

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));
150142
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());
152144
} else {
153-
$items = $paginator->getCollection()->map->serializeForIndex($this->request());
145+
$items = $paginator->getCollection();
154146
}
155147

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']),
157151
'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));
159176
}
160177

161178
/**

0 commit comments

Comments
 (0)