Update mailgateway

This commit is contained in:
philipp lang 2023-06-08 00:14:55 +02:00 committed by Philipp Lang
parent 8b2bdae5d6
commit b54472c14e
6 changed files with 75 additions and 104 deletions

View File

@ -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<string, mixed> $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<string, mixed>
*/
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<string, mixed>
*/
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<string, mixed>
*/
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());

View File

@ -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<string, mixed> $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<string, mixed>
*/
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<string, mixed>
*/
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<string, mixed>
*/
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());

View File

@ -0,0 +1,62 @@
<?php
namespace App\Mailgateway\Actions;
use App\Mailgateway\Types\Type;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Lorisleiva\Actions\ActionRequest;
trait ValidatesRequests
{
public function checkIfWorks(array $input): void
{
if (!app(data_get($input, 'type.cls'))->setParams($input['type']['params'])->works()) {
throw ValidationException::withMessages(['connection' => 'Verbindung fehlgeschlagen.']);
}
}
/**
* @return array<string, mixed>
*/
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<string, mixed>
*/
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<string, mixed>
*/
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.']);
}
}
}

View File

@ -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()]),
],
];
}

View File

@ -3,7 +3,7 @@
<template #toolbar>
<page-toolbar-button @click.prevent="model = {...data.meta.default}" color="primary" icon="plus">Neue Verbindung</page-toolbar-button>
</template>
<ui-popup heading="Neue Verbindung" v-if="model !== null && !model.id" @close="model = null">
<ui-popup :heading="model.id ? 'Verbindung bearbeiten' : 'Neue Verbindung'" v-if="model !== null" @close="model = null">
<form @submit.prevent="submit">
<section class="grid grid-cols-2 gap-3 mt-6">
<f-text v-model="model.name" name="name" id="name" label="Bezeichnung" required></f-text>
@ -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;

View File

@ -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