From 8b2bdae5d69514e4120fe48582868c05609993b3 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Thu, 8 Jun 2023 00:00:35 +0200 Subject: [PATCH] Add update for mailgateway --- app/Mailgateway/Actions/UpdateAction.php | 76 ++++++++++++++++++++++++ routes/web.php | 2 + tests/Feature/Mailgateway/UpdateTest.php | 31 ++++++++++ 3 files changed, 109 insertions(+) create mode 100644 app/Mailgateway/Actions/UpdateAction.php create mode 100644 tests/Feature/Mailgateway/UpdateTest.php diff --git a/app/Mailgateway/Actions/UpdateAction.php b/app/Mailgateway/Actions/UpdateAction.php new file mode 100644 index 00000000..90db2638 --- /dev/null +++ b/app/Mailgateway/Actions/UpdateAction.php @@ -0,0 +1,76 @@ + $input + */ + public function handle(Mailgateway $mailgateway, array $input): void + { + if (!app($input['type']['cls'])->setParams($input['type']['params'])->works()) { + throw ValidationException::withMessages(['connection' => 'Verbindung fehlgeschlagen.']); + } + + $mailgateway->update($input); + } + + /** + * @return array + */ + public function rules(): array + { + return [ + 'name' => 'required|string|max:255', + 'domain' => 'required|string|max:255', + ...$this->typeValidation(), + 'type.params' => 'present|array', + ...collect(request()->input('type.cls')::rules('storeValidator'))->mapWithKeys(fn ($rules, $key) => ["type.params.{$key}" => $rules]), + ]; + } + + /** + * @return array + */ + public function getValidationAttributes(): array + { + return [ + 'type.cls' => 'Typ', + 'name' => 'Beschreibung', + 'domain' => 'Domain', + ...collect(request()->input('type.cls')::fieldNames())->mapWithKeys(fn ($attribute, $key) => ["type.params.{$key}" => $attribute]), + ]; + } + + /** + * @return array + */ + private function typeValidation(): array + { + return [ + 'type.cls' => ['required', 'string', 'max:255', Rule::in(app('mail-gateways'))], + ]; + } + + public function prepareForValidation(ActionRequest $request): void + { + if (!is_subclass_of(request()->input('type.cls'), Type::class)) { + throw ValidationException::withMessages(['type.cls' => 'Typ ist nicht valide.']); + } + } + + public function asController(Mailgateway $mailgateway, ActionRequest $request): void + { + $this->handle($mailgateway, $request->validated()); + } +} diff --git a/routes/web.php b/routes/web.php index 8f69cb24..ef12a4d1 100644 --- a/routes/web.php +++ b/routes/web.php @@ -21,6 +21,7 @@ use App\Initialize\Actions\NamiGetSearchLayerAction; use App\Initialize\Actions\NamiLoginCheckAction; use App\Initialize\Actions\NamiSearchAction; use App\Mailgateway\Actions\StoreAction; +use App\Mailgateway\Actions\UpdateAction; use App\Member\Actions\ExportAction; use App\Member\Actions\MemberResyncAction; use App\Member\Actions\MemberShowAction; @@ -82,4 +83,5 @@ Route::group(['middleware' => 'auth:web'], function (): void { // -------------------------------- Mailgateway -------------------------------- Route::post('/api/mailgateway', StoreAction::class)->name('mailgateway.store'); + Route::patch('/api/mailgateway/{mailgateway}', UpdateAction::class)->name('mailgateway.update'); }); diff --git a/tests/Feature/Mailgateway/UpdateTest.php b/tests/Feature/Mailgateway/UpdateTest.php new file mode 100644 index 00000000..38257108 --- /dev/null +++ b/tests/Feature/Mailgateway/UpdateTest.php @@ -0,0 +1,31 @@ +login()->loginNami(); + } + + public function testItCanUpdateALocalGateway(): void + { + $mailgateway = Mailgateway::factory()->type(LocalType::class, [])->create(); + $response = $this->patchJson("/api/mailgateway/{$mailgateway->id}", MailgatewayRequestFactory::new()->name('lala')->type(LocalType::class, [])->domain('example.com')->create()); + + $response->assertOk(); + + $this->assertDatabaseCount('mailgateways', 1); + } +}