Skip to content

Commit 5ceef21

Browse files
authored
restifyjs (#344)
* RestifyJS setup. * Apply fixes from StyleCI (#345) * wip * Apply fixes from StyleCI (#346) * Return actions. * Apply fixes from StyleCI (#347) * wip * wip * wip * wip * merge
1 parent ea1763f commit 5ceef21

File tree

6 files changed

+112
-2
lines changed

6 files changed

+112
-2
lines changed

routes/api.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Binaryk\LaravelRestify\Http\Controllers\RepositoryStoreController;
2020
use Binaryk\LaravelRestify\Http\Controllers\RepositoryUpdateBulkController;
2121
use Binaryk\LaravelRestify\Http\Controllers\RepositoryUpdateController;
22+
use Binaryk\LaravelRestify\Http\Controllers\RestifyJsSetupController;
2223
use Illuminate\Support\Facades\Route;
2324

2425
// Global Search...
@@ -29,6 +30,9 @@
2930
Route::post('/profile', '\\'.ProfileUpdateController::class);
3031
Route::post('/profile/avatar', '\\'.ProfileAvatarController::class);
3132

33+
// RestifyJS
34+
Route::get('/restifyjs/setup', '\\'.RestifyJsSetupController::class);
35+
3236
// Filters
3337
Route::get('/{repository}/filters', '\\'.RepositoryFilterController::class);
3438

src/Commands/stubs/user-repository.stub

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace App\Restify;
44

55
use Binaryk\LaravelRestify\Fields\Field;
66
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
7+
use Illuminate\Http\Request;
78
use Illuminate\Support\Facades\Hash;
89

910
class UserRepository extends Repository
@@ -15,7 +16,7 @@ class UserRepository extends Repository
1516
return [
1617
Field::make('name')->rules('required'),
1718

18-
Field::make('email')->storingRules('required', ''unique:users')->messages([
19+
Field::make('email')->storingRules('required', 'unique:users')->messages([
1920
'required' => 'This field is required.',
2021
]),
2122

src/Eager/Related.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
use Binaryk\LaravelRestify\Fields\EagerField;
66
use Binaryk\LaravelRestify\Repositories\Repository;
77
use Binaryk\LaravelRestify\Traits\Make;
8+
use JsonSerializable;
89

9-
class Related
10+
class Related implements JsonSerializable
1011
{
1112
use Make;
1213

@@ -34,4 +35,14 @@ public function resolveField(Repository $repository): EagerField
3435
{
3536
return $this->field->resolve($repository);
3637
}
38+
39+
public function jsonSerialize()
40+
{
41+
return [
42+
'relation' => $this->getRelation(),
43+
'field' => isset($this->field)
44+
? $this->field->jsonSerialize()
45+
: null,
46+
];
47+
}
3748
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Http\Controllers;
4+
5+
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
6+
use Binaryk\LaravelRestify\Repositories\Repository;
7+
use Binaryk\LaravelRestify\Restify;
8+
use Illuminate\Routing\Controller;
9+
use Illuminate\Support\Str;
10+
11+
class RestifyJsSetupController extends Controller
12+
{
13+
public function __invoke(RestifyRequest $request)
14+
{
15+
return response()->json([
16+
'config' => $this->config(),
17+
'repositories' => $this->repositories($request),
18+
]);
19+
}
20+
21+
private function repositories(RestifyRequest $request): array
22+
{
23+
return collect(Restify::$repositories)
24+
->map(fn (string $repository) => app($repository))
25+
->map(fn (Repository $repository) => $repository->restifyjsSerialize($request))
26+
->all();
27+
}
28+
29+
private function config(): array
30+
{
31+
return [
32+
'domain' => $this->deleteFirstAndLastSlash(config('app.url')),
33+
'base' => $this->deleteFirstAndLastSlash(Restify::path()),
34+
];
35+
}
36+
37+
private function deleteFirstAndLastSlash(string $domain): string
38+
{
39+
if (Str::startsWith($domain, '/')) {
40+
$domain = Str::replaceFirst('/', '', $domain);
41+
}
42+
43+
if (Str::endsWith($domain, '/')) {
44+
$domain = Str::replaceLast('/', '', $domain);
45+
}
46+
47+
return $domain;
48+
}
49+
}

src/Repositories/Repository.php

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

33
namespace Binaryk\LaravelRestify\Repositories;
44

5+
use Binaryk\LaravelRestify\Actions\Action;
56
use Binaryk\LaravelRestify\Contracts\RestifySearchable;
67
use Binaryk\LaravelRestify\Controllers\RestResponse;
78
use Binaryk\LaravelRestify\Eager\Related;
@@ -1074,4 +1075,18 @@ public function isEagerState(): bool
10741075
{
10751076
return $this->eagerState === true;
10761077
}
1078+
1079+
public function restifyjsSerialize(RestifyRequest $request): array
1080+
{
1081+
return [
1082+
'uriKey' => static::uriKey(),
1083+
'related' => static::collectFilters('matches'),
1084+
'sort' => static::collectFilters('sortables'),
1085+
'match' => static::collectFilters('matches'),
1086+
'searchables' => static::collectFilters('searchables'),
1087+
'actions' => $this->resolveActions($request)->filter(fn (Action $action) => $action->isShownOnIndex(
1088+
$request, $this
1089+
))->values(),
1090+
];
1091+
}
10771092
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Tests\Controllers;
4+
5+
use Binaryk\LaravelRestify\Tests\IntegrationTest;
6+
7+
class RestifyJsSetupControllerTest extends IntegrationTest
8+
{
9+
public function test_returns_configurations()
10+
{
11+
$this->getJson('restifyjs/setup')
12+
->assertJsonStructure([
13+
'config' => [
14+
'domain',
15+
'base',
16+
],
17+
'repositories' => [
18+
[
19+
'uriKey',
20+
'related',
21+
'sort',
22+
'match',
23+
'searchables',
24+
'actions',
25+
],
26+
],
27+
])
28+
->assertOk();
29+
}
30+
}

0 commit comments

Comments
 (0)