From d19b11bd90ae2d19ba96dd54c1bef571fd977f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Ga=C5=A1pari=C4=87?= Date: Wed, 28 Aug 2024 14:49:42 +0200 Subject: [PATCH 1/4] restore deleted - because we have unique index --- .../Controllers/SelectionValueController.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/App/Http/Controllers/SelectionValueController.php b/src/App/Http/Controllers/SelectionValueController.php index 4693616..3a9600b 100644 --- a/src/App/Http/Controllers/SelectionValueController.php +++ b/src/App/Http/Controllers/SelectionValueController.php @@ -40,7 +40,24 @@ public function index(): JsonResponse */ public function store(SelectionValueRequest $request): JsonResponse { - $selectionValue = $this->selectionValue::query()->create($request->validated()); + // 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) { + 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()); } From d90d745b949dc6972794db5fec53e01a28847845 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 28 Aug 2024 12:50:02 +0000 Subject: [PATCH 2/4] Apply fixes from StyleCI --- src/App/Http/Controllers/SelectionValueController.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/App/Http/Controllers/SelectionValueController.php b/src/App/Http/Controllers/SelectionValueController.php index 3a9600b..9413f63 100644 --- a/src/App/Http/Controllers/SelectionValueController.php +++ b/src/App/Http/Controllers/SelectionValueController.php @@ -54,8 +54,7 @@ public function store(SelectionValueRequest $request): JsonResponse } else { throw new Exception('Selection value already exists.', 400); } - } - else { + } else { $selectionValue = $this->selectionValue::query()->create($request->validated()); } From c203a523be19dca8d9d277761f757cd9ce3456fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Ga=C5=A1pari=C4=87?= Date: Wed, 28 Aug 2024 15:07:55 +0200 Subject: [PATCH 3/4] Update SelectionValueController.php test --- .../Http/Controllers/SelectionValueController.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/App/Http/Controllers/SelectionValueController.php b/src/App/Http/Controllers/SelectionValueController.php index 9413f63..f2574de 100644 --- a/src/App/Http/Controllers/SelectionValueController.php +++ b/src/App/Http/Controllers/SelectionValueController.php @@ -41,10 +41,14 @@ public function index(): JsonResponse public function store(SelectionValueRequest $request): JsonResponse { // check for deleted values - $selectionValue = $this->selectionValue::withTrashed() - ->where('selection_type_id', $request->get('selection_type_id')) - ->where('value', $request->get('value')) - ->first(); + + $selectionValue = null; + if (method_exists($this->selectionValue, 'bootSoftDeletes')) { + $selectionValue = $this->selectionValue::withTrashed() + ->where('selection_type_id', $request->get('selection_type_id')) + ->where('value', $request->get('value')) + ->first(); + } if ($selectionValue) { if ($selectionValue->trashed()) { From 716780d02b30361af82b2dbbf180ee3a8c6624ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Ga=C5=A1pari=C4=87?= Date: Wed, 28 Aug 2024 15:14:15 +0200 Subject: [PATCH 4/4] Update SelectionValueController.php . --- src/App/Http/Controllers/SelectionValueController.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/App/Http/Controllers/SelectionValueController.php b/src/App/Http/Controllers/SelectionValueController.php index f2574de..7a5c7b7 100644 --- a/src/App/Http/Controllers/SelectionValueController.php +++ b/src/App/Http/Controllers/SelectionValueController.php @@ -40,17 +40,13 @@ public function index(): JsonResponse */ public function store(SelectionValueRequest $request): JsonResponse { - // check for deleted values - - $selectionValue = null; 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) { if ($selectionValue->trashed()) { // restore $selectionValue->restoreQuietly();