Skip to content

Commit

Permalink
Vsliv30 3891 restore existing selection value (#94)
Browse files Browse the repository at this point in the history
* restore deleted

- because we have unique index

* Apply fixes from StyleCI

* Update SelectionValueController.php

test

* Update SelectionValueController.php

.

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
ngaspari and StyleCIBot authored Aug 29, 2024
1 parent 5669d7e commit 00c6451
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/App/Http/Controllers/SelectionValueController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,23 @@ public function index(): JsonResponse
*/
public function store(SelectionValueRequest $request): JsonResponse
{
$selectionValue = $this->selectionValue::query()->create($request->validated());
if (method_exists($this->selectionValue, 'bootSoftDeletes')) {
// check for deleted values
$selectionValue = $this->selectionValue::withTrashed()
->where('selection_type_id', $request->get('selection_type_id'))
->where('value', $request->get('value'))
->first();

if ($selectionValue->trashed()) {
// restore
$selectionValue->restoreQuietly();
$selectionValue->update($request->validated());
} else {
throw new Exception('Selection value already exists.', 400);
}
} else {
$selectionValue = $this->selectionValue::query()->create($request->validated());
}

return response()->json($selectionValue->refresh());
}
Expand Down

0 comments on commit 00c6451

Please sign in to comment.