-
-
Notifications
You must be signed in to change notification settings - Fork 838
Expand file tree
/
Copy pathcontroller_resource.blade.php
More file actions
133 lines (116 loc) · 5.3 KB
/
controller_resource.blade.php
File metadata and controls
133 lines (116 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
@php
echo "<?php".PHP_EOL;
@endphp
namespace {{ $config->namespaces->apiController }};
use {{ $config->namespaces->apiRequest }}\Create{{ $config->modelNames->name }}APIRequest;
use {{ $config->namespaces->apiRequest }}\Update{{ $config->modelNames->name }}APIRequest;
use {{ $config->namespaces->model }}\{{ $config->modelNames->name }};
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use {{ $config->namespaces->app }}\Http\Controllers\AppBaseController;
use {{ $config->namespaces->apiResource }}\{{ $config->modelNames->name }}Resource;
{!! $docController !!}
class {{ $config->modelNames->name }}APIController extends AppBaseController
{
{!! $docIndex !!}
public function index(Request $request): JsonResponse
{
$query = {{ $config->modelNames->name }}::query();
if ($request->get('skip')) {
$query->skip($request->get('skip'));
}
if ($request->get('limit')) {
$query->limit($request->get('limit'));
}
${{ $config->modelNames->camelPlural }} = $query->get();
@if($config->options->localized)
return $this->sendResponse(
{{ $config->modelNames->name }}Resource::collection(${{ $config->modelNames->camelPlural }}),
__('messages.retrieved', ['model' => __('models/{{ $config->modelNames->camelPlural }}.plural')])
);
@else
return $this->sendResponse({{ $config->modelNames->name }}Resource::collection(${{ $config->modelNames->camelPlural }}), '{{ $config->modelNames->humanPlural }} retrieved successfully');
@endif
}
{!! $docStore !!}
public function store(Create{{ $config->modelNames->name }}APIRequest $request): JsonResponse
{
$input = $request->all();
/** @var {{ $config->modelNames->name }} ${{ $config->modelNames->camel }} */
${{ $config->modelNames->camel }} = {{ $config->modelNames->name }}::create($input);
@if($config->options->localized)
return $this->sendResponse(
new {{ $config->modelNames->name }}Resource(${{ $config->modelNames->camel }}),
__('messages.saved', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')])
);
@else
return $this->sendResponse(new {{ $config->modelNames->name }}Resource(${{ $config->modelNames->camel }}), '{{ $config->modelNames->human }} saved successfully');
@endif
}
{!! $docShow !!}
public function show({{ $config->modelNames->name }} ${{ $config->modelNames->camel }}): JsonResponse
{
if (empty(${{ $config->modelNames->camel }})) {
@if($config->options->localized)
return $this->sendError(
__('messages.not_found', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')])
);
@else
return $this->sendError('{{ $config->modelNames->human }} not found');
@endif
}
@if($config->options->localized)
return $this->sendResponse(
new {{ $config->modelNames->name }}Resource(${{ $config->modelNames->camel }}),
__('messages.retrieved', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')])
);
@else
return $this->sendResponse(new {{ $config->modelNames->name }}Resource(${{ $config->modelNames->camel }}), '{{ $config->modelNames->human }} retrieved successfully');
@endif
}
{!! $docUpdate !!}
public function update({{ $config->modelNames->name }} ${{ $config->modelNames->camel }}, Update{{ $config->modelNames->name }}APIRequest $request): JsonResponse
{
if (empty(${{ $config->modelNames->camel }})) {
@if($config->options->localized)
return $this->sendError(
__('messages.not_found', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')])
);
@else
return $this->sendError('{{ $config->modelNames->human }} not found');
@endif
}
${{ $config->modelNames->camel }}->fill($request->all());
${{ $config->modelNames->camel }}->save();
@if($config->options->localized)
return $this->sendResponse(
new {{ $config->modelNames->name }}Resource(${{ $config->modelNames->camel }}),
__('messages.updated', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')])
);
@else
return $this->sendResponse(new {{ $config->modelNames->name }}Resource(${{ $config->modelNames->camel }}), '{{ $config->modelNames->name }} updated successfully');
@endif
}
{!! $docDestroy !!}
public function destroy({{ $config->modelNames->name }} ${{ $config->modelNames->camel }}): JsonResponse
{
if (empty(${{ $config->modelNames->camel }})) {
@if($config->options->localized)
return $this->sendError(
__('messages.not_found', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')])
);
@else
return $this->sendError('{{ $config->modelNames->human }} not found');
@endif
}
${{ $config->modelNames->camel }}->delete();
@if($config->options->localized)
return $this->sendResponse(
${{ $config->modelNames->camel }}->id,
__('messages.deleted', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')])
);
@else
return $this->sendSuccess('{{ $config->modelNames->human }} deleted successfully');
@endif
}
}