1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alive2212\LaravelSmartRestful; |
4
|
|
|
|
5
|
|
|
use Alive2212\ExcelHelper\ExcelHelper; |
6
|
|
|
use Alive2212\LaravelQueryHelper\QueryHelper; |
|
|
|
|
7
|
|
|
use Alive2212\LaravelRequestHelper\RequestHelper; |
|
|
|
|
8
|
|
|
use Alive2212\LaravelSmartResponse\ResponseModel; |
9
|
|
|
use Alive2212\LaravelSmartResponse\SmartResponse\SmartResponse; |
|
|
|
|
10
|
|
|
use Alive2212\LaravelStringHelper\StringHelper; |
|
|
|
|
11
|
|
|
use App\Car; |
|
|
|
|
12
|
|
|
use App\Http\Controllers\Controller; |
|
|
|
|
13
|
|
|
use App\Location; |
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
15
|
|
|
use Illuminate\Database\QueryException; |
16
|
|
|
use Illuminate\Http\Request; |
17
|
|
|
use Illuminate\Support\Facades\DB; |
18
|
|
|
use Illuminate\Support\MessageBag; |
19
|
|
|
use Illuminate\Support\Facades\Validator; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
abstract class BaseController extends Controller |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* to use this class |
26
|
|
|
* create message list as messages in message file |
27
|
|
|
* override __constructor and define your model |
28
|
|
|
* define your rules for index,store and update |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $DEFAULT_RESULT_PER_PAGE = 15; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $DEFAULT_PAGE_NUMBER = 1; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $pivotFields = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $uniqueFields = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var bool|string |
53
|
|
|
*/ |
54
|
|
|
protected $modelName; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $messagePrefix = 'messages.api.v1.'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* this model |
63
|
|
|
*/ |
64
|
|
|
protected $model; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* index request validator rules |
68
|
|
|
* |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
protected $indexValidateArray = [ |
72
|
|
|
// |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* array of relationship for eager loading |
77
|
|
|
* |
78
|
|
|
* @var array |
79
|
|
|
*/ |
80
|
|
|
protected $indexLoad = [ |
81
|
|
|
// |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* array of relationship for eager loading |
86
|
|
|
* |
87
|
|
|
* @var array |
88
|
|
|
*/ |
89
|
|
|
protected $editLoad = [ |
90
|
|
|
// |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* array of relationship for eager loading |
95
|
|
|
* |
96
|
|
|
* @var array |
97
|
|
|
*/ |
98
|
|
|
protected $updateLoad = [ |
99
|
|
|
// |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* array of relationship for eager loading |
104
|
|
|
* |
105
|
|
|
* @var array |
106
|
|
|
*/ |
107
|
|
|
protected $storeLoad = [ |
108
|
|
|
// |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* store request validator rules |
113
|
|
|
* |
114
|
|
|
* @var array |
115
|
|
|
*/ |
116
|
|
|
protected $storeValidateArray = [ |
117
|
|
|
// |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* update request validator rules |
122
|
|
|
* |
123
|
|
|
* @var array |
124
|
|
|
*/ |
125
|
|
|
protected $updateValidateArray = [ |
126
|
|
|
// |
127
|
|
|
]; |
128
|
|
|
|
129
|
|
|
protected $middlewareParams = []; |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* defaultController constructor. |
133
|
|
|
*/ |
134
|
|
|
public function __construct() |
135
|
|
|
{ |
136
|
|
|
// dd("I have closest relationship with all US celebrities"); |
137
|
|
|
// init controller |
138
|
|
|
$this->initController(); |
139
|
|
|
|
140
|
|
|
// set local language |
141
|
|
|
$this->setLocale(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
abstract public function initController(); |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Display a listing of the resource. |
148
|
|
|
* |
149
|
|
|
* @param Request $request |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function index(Request $request) |
153
|
|
|
{ |
154
|
|
|
// create response model |
155
|
|
|
$response = new ResponseModel(); |
156
|
|
|
|
157
|
|
|
$pageSize = $this->DEFAULT_RESULT_PER_PAGE; |
|
|
|
|
158
|
|
|
$pageNumber = 1; |
159
|
|
|
|
160
|
|
|
//set default pagination |
161
|
|
|
|
162
|
|
|
//set page size |
163
|
|
|
if (!isset($request->toArray()['page']['size'])) { |
164
|
|
|
$pageSize = $this->DEFAULT_RESULT_PER_PAGE; |
165
|
|
|
} elseif (($request->get('page')['size']) == 0) { |
166
|
|
|
$pageSize = $this->DEFAULT_RESULT_PER_PAGE; |
167
|
|
|
} else { |
168
|
|
|
$pageSize = $request->get('page')['size']; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
//set page number |
172
|
|
|
if (!isset($request->get('page')['number'])) { |
173
|
|
|
$pageNumber = $this->DEFAULT_PAGE_NUMBER; |
174
|
|
|
} elseif (($request->get('page')['number']) == 0) { |
175
|
|
|
$pageNumber = $this->DEFAULT_PAGE_NUMBER; |
176
|
|
|
} else { |
177
|
|
|
$pageNumber = $request->get('page')['number']; |
178
|
|
|
} |
179
|
|
|
$request['page'] = $pageNumber; |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
//set default ordering |
183
|
|
|
if (isset($request->toArray()['order_by'])) { |
184
|
|
|
if (is_null($request['order_by'])) { |
185
|
|
|
$request['order_by'] = "{\"field\":\"id\",\"operator\":\"Desc\"}"; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$validationErrors = $this->checkRequestValidation($request, $this->indexValidateArray); |
190
|
|
|
if ($validationErrors != null) { |
191
|
|
|
|
192
|
|
|
// return response |
193
|
|
|
$response->setData(collect($validationErrors->toArray())); |
194
|
|
|
$response->setMessage($this->getTrans('index', 'failed_validation')); |
195
|
|
|
$response->setStatus(false); |
196
|
|
|
$response->setError(99); |
197
|
|
|
return SmartResponse::response($response); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
try { |
201
|
|
|
$data = $request->get('query') != null ? |
202
|
|
|
$this->model |
203
|
|
|
->whereKey(collect($this->model |
204
|
|
|
->search(($request->get('query'))) |
205
|
|
|
->raw())->get('ids')) : |
206
|
|
|
$this->model; |
207
|
|
|
if (array_key_exists('file', $request->toArray())) { |
208
|
|
|
//TODO add relation on top if here and create a tree flatter array in array helper |
209
|
|
|
return (new ExcelHelper())->setOptions([ |
|
|
|
|
210
|
|
|
'store_format' => $request->get('file') == null ? 'xls' : $request->get('file'), |
211
|
|
|
'download_format' => $request->get('file') == null ? 'xls' : $request->get('file'), |
212
|
|
|
])->table($data->get()->toArray())->createExcelFile()->download(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// load relations |
216
|
|
|
if (count($this->indexLoad) > 0) { |
217
|
|
|
$data = $data->with($this->indexLoad); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// filters by |
221
|
|
|
if (isset($request->toArray()['filters'])) { |
222
|
|
|
$data = (new QueryHelper())->deepFilter($data, (new RequestHelper())->getCollectFromJson($request['filters'])); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
// order by |
226
|
|
|
if (isset($request->toArray()['order_by'])) { |
227
|
|
|
$data = (new QueryHelper())->orderBy($data, (new RequestHelper())->getCollectFromJson($request['order_by'])); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
// return response |
231
|
|
|
$response->setData(collect($data->paginate($pageSize))); |
232
|
|
|
$response->setMessage($this->getTrans('index', 'successful')); |
233
|
|
|
return SmartResponse::response($response); |
234
|
|
|
|
235
|
|
|
} catch (QueryException $exception) { |
236
|
|
|
|
237
|
|
|
// return response |
238
|
|
|
$response->setData(collect($exception->getMessage())); |
239
|
|
|
$response->setError($exception->getCode()); |
240
|
|
|
$response->setMessage($this->getTrans('index', 'failed')); |
241
|
|
|
$response->setStatus(false); |
242
|
|
|
return SmartResponse::response($response); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function checkRequestValidation(Request $request, $validationArray) |
247
|
|
|
{ |
248
|
|
|
$requestParams = $request->toArray(); |
|
|
|
|
249
|
|
|
$validator = Validator::make($request->all(), $validationArray); |
250
|
|
|
if ($validator->fails()) { |
251
|
|
|
return $validator->errors(); |
252
|
|
|
} |
253
|
|
|
return null; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @param $status |
258
|
|
|
* @return mixed |
259
|
|
|
*/ |
260
|
|
|
public function message($status) |
261
|
|
|
{ |
262
|
|
|
$key = $this->messagePrefix . $this->modelName . '.' . debug_backtrace()[1]['function'] . '.' . $status; |
263
|
|
|
return $this->getMessageFromFile($key); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param $key |
268
|
|
|
* @return mixed |
269
|
|
|
*/ |
270
|
|
|
public function getMessageFromFile($key) |
271
|
|
|
{ |
272
|
|
|
return config($key); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Show the form for creating a new resource. |
277
|
|
|
* |
278
|
|
|
* @return \Illuminate\Http\JsonResponse |
279
|
|
|
*/ |
280
|
|
|
public function create() |
281
|
|
|
{ |
282
|
|
|
// Create Response Model |
283
|
|
|
$response = new ResponseModel(); |
284
|
|
|
|
285
|
|
|
// return response |
286
|
|
|
$response->setData(collect($this->model->getFillable())); |
287
|
|
|
$response->setMessage($this->getTrans('create', 'successful')); |
288
|
|
|
return SmartResponse::response($response); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Store a newly created resource in storage. |
293
|
|
|
* |
294
|
|
|
* @param \Illuminate\Http\Request $request |
295
|
|
|
* @return \Illuminate\Http\JsonResponse |
296
|
|
|
*/ |
297
|
|
|
public function store(Request $request) |
298
|
|
|
{ |
299
|
|
|
// Create Response Model |
300
|
|
|
$response = new ResponseModel(); |
301
|
|
|
|
302
|
|
|
if (!isset($userId)) { |
|
|
|
|
303
|
|
|
$userId = 1; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
//add author id into the request if doesn't exist |
307
|
|
|
if (is_null($request->get('author_id'))) { |
308
|
|
|
$request['author_id'] = $userId; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
//add user id into the request if doesn't exist |
312
|
|
|
if (is_null($request->get('user_id'))) { |
313
|
|
|
$request['user_id'] = $userId; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
$validationErrors = $this->checkRequestValidation($request, $this->storeValidateArray); |
317
|
|
|
|
318
|
|
|
if ($validationErrors != null) { |
319
|
|
|
if (env('APP_DEBUG', false)) { |
320
|
|
|
$response->setData(collect($validationErrors->getMessages())); |
321
|
|
|
} |
322
|
|
|
$response->setMessage($this->getTrans(__FUNCTION__, 'validation_failed')); |
323
|
|
|
$response->setStatus(false); |
324
|
|
|
return SmartResponse::response($response); |
325
|
|
|
} |
326
|
|
|
try { |
327
|
|
|
// get result of model creation |
328
|
|
|
$result = $this->model->create($request->all()); |
329
|
|
|
// sync many to many relation |
330
|
|
|
foreach ($this->pivotFields as $pivotField) { |
331
|
|
|
if (collect($request[$pivotField])->count()) { |
332
|
|
|
$pivotField = (new StringHelper())->toCamel($pivotField); |
333
|
|
|
$this->model->find($result['id'])->$pivotField()->sync(json_decode($request[$pivotField])); |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
$response->setMessage($this->getTrans('store', 'successful')); |
337
|
|
|
|
338
|
|
|
|
339
|
|
|
$response->setData($this->model |
340
|
|
|
->where($this->model->getKeyName(), $result['id']) |
341
|
|
|
->with(collect($this->updateLoad)->count() == 0 ? $this->indexLoad : $this->updateLoad) |
342
|
|
|
->get()); |
343
|
|
|
|
344
|
|
|
$response->setStatus(true); |
345
|
|
|
} catch (QueryException $exception) { |
346
|
|
|
$response->setError($exception->getCode()); |
|
|
|
|
347
|
|
|
$response->setMessage($this->getTrans('store', 'failed')); |
348
|
|
|
$response->setStatus(false); |
349
|
|
|
if (env('APP_DEBUG', false)) { |
350
|
|
|
$response->setData(collect($exception->getMessage())); |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
return SmartResponse::response($response); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Display the specdefaultied resource. |
358
|
|
|
* |
359
|
|
|
* @param int $id |
360
|
|
|
* @return \Illuminate\Http\JsonResponse |
361
|
|
|
*/ |
362
|
|
|
public function show($id) |
363
|
|
|
{ |
364
|
|
|
// Create Response Model |
365
|
|
|
$response = new ResponseModel(); |
366
|
|
|
|
367
|
|
|
// try to get data |
368
|
|
|
try { |
369
|
|
|
$response->setMessage($this->getTrans('show', 'successful')); |
370
|
|
|
$response->setData(collect($this->model->findOrFail($id))); |
371
|
|
|
|
372
|
|
|
// catch exception |
373
|
|
|
} catch (ModelNotFoundException $exception) { |
374
|
|
|
$response->setError($exception->getCode()); |
|
|
|
|
375
|
|
|
$response->setMessage($this->getTrans('show', 'failed')); |
376
|
|
|
$response->setStatus(false); |
377
|
|
|
if (env('APP_DEBUG', false)) { |
378
|
|
|
$response->setData(collect($exception->getMessage())); |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
|
return SmartResponse::response($response); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Show the form for editing the specified resource. |
386
|
|
|
* |
387
|
|
|
* @param int $id |
388
|
|
|
* @return \Illuminate\Http\JsonResponse |
389
|
|
|
*/ |
390
|
|
|
public function edit($id) |
391
|
|
|
{ |
392
|
|
|
// Create Response Model |
393
|
|
|
$response = new ResponseModel(); |
394
|
|
|
|
395
|
|
|
try { |
396
|
|
|
$response->setMessage($this->getTrans('edit', 'successful')); |
397
|
|
|
$response->setData($this->model |
398
|
|
|
->where($this->model->getKeyName(), $id) |
399
|
|
|
->with(collect($this->editLoad)->count() == 0 ? $this->indexLoad : $this->editLoad) |
400
|
|
|
->get()); |
401
|
|
|
|
402
|
|
|
} catch (ModelNotFoundException $exception) { |
403
|
|
|
$response->setError($exception->getCode()); |
|
|
|
|
404
|
|
|
$response->setMessage($this->getTrans('edit', 'failed')); |
405
|
|
|
$response->setStatus(false); |
406
|
|
|
if (env('APP_DEBUG', false)) { |
407
|
|
|
$response->setData(collect($exception->getMessage())); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
return SmartResponse::response($response); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Update the specified resource in storage. |
417
|
|
|
* |
418
|
|
|
* @param \Illuminate\Http\Request $request |
419
|
|
|
* @param int $id |
420
|
|
|
* @return \Illuminate\Http\JsonResponse |
421
|
|
|
*/ |
422
|
|
|
public function update(Request $request, $id) |
423
|
|
|
{ |
424
|
|
|
// Create Response Model |
425
|
|
|
$response = new ResponseModel(); |
426
|
|
|
|
427
|
|
|
$validationErrors = $this->checkRequestValidation($request, $this->updateValidateArray); |
428
|
|
|
if ($validationErrors != null) { |
429
|
|
|
$response->setData(collect($validationErrors->toArray())); |
430
|
|
|
$response->setMessage($this->getTrans('update', 'failed_validation')); |
431
|
|
|
$response->setStatus(false); |
432
|
|
|
$response->setError(99); |
433
|
|
|
return SmartResponse::response($response); |
434
|
|
|
|
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
try { |
438
|
|
|
// sync many to many relation |
439
|
|
|
foreach ($this->pivotFields as $pivotField) { |
440
|
|
|
if (collect($request[$pivotField])->count()) { |
441
|
|
|
$pivotMethod = (new StringHelper())->toCamel($pivotField); |
442
|
|
|
$this->model->findOrFail($id)->$pivotMethod()->sync(json_decode($request[$pivotField], true)); |
443
|
|
|
} |
444
|
|
|
} |
445
|
|
|
//get result of update |
446
|
|
|
$result = $this->model->findOrFail($id)->update($request->all()); |
447
|
|
|
|
448
|
|
|
// return response |
449
|
|
|
$response->setData($this->model |
450
|
|
|
->where($this->model->getKeyName(), $id) |
451
|
|
|
->with(collect($this->updateLoad)->count() == 0 ? $this->indexLoad : $this->updateLoad) |
452
|
|
|
->get()); |
453
|
|
|
$response->setMessage( |
454
|
|
|
$this->getTrans('update', 'successful1') . |
|
|
|
|
455
|
|
|
$result . |
456
|
|
|
$this->getTrans('update', 'successful2') |
|
|
|
|
457
|
|
|
); |
458
|
|
|
|
459
|
|
|
} catch (ModelNotFoundException $exception) { |
460
|
|
|
$response->setStatus(false); |
461
|
|
|
$response->setMessage($this->getTrans('update', 'model_not_found')); |
462
|
|
|
$response->setError($exception->getCode()); |
463
|
|
|
if (env('APP_DEBUG', false)) { |
464
|
|
|
$response->setData(collect($exception->getMessage())); |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
} catch (QueryException $exception) { |
468
|
|
|
$response->setStatus(false); |
469
|
|
|
$response->setMessage($this->getTrans('update', 'failed')); |
470
|
|
|
$response->setError($exception->getCode()); |
471
|
|
|
if (env('APP_DEBUG', false)) { |
472
|
|
|
$response->setData(collect($exception->getMessage())); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
return SmartResponse::response($response); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Remove the specified resource from storage. |
482
|
|
|
* |
483
|
|
|
* @param int $id |
484
|
|
|
* @return \Illuminate\Http\JsonResponse |
485
|
|
|
*/ |
486
|
|
|
public function destroy($id) |
487
|
|
|
{ |
488
|
|
|
// Create Response Model |
489
|
|
|
$response = new ResponseModel(); |
490
|
|
|
|
491
|
|
|
try { |
492
|
|
|
$response->setData(collect($this->model->findOrFail($id)->delete())); |
493
|
|
|
$response->setMessage($this->getTrans('destroy', 'successful')); |
494
|
|
|
|
495
|
|
|
} catch (ModelNotFoundException $exception) { |
496
|
|
|
$response->setMessage($this->getTrans('destroy', 'successful')); |
497
|
|
|
$response->setStatus(false); |
498
|
|
|
$response->setError($exception->getCode()); |
|
|
|
|
499
|
|
|
if (env('APP_DEBUG', false)) { |
500
|
|
|
$response->setData(collect($exception->getMessage())); |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
return SmartResponse::response($response); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* @param $method |
510
|
|
|
* @param $status |
511
|
|
|
* @return array|\Illuminate\Contracts\Translation\Translator|null|string |
512
|
|
|
*/ |
513
|
|
|
public function getTrans($method, $status) |
514
|
|
|
{ |
515
|
|
|
return trans('laravel_smart_restful::' . 'controller.' . get_class($this->model) . '.' . $method . '.' . $status); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
public function setLocale() |
519
|
|
|
{ |
520
|
|
|
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
521
|
|
|
\App::setLocale($_SERVER['HTTP_ACCEPT_LANGUAGE']); |
522
|
|
|
} |
523
|
|
|
} |
524
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths