1
1
<?php
2
2
3
3
namespace $CLASS_NAMESPACE$;
4
+
4
5
use Exception;
6
+ use $MODULE$\$MODEL$;
5
7
use Laraflow\ApiCrud\Exceptions\StoreOperationException;
6
8
use Laraflow\ApiCrud\Exceptions\UpdateOperationException;
7
9
use Laraflow\ApiCrud\Exceptions\DeleteOperationException;
8
- use Laraflow\ApiCrud\Exceptions\RestoreOperationException;
9
- use Fintech\$MODULE$\Facades\$MODULE$;
10
10
$RESOURCE_NAMESPACES$
11
11
$REQUEST_NAMESPACES$
12
12
use Illuminate\Database\Eloquent\ModelNotFoundException;
@@ -23,7 +23,6 @@ use Illuminate\Routing\Controller;
23
23
* @lrd:end
24
24
*
25
25
*/
26
-
27
26
class $CLASS$ extends Controller
28
27
{
29
28
/**
@@ -41,9 +40,16 @@ class $CLASS$ extends Controller
41
40
try {
42
41
$inputs = $request->validated();
43
42
44
- $$RESOURCE_VARIABLE$Paginate = $MODULE$::$RESOURCE_VARIABLE$()->list($inputs);
43
+ $$RESOURCE_VARIABLE$s = $RESOURCE$::filter($inputs)
44
+ ->orderBy($request->input('sort', 'id'), $request->input('dir', 'asc'));
45
+
46
+ if($request->input('paginate', true)) {
47
+ $$RESOURCE_VARIABLE$s = $$RESOURCE_VARIABLE$s->paginate($request->input('per_page'));
48
+ } else {
49
+ $$RESOURCE_VARIABLE$s = $$RESOURCE_VARIABLE$s->get();
50
+ }
45
51
46
- return new $RESOURCE$Collection($$RESOURCE_VARIABLE$Paginate );
52
+ return new $RESOURCE$Collection($$RESOURCE_VARIABLE$s );
47
53
48
54
} catch (Exception $exception) {
49
55
@@ -65,15 +71,15 @@ class $CLASS$ extends Controller
65
71
try {
66
72
$inputs = $request->validated();
67
73
68
- $$RESOURCE_VARIABLE$ = $MODULE $::$RESOURCE_VARIABLE$()-> create($inputs);
74
+ $$RESOURCE_VARIABLE$ = $RESOURCE $::create($inputs);
69
75
70
76
if (!$$RESOURCE_VARIABLE$) {
71
- throw (new StoreOperationException)->setModel(config('fintech.$LOWER_NAME$.$CONFIG_VARIABLE$_model') );
77
+ throw (new StoreOperationException)->setModel($RESOURCE$::class );
72
78
}
73
79
74
80
return response()->created([
75
- 'message' => __('restapi ::messages.resource.created', ['model' => '$MESSAGE_VARIABLE$']),
76
- 'id' => $$RESOURCE_VARIABLE$->id
81
+ 'message' => __('api-crud ::messages.resource.created', ['model' => '$MESSAGE_VARIABLE$']),
82
+ 'id' => $$RESOURCE_VARIABLE$->getKey()
77
83
]);
78
84
79
85
} catch (Exception $exception) {
@@ -87,26 +93,15 @@ class $CLASS$ extends Controller
87
93
* Return a specified *$RESOURCE$* resource found by id.
88
94
* @lrd:end
89
95
*
90
- * @param string|int $id
96
+ * @param $RESOURCE$ $$RESOURCE_VARIABLE$
91
97
* @return $RESOURCE$Resource|JsonResponse
92
- * @throws ModelNotFoundException
93
98
*/
94
- public function show(string|int $id ): $RESOURCE$Resource|JsonResponse
99
+ public function show($RESOURCE$ $$RESOURCE_VARIABLE$ ): $RESOURCE$Resource|JsonResponse
95
100
{
96
101
try {
97
102
98
- $$RESOURCE_VARIABLE$ = $MODULE$::$RESOURCE_VARIABLE$()->find($id);
99
-
100
- if (!$$RESOURCE_VARIABLE$) {
101
- throw (new ModelNotFoundException)->setModel(config('fintech.$LOWER_NAME$.$CONFIG_VARIABLE$_model'), $id);
102
- }
103
-
104
103
return new $RESOURCE$Resource($$RESOURCE_VARIABLE$);
105
104
106
- } catch (ModelNotFoundException $exception) {
107
-
108
- return response()->notfound($exception->getMessage());
109
-
110
105
} catch (Exception $exception) {
111
106
112
107
return response()->failed($exception->getMessage());
@@ -119,33 +114,22 @@ class $CLASS$ extends Controller
119
114
* @lrd:end
120
115
*
121
116
* @param $UPDATE_REQUEST$ $request
122
- * @param string|int $id
117
+ * @param $RESOURCE$ $$RESOURCE_VARIABLE$
123
118
* @return JsonResponse
124
- * @throws ModelNotFoundException
125
119
* @throws UpdateOperationException
126
120
*/
127
- public function update($UPDATE_REQUEST$ $request, string|int $id ): JsonResponse
121
+ public function update($UPDATE_REQUEST$ $request, $RESOURCE$ $$RESOURCE_VARIABLE$ ): JsonResponse
128
122
{
129
123
try {
130
124
131
- $$RESOURCE_VARIABLE$ = $MODULE$::$RESOURCE_VARIABLE$()->find($id);
132
-
133
- if (!$$RESOURCE_VARIABLE$) {
134
- throw (new ModelNotFoundException)->setModel(config('fintech.$LOWER_NAME$.$CONFIG_VARIABLE$_model'), $id);
135
- }
136
-
137
125
$inputs = $request->validated();
138
126
139
- if (!$MODULE$::$ RESOURCE_VARIABLE$() ->update($id, $inputs)) {
127
+ if (!$$ RESOURCE_VARIABLE$->update($inputs)) {
140
128
141
- throw (new UpdateOperationException)->setModel(config('fintech.$LOWER_NAME$.$CONFIG_VARIABLE$_model') , $id );
129
+ throw (new UpdateOperationException)->setModel($RESOURCE$::class , $$RESOURCE_VARIABLE$->getKey() );
142
130
}
143
131
144
- return response()->updated(__('restapi::messages.resource.updated', ['model' => '$MESSAGE_VARIABLE$']));
145
-
146
- } catch (ModelNotFoundException $exception) {
147
-
148
- return response()->notfound($exception->getMessage());
132
+ return response()->updated(__('api-crud::messages.resource.updated', ['model' => '$MESSAGE_VARIABLE$']));
149
133
150
134
} catch (Exception $exception) {
151
135
@@ -158,117 +142,21 @@ class $CLASS$ extends Controller
158
142
* Soft delete a specified *$RESOURCE$* resource using id.
159
143
* @lrd:end
160
144
*
161
- * @param string|int $id
145
+ * @param $RESOURCE$ $$RESOURCE_VARIABLE$
162
146
* @return JsonResponse
163
147
* @throws ModelNotFoundException
164
148
* @throws DeleteOperationException
165
149
*/
166
- public function destroy(string|int $id)
167
- {
168
- try {
169
-
170
- $$RESOURCE_VARIABLE$ = $MODULE$::$RESOURCE_VARIABLE$()->find($id);
171
-
172
- if (!$$RESOURCE_VARIABLE$) {
173
- throw (new ModelNotFoundException)->setModel(config('fintech.$LOWER_NAME$.$CONFIG_VARIABLE$_model'), $id);
174
- }
175
-
176
- if (!$MODULE$::$RESOURCE_VARIABLE$()->destroy($id)) {
177
-
178
- throw (new DeleteOperationException())->setModel(config('fintech.$LOWER_NAME$.$CONFIG_VARIABLE$_model'), $id);
179
- }
180
-
181
- return response()->deleted(__('restapi::messages.resource.deleted', ['model' => '$MESSAGE_VARIABLE$']));
182
-
183
- } catch (ModelNotFoundException $exception) {
184
-
185
- return response()->notfound($exception->getMessage());
186
-
187
- } catch (Exception $exception) {
188
-
189
- return response()->failed($exception->getMessage());
190
- }
191
- }
192
-
193
- /**
194
- * @lrd:start
195
- * Restore the specified *$RESOURCE$* resource from trash.
196
- * ** ```Soft Delete``` needs to enabled to use this feature**
197
- * @lrd:end
198
- *
199
- * @param string|int $id
200
- * @return JsonResponse
201
- */
202
- public function restore(string|int $id)
150
+ public function destroy($RESOURCE$ $$RESOURCE_VARIABLE$)
203
151
{
204
152
try {
205
153
206
- $$RESOURCE_VARIABLE$ = $MODULE$::$ RESOURCE_VARIABLE$()->find($id, true);
154
+ if (!$$ RESOURCE_VARIABLE$->delete()) {
207
155
208
- if (!$$RESOURCE_VARIABLE$) {
209
- throw (new ModelNotFoundException)->setModel(config('fintech.$LOWER_NAME$.$CONFIG_VARIABLE$_model'), $id);
156
+ throw (new DeleteOperationException())->setModel($RESOURCE$::class, $$RESOURCE_VARIABLE$->getKey());
210
157
}
211
158
212
- if (!$MODULE$::$RESOURCE_VARIABLE$()->restore($id)) {
213
-
214
- throw (new RestoreOperationException())->setModel(config('fintech.$LOWER_NAME$.$CONFIG_VARIABLE$_model'), $id);
215
- }
216
-
217
- return response()->restored(__('restapi::messages.resource.restored', ['model' => '$MESSAGE_VARIABLE$']));
218
-
219
- } catch (ModelNotFoundException $exception) {
220
-
221
- return response()->notfound($exception->getMessage());
222
-
223
- } catch (Exception $exception) {
224
-
225
- return response()->failed($exception->getMessage());
226
- }
227
- }
228
-
229
- /**
230
- * @lrd:start
231
- * Create a exportable list of the *$RESOURCE$* resource as document.
232
- * After export job is done system will fire export completed event
233
- *
234
- * @lrd:end
235
- *
236
- * @param $INDEX_REQUEST$ $request
237
- * @return JsonResponse
238
- */
239
- public function export($INDEX_REQUEST$ $request): JsonResponse
240
- {
241
- try {
242
- $inputs = $request->validated();
243
-
244
- $$RESOURCE_VARIABLE$Paginate = $MODULE$::$RESOURCE_VARIABLE$()->export($inputs);
245
-
246
- return response()->exported(__('restapi::messages.resource.exported', ['model' => '$MESSAGE_VARIABLE$']));
247
-
248
- } catch (Exception $exception) {
249
-
250
- return response()->failed($exception->getMessage());
251
- }
252
- }
253
-
254
- /**
255
- * @lrd:start
256
- * Create a exportable list of the *$RESOURCE$* resource as document.
257
- * After export job is done system will fire export completed event
258
- *
259
- * @lrd:end
260
- *
261
- * @param $IMPORT_REQUEST$ $request
262
- * @return $RESOURCE$Collection|JsonResponse
263
- */
264
- public function import($IMPORT_REQUEST$ $request): JsonResponse
265
- {
266
- try {
267
- $inputs = $request->validated();
268
-
269
- $$RESOURCE_VARIABLE$Paginate = $MODULE$::$RESOURCE_VARIABLE$()->list($inputs);
270
-
271
- return new $RESOURCE$Collection($$RESOURCE_VARIABLE$Paginate);
159
+ return response()->deleted(__('api-crud::messages.resource.deleted', ['model' => '$MESSAGE_VARIABLE$']));
272
160
273
161
} catch (Exception $exception) {
274
162
0 commit comments