diff --git a/app/Mailgateway/Actions/StoreAction.php b/app/Mailgateway/Actions/StoreAction.php index 5a2e6fbf..41194c6a 100644 --- a/app/Mailgateway/Actions/StoreAction.php +++ b/app/Mailgateway/Actions/StoreAction.php @@ -3,72 +3,23 @@ namespace App\Mailgateway\Actions; use App\Mailgateway\Models\Mailgateway; -use App\Mailgateway\Types\Type; -use Illuminate\Validation\Rule; -use Illuminate\Validation\ValidationException; use Lorisleiva\Actions\ActionRequest; use Lorisleiva\Actions\Concerns\AsAction; class StoreAction { use AsAction; + use ValidatesRequests; /** * @param array $input */ public function handle(array $input): void { - if (!app($input['type']['cls'])->setParams($input['type']['params'])->works()) { - throw ValidationException::withMessages(['connection' => 'Verbindung fehlgeschlagen.']); - } - + $this->checkIfWorks($input); Mailgateway::create($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(ActionRequest $request): void { $this->handle($request->validated()); diff --git a/app/Mailgateway/Actions/UpdateAction.php b/app/Mailgateway/Actions/UpdateAction.php index 90db2638..4b794520 100644 --- a/app/Mailgateway/Actions/UpdateAction.php +++ b/app/Mailgateway/Actions/UpdateAction.php @@ -3,72 +3,24 @@ namespace App\Mailgateway\Actions; use App\Mailgateway\Models\Mailgateway; -use App\Mailgateway\Types\Type; -use Illuminate\Validation\Rule; -use Illuminate\Validation\ValidationException; use Lorisleiva\Actions\ActionRequest; use Lorisleiva\Actions\Concerns\AsAction; class UpdateAction { use AsAction; + use ValidatesRequests; /** * @param array $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.']); - } + $this->checkIfWorks($input); $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/app/Mailgateway/Actions/ValidatesRequests.php b/app/Mailgateway/Actions/ValidatesRequests.php new file mode 100644 index 00000000..8397ea3b --- /dev/null +++ b/app/Mailgateway/Actions/ValidatesRequests.php @@ -0,0 +1,62 @@ +setParams($input['type']['params'])->works()) { + throw ValidationException::withMessages(['connection' => 'Verbindung fehlgeschlagen.']); + } + } + + /** + * @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.']); + } + } +} diff --git a/app/Mailgateway/Resources/MailgatewayResource.php b/app/Mailgateway/Resources/MailgatewayResource.php index 9d22e10f..3328a867 100644 --- a/app/Mailgateway/Resources/MailgatewayResource.php +++ b/app/Mailgateway/Resources/MailgatewayResource.php @@ -28,6 +28,10 @@ class MailgatewayResource extends JsonResource 'type_human' => $this->type::name(), 'works' => $this->type->works(), 'type' => $this->type->toResource(), + 'id' => $this->id, + 'links' => [ + 'update' => route('mailgateway.update', ['mailgateway' => $this->getModel()]), + ], ]; } diff --git a/resources/js/views/mailgateway/Index.vue b/resources/js/views/mailgateway/Index.vue index c7bce639..d06857db 100644 --- a/resources/js/views/mailgateway/Index.vue +++ b/resources/js/views/mailgateway/Index.vue @@ -3,7 +3,7 @@ - +
@@ -101,7 +101,7 @@ export default { }, async submit() { try { - await this.axios.post(this.data.meta.links.store, this.model); + await this.axios[this.model.id ? 'patch' : 'post'](this.model.id ? this.model.links.update : this.data.meta.links.store, this.model); this.reload(); this.model = null; diff --git a/tests/Feature/Mailgateway/IndexTest.php b/tests/Feature/Mailgateway/IndexTest.php index 5e378896..ff13ba47 100644 --- a/tests/Feature/Mailgateway/IndexTest.php +++ b/tests/Feature/Mailgateway/IndexTest.php @@ -32,7 +32,7 @@ class IndexTest extends TestCase public function testItDisplaysLocalGateways(): void { $this->withoutExceptionHandling(); - Mailgateway::factory()->type(LocalType::class, [])->name('Lore')->domain('example.com')->create(); + $mailgateway = Mailgateway::factory()->type(LocalType::class, [])->name('Lore')->domain('example.com')->create(); $response = $this->get('/setting/mailgateway'); @@ -40,6 +40,8 @@ class IndexTest extends TestCase $this->assertInertiaHas('Lore', $response, 'data.data.0.name'); $this->assertInertiaHas('Lokal', $response, 'data.data.0.type_human'); $this->assertInertiaHas(true, $response, 'data.data.0.works'); + $this->assertInertiaHas($mailgateway->id, $response, 'data.data.0.id'); + $this->assertInertiaHas(route('mailgateway.update', ['mailgateway' => $mailgateway->id]), $response, 'data.data.0.links.update'); } public function testItDisplaysMailmanGateways(): void