-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathApiController.php
420 lines (355 loc) · 12.6 KB
/
ApiController.php
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
/**
* Created by PhpStorm.
* User: luezoid
* Date: 12/18/17
* Time: 11:53 AM
*/
namespace Luezoid\Laravelcore\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Luezoid\Laravelcore\Constants\ErrorConstants;
use Luezoid\Laravelcore\Jobs\BaseJob;
use Luezoid\Laravelcore\Services\EnvironmentService;
use Luezoid\Laravelcore\Services\UtilityService;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
abstract class ApiController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected $repo;
protected $model;
protected $isCamelToSnake = true;
protected $isSnakeToCamel = true;
protected $haveUserCondition = false;
protected $indexCall = 'getAll';
protected $indexWith = [];
protected $showCall = 'show';
protected $showWith = [];
protected $storeJobMethod = 'create';
protected $updateJobMethod = 'update';
protected $deleteJobMethod = "delete";
protected $jobMethod;
protected $jobEvent;
protected $jobRepository;
protected $createJob;
protected $updateJob;
protected $deleteJob;
protected $showRequest;
protected $storeRequest;
protected $updateRequest;
protected $deleteRequest;
protected $repository;
protected $request;
protected $transformer;
protected $notImplemented = [];
protected $customRequest;
protected $customMessage = null;
protected $defaultMessage = "Resource created/Updated";
protected $resourceName = "";
/**
* Prepare Repository
* BaseController constructor.
*/
public function __construct()
{
if ($this->repository) {
$this->repo = new $this->repository;
if ($this->repo->model) {
$this->model = ($this->repo)->model;
}
}
}
/**
* global index function . return all data of Specific Model.
* @param Request $request
* @return \Illuminate\Http\JsonResponse
* @throws \Exception
*/
public function index(Request $request)
{
$inputs = array_replace_recursive(
$request->all(),
$request->route()->parameters()
);
if ($this->isCamelToSnake) {
$inputs = UtilityService::fromCamelToSnake($inputs);
if (isset($inputs['date_filter_column'])) {
// i.e. if custom date_filter_column is passed from frontend, then transform it
$inputs['date_filter_column'] = Str::snake($inputs['date_filter_column']);
}
}
$result = $this->repo->{$this->indexCall}(["with" => $this->indexWith, "inputs" => $inputs]);
return $this->standardResponse($result);
}
/**
* @param $data
* @param null $message
* @param int $httpCode
* @param null $type
* @return \Illuminate\Http\JsonResponse
*/
public function standardResponse($data, $message = null, $httpCode = 200, $type = null)
{
if ($httpCode == 200 && $data && $this->isSnakeToCamel && (is_array($data) || is_object($data))) {
$data = UtilityService::fromSnakeToCamel(json_decode(json_encode($data), true));
}
return response()->json([
"message" => $message,
"data" => $data && ($data instanceof Collection) ? $data->toArray() : $data,
"type" => $type
], $httpCode);
}
/**
* global show method , return selected $id row from Specific Model
* @param Request $request
* @param $id
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function show(Request $request, $id)
{
if ($this->showRequest && $response = $this->validateRequest($this->showRequest)) return $response;
$result = $this->repo->{$this->showCall}($id, ["with" => $this->showWith]);
if ($this->transformer) {
$transformer = app()->make($this->transformer);
$result = $transformer->transform($result);
}
return $this->standardResponse($result);
}
/**
* @param $method
* @return bool|\Illuminate\Http\JsonResponse
*/
protected function validateRequest($method)
{
/**
* create request object
*/
$this->request = app($method);
$validator = $this->request->getValidator();
/**
* check request is valid ?
*/
if ($validator->fails()) {
return $this->standardResponse(null, $validator->errors()->messages(), 400, ErrorConstants::TYPE_VALIDATION_ERROR);
}
return false;
}
/**
* @param Request $request
* @return bool|\Illuminate\Http\JsonResponse
*/
public function store(Request $request)
{
$this->defaultMessage = $this->resourceName ? $this->resourceName . " created successfully" : "Resource Created successfully";
$this->jobMethod = $this->storeJobMethod ?? 'create';
if (!$this->createJob) throw new MethodNotAllowedHttpException(['message' => 'Method Not Allowed', 'code' => 405]);
$data = array_replace_recursive(
$request->json()->all(),
$request->route()->parameters()
);
if ($this->storeRequest && $response = $this->validateRequest($this->storeRequest)) return $response;
if ($this->isCamelToSnake) {
$data = UtilityService::fromCamelToSnake($data);
}
$data = $this->requestSanitizer($data, 'createExcept');
return $this->executeJob($request, $this->createJob, [
'data' => $data,
]);
}
/**
* @param $data
* @param $modelKey
* @return mixed
*/
private function requestSanitizer($data, $modelKey)
{
// removes keys which cannot be updated as defined in model class variable
$exceptionKeys = [];
if ($this->model) {
$_model = new $this->model;
$exceptionKeys = property_exists($_model, $modelKey) ? ($_model)->{$modelKey} : [];
}
if (count((array)$exceptionKeys)) {
foreach ($exceptionKeys as $key) {
if (array_key_exists($key, $data)) unset($data[$key]);
}
}
return $data;
}
/**
* @param $request
* @param $jobClass
* @param $params
* @return \Illuminate\Http\JsonResponse
*/
protected function executeJob($request, $jobClass, $params)
{
$job = new $jobClass($params);
if ($jobClass === BaseJob::class) {
$job->method = $this->jobMethod;
$job->event = $this->jobEvent;
$job->repository = $this->jobRepository ? $this->jobRepository : $this->repository;
}
return $this->dispatchJob($request, $job, $params);
}
/**
* @param $request
* @param $job
* @param $params
* @return \Illuminate\Http\JsonResponse
*/
protected function dispatchJob($request, $job, $params)
{
$result = $this->dispatchSync($job);
return $this->standardResponse($result, $this->customMessage ? $this->customMessage : $this->defaultMessage);
}
public function __call($method, $arguments)
{
parent::__call($method, $arguments);
}
/**
* @param Request $request
* @param $id
* @return bool|\Illuminate\Http\JsonResponse
*/
public function update(Request $request, $id)
{
$this->defaultMessage = $this->resourceName ? $this->resourceName . " updated successfully" : "Resource Updated successfully";
$this->jobMethod = $this->updateJobMethod ?? 'update';
if (!$this->updateJob) throw new MethodNotAllowedHttpException(['message' => 'Method Not Allowed', 'code' => 405]);
$data = array_replace_recursive(
$request->json()->all(),
$request->route()->parameters()
);
if ($this->updateRequest && $response = $this->validateRequest($this->updateRequest)) return $response;
if ($this->isCamelToSnake) {
$data = UtilityService::fromCamelToSnake($data);
}
$data = $this->requestSanitizer($data, 'updateExcept');
return $this->executeJob($request, $this->updateJob, [
'data' => $data,
'id' => $id
]);
}
/**
* @param Request $request
* @param $id
* @return bool|\Illuminate\Http\JsonResponse
* @throws MethodNotAllowedHttpException
*/
public function destroy(Request $request, $id)
{
$this->defaultMessage = $this->resourceName ? $this->resourceName . " deleted successfully" : "Resource deleted successfully";
$this->jobMethod = $this->deleteJobMethod ?? 'delete';
if (!$this->deleteJob) throw new MethodNotAllowedHttpException(['message' => 'Method Not Allowed', 'code' => 405]);
if ($this->deleteRequest && $response = $this->validateRequest($this->deleteRequest)) return $response;
return $this->executeJob($request, $this->deleteJob, [
'id' => $id,
'data' => []
]);
}
/**
* @return \Illuminate\Contracts\Auth\Authenticatable|\Illuminate\Http\JsonResponse|null
*/
public function getUserByToken()
{
$user = Auth::user();
if (!$user) {
return $this->standardResponse(null, "Invalid token. No user found.", 401, ErrorConstants::TYPE_AUTHORIZATION_ERROR);
}
return $user;
}
/**
* Method which can be used as substitute for the Custom POST/PUT routes other than the resource route
* @param $job
* @param Request|null $request
* @param array|null $additionalData
* @return bool|\Illuminate\Http\JsonResponse
*/
public function handleCustomEndPoint($job, $request = null, $additionalData = null)
{
if ($this->customRequest && $response = $this->validateRequest($this->customRequest)) return $response;
$data = [];
if ($request) {
$requestData = array_replace_recursive(
$request->json()->all(),
$request->route()->parameters()
);
$data = UtilityService::fromCamelToSnake($requestData);
}
return $this->executeJob($request, $job, [
'data' => $data,
'additionalData' => $additionalData
]);
}
/**
* Method which can be used as substitute for the Custom GET(index) route other than the resource route
* @param $job
* @param Request|null $request
* @param array $additionalData
* @return \Illuminate\Http\JsonResponse
*/
public function handleCustomEndPointGet($job, $request, $additionalData = [])
{
$this->defaultMessage = null;
if ($this->customRequest && $response = $this->validateRequest($this->customRequest)) return $response;
$inputs = [];
if ($request) {
$inputs = array_replace_recursive(
$request->all(),
$request->route()->parameters()
);
}
if ($this->isCamelToSnake) {
$inputs = UtilityService::fromCamelToSnake($inputs);
}
return $this->executeJob($request, $job, ["inputs" => $inputs, "additionalData" => $additionalData]);
}
/**
* * Method which can be used as substitute for the Custom GET(show) route other than the resource route
* @param $job
* @param $request
* @param $column
* @param $value
* @return \Illuminate\Http\JsonResponse
*/
public function handleCustomEndPointShow($job, $request, $column, $value)
{
$this->defaultMessage = null;
if ($this->customRequest && $response = $this->validateRequest($this->customRequest)) return $response;
$params = [
'inputs' => [
$column => $value
]
];
if ($request) {
$params['inputs'] = array_merge($params['inputs'], $request->all());
}
return $this->executeJob($request, $job, $params);
}
/**
* @return int|null
*/
protected function getLoggedInUserId()
{
return EnvironmentService::getLoggedInUserId();
}
/**
* @return null|object
*/
protected function getLoggedInUser()
{
return EnvironmentService::getLoggedInUser();
}
protected function notImplemented($data, $message = null)
{
return $this->standardResponse($data, $message);
}
}