Skip to content

Commit 00c6451

Browse files
ngaspariStyleCIBot
andauthored
Vsliv30 3891 restore existing selection value (#94)
* restore deleted - because we have unique index * Apply fixes from StyleCI * Update SelectionValueController.php test * Update SelectionValueController.php . --------- Co-authored-by: StyleCI Bot <[email protected]>
1 parent 5669d7e commit 00c6451

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/App/Http/Controllers/SelectionValueController.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,23 @@ public function index(): JsonResponse
4040
*/
4141
public function store(SelectionValueRequest $request): JsonResponse
4242
{
43-
$selectionValue = $this->selectionValue::query()->create($request->validated());
43+
if (method_exists($this->selectionValue, 'bootSoftDeletes')) {
44+
// check for deleted values
45+
$selectionValue = $this->selectionValue::withTrashed()
46+
->where('selection_type_id', $request->get('selection_type_id'))
47+
->where('value', $request->get('value'))
48+
->first();
49+
50+
if ($selectionValue->trashed()) {
51+
// restore
52+
$selectionValue->restoreQuietly();
53+
$selectionValue->update($request->validated());
54+
} else {
55+
throw new Exception('Selection value already exists.', 400);
56+
}
57+
} else {
58+
$selectionValue = $this->selectionValue::query()->create($request->validated());
59+
}
4460

4561
return response()->json($selectionValue->refresh());
4662
}

0 commit comments

Comments
 (0)