Skip to content

Commit 54e31d5

Browse files
authored
Merge pull request #21 from Lomkit/features/resource-get
Feature - resource get
2 parents b62225b + d9b3d0d commit 54e31d5

File tree

15 files changed

+279
-45
lines changed

15 files changed

+279
-45
lines changed

src/Actions/Action.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Action implements \JsonSerializable
5757
*/
5858
public function name()
5959
{
60-
return $this->name ?: Str::of(class_basename(get_class($this)))->beforeLast('Action')->snake(' ')->title();
60+
return $this->name ?: Str::of(class_basename(get_class($this)))->beforeLast('Action')->snake(' ')->title()->toString();
6161
}
6262

6363
/**

src/Concerns/PerformsRestOperations.php

+11-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Lomkit\Rest\Contracts\QueryBuilder;
1111
use Lomkit\Rest\Http\Requests\ActionsRequest;
1212
use Lomkit\Rest\Http\Requests\DestroyRequest;
13+
use Lomkit\Rest\Http\Requests\DetailRequest;
1314
use Lomkit\Rest\Http\Requests\ForceDestroyRequest;
1415
use Lomkit\Rest\Http\Requests\OperateRequest;
1516
use Lomkit\Rest\Http\Requests\RestoreRequest;
@@ -19,6 +20,16 @@
1920

2021
trait PerformsRestOperations
2122
{
23+
public function detail(DetailRequest $request) {
24+
$request->resource($resource = static::newResource());
25+
26+
$resource->authorizeTo('viewAny', $resource::$model);
27+
28+
return [
29+
'data' => $resource->jsonSerialize()
30+
];
31+
}
32+
2233
public function search(SearchRequest $request) {
2334
$request->resource($resource = static::newResource());
2435

@@ -48,15 +59,6 @@ public function mutate(MutateRequest $request) {
4859
return $operations;
4960
}
5061

51-
public function actions(ActionsRequest $request) {
52-
$request->resource($resource = static::newResource());
53-
54-
return response([
55-
'data' =>
56-
collect($resource->actions($request))->each->jsonSerialize()
57-
]);
58-
}
59-
6062
public function operate(OperateRequest $request, $action) {
6163
$request->resource($resource = static::newResource());
6264

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Console\Commands;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Contracts\Console\PromptsForMissingInput;
7+
use Illuminate\Database\Migrations\MigrationCreator;
8+
use Illuminate\Support\Composer;
9+
use Illuminate\Support\Str;
10+
use Lomkit\Rest\Console\ResolvesStubPath;
11+
12+
class InstructionCommand extends GeneratorCommand implements PromptsForMissingInput
13+
{
14+
use ResolvesStubPath;
15+
16+
/**
17+
* The console command signature.
18+
*
19+
* @var string
20+
*/
21+
protected $signature = 'rest:instruction {name : The name of instruction action}
22+
{--path= : The location where the instruction file should be created}';
23+
24+
/**
25+
* The type of class being generated.
26+
*
27+
* @var string
28+
*/
29+
protected $type = 'Instruction';
30+
31+
/**
32+
* The console command description.
33+
*
34+
* @var string
35+
*/
36+
protected $description = 'Create a new instruction class';
37+
38+
public function handle()
39+
{
40+
parent::handle();
41+
}
42+
43+
/**
44+
* Build the class with the given name.
45+
*
46+
* @param string $name
47+
* @return string
48+
*/
49+
protected function buildClass($name)
50+
{
51+
return parent::buildClass($name);
52+
}
53+
54+
/**
55+
* Get the stub file for the generator.
56+
*
57+
* @return string
58+
*/
59+
protected function getStub()
60+
{
61+
return $this->resolveStubPath('/stubs/rest/instruction.stub');
62+
}
63+
64+
/**
65+
* Get the default namespace for the class.
66+
*
67+
* @param string $rootNamespace
68+
* @return string
69+
*/
70+
protected function getDefaultNamespace($rootNamespace)
71+
{
72+
return $rootNamespace.'\Rest\Instructions';
73+
}
74+
}

src/Console/stubs/action.stub

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class {{ class }} extends RestAction
2121
}
2222

2323
/**
24-
* Called in an action failed.
24+
* The action fields.
2525
*
2626
* @param \Lomkit\Rest\Http\Requests\RestRequest $request
2727
* @return array

src/Console/stubs/instruction.stub

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Lomkit\Rest\Instructions\Instruction as RestInstruction;
6+
7+
class {{ class }} extends RestInstruction
8+
{
9+
/**
10+
* Perform the action on the given models.
11+
*
12+
* @param array $fields
13+
* @param \Illuminate\Database\Eloquent\Builder $query
14+
* @return void
15+
*/
16+
public function handle(array $fields, \Illuminate\Database\Eloquent\Builder $query)
17+
{
18+
// ...
19+
}
20+
21+
/**
22+
* The instruction fields.
23+
*
24+
* @param \Lomkit\Rest\Http\Requests\RestRequest $request
25+
* @return array
26+
*/
27+
public function fields(\Lomkit\Rest\Http\Requests\RestRequest $request)
28+
{
29+
return [
30+
'id' => [
31+
'required'
32+
]
33+
];
34+
}
35+
}

src/Console/stubs/resource.stub

+9
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,13 @@ class {{ class }} extends RestResource
6767
public function actions(RestRequest $request): array {
6868
return [];
6969
}
70+
71+
/**
72+
* The instructions that should be linked
73+
* @param RestRequest $request
74+
* @return array
75+
*/
76+
public function instructions(RestRequest $request): array {
77+
return [];
78+
}
7079
}

src/Http/Requests/DetailRequest.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Http\Requests;
4+
5+
use Closure;
6+
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
7+
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Fluent;
9+
use Illuminate\Support\Str;
10+
use Illuminate\Validation\Rule;
11+
use Lomkit\Rest\Actions\Action;
12+
use Lomkit\Rest\Contracts\QueryBuilder;
13+
use Lomkit\Rest\Http\Controllers\Controller;
14+
use Lomkit\Rest\Http\Resource;
15+
use Lomkit\Rest\Relations\BelongsTo;
16+
use Lomkit\Rest\Relations\BelongsToMany;
17+
use Lomkit\Rest\Relations\HasMany;
18+
use Lomkit\Rest\Relations\HasManyThrough;
19+
use Lomkit\Rest\Relations\MorphedByMany;
20+
use Lomkit\Rest\Relations\MorphMany;
21+
use Lomkit\Rest\Relations\MorphToMany;
22+
use Lomkit\Rest\Rules\ActionField;
23+
use Lomkit\Rest\Rules\CustomRulable;
24+
use Lomkit\Rest\Rules\Includable;
25+
use Lomkit\Rest\Rules\RequiredRelation;
26+
use Symfony\Component\HttpKernel\Exception\HttpException;
27+
28+
class DetailRequest extends RestRequest
29+
{
30+
31+
}

src/Http/Resource.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Lomkit\Rest\Http\Requests\RestRequest;
1515
use Lomkit\Rest\Instructions\Instructionable;
1616

17-
class Resource
17+
class Resource implements \JsonSerializable
1818
{
1919
use PerformsQueries,
2020
PerformsModelOperations,
@@ -79,4 +79,23 @@ public function isAutomaticGatingEnabled() : bool {
7979
public function isAuthorizingEnabled() : bool {
8080
return config('rest.authorizations.enabled');
8181
}
82+
83+
public function jsonSerialize(): mixed
84+
{
85+
$request = app(RestRequest::class);
86+
87+
return [
88+
'actions' => collect($this->actions($request))->map->jsonSerialize()->toArray(),
89+
'instructions' => collect($this->instructions($request))->map->jsonSerialize()->toArray(),
90+
'fields' => $this->exposedFields($request),
91+
'limits' => $this->exposedLimits($request),
92+
'scopes' => $this->exposedScopes($request),
93+
'relations' => collect($this->relations($request))->map->jsonSerialize()->toArray(),
94+
'rules' => [
95+
'all' => $this->rules($request),
96+
'create' => $this->createRules($request),
97+
'update' => $this->updateRules($request)
98+
]
99+
];
100+
}
82101
}

src/Http/Routing/ResourceRegistrar.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ResourceRegistrar extends BaseResourceRegistrar
1414
*
1515
* @var string[]
1616
*/
17-
protected $resourceDefaults = ['search', 'mutate', 'actions', 'operate', 'destroy', 'restore', 'forceDelete'];
17+
protected $resourceDefaults = ['detail', 'search', 'mutate', 'operate', 'destroy', 'restore', 'forceDelete'];
1818

1919
/**
2020
* The verbs used in the resource URIs.
@@ -30,63 +30,63 @@ class ResourceRegistrar extends BaseResourceRegistrar
3030
];
3131

3232
/**
33-
* Add the search method for a resourceful route.
33+
* Add the detail method for a resourceful route.
3434
*
3535
* @param string $name
3636
* @param string $base
3737
* @param string $controller
3838
* @param array $options
3939
* @return \Illuminate\Routing\Route
4040
*/
41-
protected function addResourceSearch($name, $base, $controller, $options)
41+
protected function addResourceDetail($name, $base, $controller, $options)
4242
{
43-
$uri = $this->getResourceUri($name).'/'.static::$verbs['search'];
43+
$uri = $this->getResourceUri($name);
4444

4545
unset($options['missing']);
4646

47-
$action = $this->getResourceAction($name, $controller, 'search', $options);
47+
$action = $this->getResourceAction($name, $controller, 'detail', $options);
4848

49-
return $this->router->post($uri, $action);
49+
return $this->router->get($uri, $action);
5050
}
5151

5252
/**
53-
* Add the mutate method for a resourceful route.
53+
* Add the search method for a resourceful route.
5454
*
5555
* @param string $name
5656
* @param string $base
5757
* @param string $controller
5858
* @param array $options
5959
* @return \Illuminate\Routing\Route
6060
*/
61-
protected function addResourceMutate($name, $base, $controller, $options)
61+
protected function addResourceSearch($name, $base, $controller, $options)
6262
{
63-
$uri = $this->getResourceUri($name).'/'.static::$verbs['mutate'];
63+
$uri = $this->getResourceUri($name).'/'.static::$verbs['search'];
6464

6565
unset($options['missing']);
6666

67-
$action = $this->getResourceAction($name, $controller, 'mutate', $options);
67+
$action = $this->getResourceAction($name, $controller, 'search', $options);
6868

6969
return $this->router->post($uri, $action);
7070
}
7171

7272
/**
73-
* Add the actions method for a resourceful route.
73+
* Add the mutate method for a resourceful route.
7474
*
7575
* @param string $name
7676
* @param string $base
7777
* @param string $controller
7878
* @param array $options
7979
* @return \Illuminate\Routing\Route
8080
*/
81-
protected function addResourceActions($name, $base, $controller, $options)
81+
protected function addResourceMutate($name, $base, $controller, $options)
8282
{
83-
$uri = $this->getResourceUri($name).'/'.static::$verbs['actions'];
83+
$uri = $this->getResourceUri($name).'/'.static::$verbs['mutate'];
8484

8585
unset($options['missing']);
8686

87-
$action = $this->getResourceAction($name, $controller, 'actions', $options);
87+
$action = $this->getResourceAction($name, $controller, 'mutate', $options);
8888

89-
return $this->router->get($uri, $action);
89+
return $this->router->post($uri, $action);
9090
}
9191

9292
/**

src/Instructions/Instruction.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Instruction
2828
*/
2929
public function name()
3030
{
31-
return $this->name ?: Str::of(class_basename(get_class($this)))->beforeLast('Instruction')->snake(' ')->title();
31+
return $this->name ?: Str::of(class_basename(get_class($this)))->beforeLast('Instruction')->snake(' ')->title()->toString();
3232
}
3333

3434
/**

0 commit comments

Comments
 (0)