From a603aca80cb8d86c9139d4d83f40039675e5a2fb Mon Sep 17 00:00:00 2001 From: philipp lang Date: Fri, 12 Jul 2024 20:15:24 +0200 Subject: [PATCH] Add backend for update --- app/Form/Actions/ParticipantUpdateAction.php | 29 +++++++++++++++++ app/Form/Resources/ParticipantResource.php | 1 + routes/web.php | 2 ++ .../Form/ParticipantIndexActionTest.php | 1 + .../Form/ParticipantUpdateActionTest.php | 31 +++++++++++++++++++ 5 files changed, 64 insertions(+) create mode 100644 app/Form/Actions/ParticipantUpdateAction.php create mode 100644 tests/Feature/Form/ParticipantUpdateActionTest.php diff --git a/app/Form/Actions/ParticipantUpdateAction.php b/app/Form/Actions/ParticipantUpdateAction.php new file mode 100644 index 00000000..df57b641 --- /dev/null +++ b/app/Form/Actions/ParticipantUpdateAction.php @@ -0,0 +1,29 @@ + 'required', + ]; + } + + public function handle(Participant $participant, ActionRequest $request): JsonResponse + { + $participant->update(['data' => [...$participant->data, ...$request->validated('data')]]); + + Succeeded::message('Teilnehmer*in bearbeitet.')->dispatch(); + return response()->json([]); + } +} diff --git a/app/Form/Resources/ParticipantResource.php b/app/Form/Resources/ParticipantResource.php index 9ad96f18..95ec8556 100644 --- a/app/Form/Resources/ParticipantResource.php +++ b/app/Form/Resources/ParticipantResource.php @@ -33,6 +33,7 @@ class ParticipantResource extends JsonResource 'destroy' => route('participant.destroy', ['participant' => $this->getModel()]), 'children' => route('form.participant.index', ['form' => $this->form, 'parent' => $this->id]), 'fields' => route('participant.fields', ['participant' => $this->getModel()]), + 'update' => route('participant.update', ['participant' => $this->getModel()]), ] ]; } diff --git a/routes/web.php b/routes/web.php index 7184b80b..4e14819e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -40,6 +40,7 @@ use App\Form\Actions\ParticipantAssignAction; use App\Form\Actions\ParticipantDestroyAction; use App\Form\Actions\ParticipantFieldsAction; use App\Form\Actions\ParticipantIndexAction; +use App\Form\Actions\ParticipantUpdateAction; use App\Initialize\Actions\InitializeAction; use App\Initialize\Actions\InitializeFormAction; use App\Initialize\Actions\NamiGetSearchLayerAction; @@ -174,6 +175,7 @@ Route::group(['middleware' => 'auth:web'], function (): void { Route::delete('/participant/{participant}', ParticipantDestroyAction::class)->name('participant.destroy'); Route::post('/participant/{participant}/assign', ParticipantAssignAction::class)->name('participant.assign'); Route::get('/participant/{participant}/fields', ParticipantFieldsAction::class)->name('participant.fields'); + Route::patch('/participant/{participant}', ParticipantUpdateAction::class)->name('participant.update'); // ------------------------------------ fileshare ----------------------------------- Route::post('/fileshare', FileshareStoreAction::class)->name('fileshare.store'); diff --git a/tests/Feature/Form/ParticipantIndexActionTest.php b/tests/Feature/Form/ParticipantIndexActionTest.php index d74507a5..65d5446c 100644 --- a/tests/Feature/Form/ParticipantIndexActionTest.php +++ b/tests/Feature/Form/ParticipantIndexActionTest.php @@ -49,6 +49,7 @@ class ParticipantIndexActionTest extends FormTestCase ->assertJsonPath('data.0.links.destroy', route('participant.destroy', ['participant' => $form->participants->first()])) ->assertJsonPath('data.0.links.assign', route('participant.assign', ['participant' => $form->participants->first()])) ->assertJsonPath('data.0.links.fields', route('participant.fields', ['participant' => $form->participants->first()])) + ->assertJsonPath('data.0.links.update', route('participant.update', ['participant' => $form->participants->first()])) ->assertJsonPath('meta.columns.0.name', 'Vorname') ->assertJsonPath('meta.columns.0.base_type', class_basename(TextField::class)) ->assertJsonPath('meta.columns.0.id', 'vorname') diff --git a/tests/Feature/Form/ParticipantUpdateActionTest.php b/tests/Feature/Form/ParticipantUpdateActionTest.php new file mode 100644 index 00000000..a4dddb32 --- /dev/null +++ b/tests/Feature/Form/ParticipantUpdateActionTest.php @@ -0,0 +1,31 @@ +login()->loginNami()->withoutExceptionHandling(); + $participant = Participant::factory()->data(['vorname' => 'Max', 'select' => ['A', 'B']]) + ->for(Form::factory()->sections([ + FormtemplateSectionRequest::new()->name('Sektion')->fields([ + $this->textField('vorname')->name('Vorname'), + $this->checkboxesField('select')->options(['A', 'B', 'C']), + ]) + ])) + ->create(); + + $this->patchJson(route('participant.update', ['participant' => $participant->id]), ['data' => ['vorname' => 'Jane']]) + ->assertOk(); + + $this->assertEquals('Jane', $participant->fresh()->data['vorname']); + } +}