-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourceController.php
93 lines (79 loc) · 3.28 KB
/
ResourceController.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
<?php
namespace App\Http\Controllers;
use App\Requests\ResourceRequest;
use App\Support\DbCrudMap;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
use MacropaySolutions\LaravelCrudWizard\Exceptions\CrudValidationException;
use MacropaySolutions\LaravelCrudWizard\Helpers\GeneralHelper;
use MacropaySolutions\LaravelCrudWizard\Http\Controllers\ResourceControllerTrait;
class ResourceController extends Controller
{
use ResourceControllerTrait;
/**
* @throws \Throwable
*/
public function __construct()
{
$this->init(100);
}
public function update(string $identifier, Request $request): JsonResponse
{
try {
$this->throwIfForbidden($this->forbidUpdate);
$all = $request->all();
try {
$model = $this->resourceService->get($identifier, appendIndex: false);
$increments = $this->getValidIncrementsFromRequest($model, $all);
($clone = clone $model)->fill(GeneralHelper::filterDataByKeys($all, \array_diff(
$this->resourceService->getModelColumns(),
$this->resourceService->getIgnoreExternalUpdateFor(),
\array_keys($increments)
)));
/** $request can contain also files so, overwrite this function to handle them */
$request->replace($clone->getDirty());
$validatedPayload = $this->validateUpdateRequest($request);
$baseModel = $increments !== [] ?
$this->resourceService->incrementBulk($model, $increments, $validatedPayload) :
$this->resourceService->update($identifier, $validatedPayload);
return \response()->json($this->loadRelations(
$request->replace($all),
$baseModel
));
} catch (ModelNotFoundException $e) {
if (!$this->resourceService->isUpdateOrCreateAble($all)) {
throw $e;
}
$request->replace($all);
$request->server->set('REQUEST_METHOD', 'POST');
return $this->create($request);
}
} catch (ValidationException |\Transactions\Crud\Exceptions\CrudValidationException $e) {
return \response()->json(['message' => $e->getMessage(), 'errors' => $e->errors()], 400);
} catch (\Throwable $e) {
Log::error($this->label . ' update for identifier: ' . $identifier . ', error = ' . $e->getMessage());
return \response()->json(['message' => GeneralHelper::getSafeErrorMessage($e)], 400);
}
}
protected function setModelFqnToControllerMap(): void
{
$this->modelFqnToControllerMap = DbCrudMap::MODEL_FQN_TO_CONTROLLER_MAP;
}
/**
* @throws \Throwable
*/
protected function validateCreateRequest(Request $request): array
{
return \resolve(ResourceRequest::class)->validated();
}
/**
* @throws \Throwable
*/
protected function validateUpdateRequest(Request $request): array
{
return $this->validateCreateRequest($request);
}
}