Skip to content

Commit 34eec77

Browse files
committed
Old Stubs are Updated
1 parent b1cc2cc commit 34eec77

File tree

9 files changed

+50
-311
lines changed

9 files changed

+50
-311
lines changed

config/api-crud.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@
4848
|--------------------------------------------------------------------------
4949
| Customise the paths where the folders will be generated.
5050
| Set the generate key too false to not generate that folder
51+
| Note: migration, tests are excluded from root path
52+
|
5153
*/
5254
'templates' => [
5355
'migration' => [
5456
'path' => 'database/migrations',
55-
'generate' => false,
57+
'generate' => true,
5658
'namespace' => null,
5759
],
5860
'seeder' => [

stubs/controller-crud.stub

Lines changed: 27 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

33
namespace $CLASS_NAMESPACE$;
4+
45
use Exception;
6+
use $MODULE$\$MODEL$;
57
use Laraflow\ApiCrud\Exceptions\StoreOperationException;
68
use Laraflow\ApiCrud\Exceptions\UpdateOperationException;
79
use Laraflow\ApiCrud\Exceptions\DeleteOperationException;
8-
use Laraflow\ApiCrud\Exceptions\RestoreOperationException;
9-
use Fintech\$MODULE$\Facades\$MODULE$;
1010
$RESOURCE_NAMESPACES$
1111
$REQUEST_NAMESPACES$
1212
use Illuminate\Database\Eloquent\ModelNotFoundException;
@@ -23,7 +23,6 @@ use Illuminate\Routing\Controller;
2323
* @lrd:end
2424
*
2525
*/
26-
2726
class $CLASS$ extends Controller
2827
{
2928
/**
@@ -41,9 +40,16 @@ class $CLASS$ extends Controller
4140
try {
4241
$inputs = $request->validated();
4342

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+
}
4551

46-
return new $RESOURCE$Collection($$RESOURCE_VARIABLE$Paginate);
52+
return new $RESOURCE$Collection($$RESOURCE_VARIABLE$s);
4753

4854
} catch (Exception $exception) {
4955

@@ -65,15 +71,15 @@ class $CLASS$ extends Controller
6571
try {
6672
$inputs = $request->validated();
6773

68-
$$RESOURCE_VARIABLE$ = $MODULE$::$RESOURCE_VARIABLE$()->create($inputs);
74+
$$RESOURCE_VARIABLE$ = $RESOURCE$::create($inputs);
6975

7076
if (!$$RESOURCE_VARIABLE$) {
71-
throw (new StoreOperationException)->setModel(config('fintech.$LOWER_NAME$.$CONFIG_VARIABLE$_model'));
77+
throw (new StoreOperationException)->setModel($RESOURCE$::class);
7278
}
7379

7480
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()
7783
]);
7884

7985
} catch (Exception $exception) {
@@ -87,26 +93,15 @@ class $CLASS$ extends Controller
8793
* Return a specified *$RESOURCE$* resource found by id.
8894
* @lrd:end
8995
*
90-
* @param string|int $id
96+
* @param $RESOURCE$ $$RESOURCE_VARIABLE$
9197
* @return $RESOURCE$Resource|JsonResponse
92-
* @throws ModelNotFoundException
9398
*/
94-
public function show(string|int $id): $RESOURCE$Resource|JsonResponse
99+
public function show($RESOURCE$ $$RESOURCE_VARIABLE$): $RESOURCE$Resource|JsonResponse
95100
{
96101
try {
97102

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-
104103
return new $RESOURCE$Resource($$RESOURCE_VARIABLE$);
105104

106-
} catch (ModelNotFoundException $exception) {
107-
108-
return response()->notfound($exception->getMessage());
109-
110105
} catch (Exception $exception) {
111106

112107
return response()->failed($exception->getMessage());
@@ -119,33 +114,22 @@ class $CLASS$ extends Controller
119114
* @lrd:end
120115
*
121116
* @param $UPDATE_REQUEST$ $request
122-
* @param string|int $id
117+
* @param $RESOURCE$ $$RESOURCE_VARIABLE$
123118
* @return JsonResponse
124-
* @throws ModelNotFoundException
125119
* @throws UpdateOperationException
126120
*/
127-
public function update($UPDATE_REQUEST$ $request, string|int $id): JsonResponse
121+
public function update($UPDATE_REQUEST$ $request, $RESOURCE$ $$RESOURCE_VARIABLE$): JsonResponse
128122
{
129123
try {
130124

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-
137125
$inputs = $request->validated();
138126

139-
if (!$MODULE$::$RESOURCE_VARIABLE$()->update($id, $inputs)) {
127+
if (!$$RESOURCE_VARIABLE$->update($inputs)) {
140128

141-
throw (new UpdateOperationException)->setModel(config('fintech.$LOWER_NAME$.$CONFIG_VARIABLE$_model'), $id);
129+
throw (new UpdateOperationException)->setModel($RESOURCE$::class, $$RESOURCE_VARIABLE$->getKey());
142130
}
143131

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$']));
149133

150134
} catch (Exception $exception) {
151135

@@ -158,117 +142,21 @@ class $CLASS$ extends Controller
158142
* Soft delete a specified *$RESOURCE$* resource using id.
159143
* @lrd:end
160144
*
161-
* @param string|int $id
145+
* @param $RESOURCE$ $$RESOURCE_VARIABLE$
162146
* @return JsonResponse
163147
* @throws ModelNotFoundException
164148
* @throws DeleteOperationException
165149
*/
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$)
203151
{
204152
try {
205153

206-
$$RESOURCE_VARIABLE$ = $MODULE$::$RESOURCE_VARIABLE$()->find($id, true);
154+
if (!$$RESOURCE_VARIABLE$->delete()) {
207155

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());
210157
}
211158

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$']));
272160

273161
} catch (Exception $exception) {
274162

stubs/factory.stub

Lines changed: 0 additions & 28 deletions
This file was deleted.

stubs/feature-test.stub

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)