Update mailgateway
This commit is contained in:
parent
8b2bdae5d6
commit
b54472c14e
|
@ -3,72 +3,23 @@
|
||||||
namespace App\Mailgateway\Actions;
|
namespace App\Mailgateway\Actions;
|
||||||
|
|
||||||
use App\Mailgateway\Models\Mailgateway;
|
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\ActionRequest;
|
||||||
use Lorisleiva\Actions\Concerns\AsAction;
|
use Lorisleiva\Actions\Concerns\AsAction;
|
||||||
|
|
||||||
class StoreAction
|
class StoreAction
|
||||||
{
|
{
|
||||||
use AsAction;
|
use AsAction;
|
||||||
|
use ValidatesRequests;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array<string, mixed> $input
|
* @param array<string, mixed> $input
|
||||||
*/
|
*/
|
||||||
public function handle(array $input): void
|
public function handle(array $input): void
|
||||||
{
|
{
|
||||||
if (!app($input['type']['cls'])->setParams($input['type']['params'])->works()) {
|
$this->checkIfWorks($input);
|
||||||
throw ValidationException::withMessages(['connection' => 'Verbindung fehlgeschlagen.']);
|
|
||||||
}
|
|
||||||
|
|
||||||
Mailgateway::create($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
|
public function asController(ActionRequest $request): void
|
||||||
{
|
{
|
||||||
$this->handle($request->validated());
|
$this->handle($request->validated());
|
||||||
|
|
|
@ -3,72 +3,24 @@
|
||||||
namespace App\Mailgateway\Actions;
|
namespace App\Mailgateway\Actions;
|
||||||
|
|
||||||
use App\Mailgateway\Models\Mailgateway;
|
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\ActionRequest;
|
||||||
use Lorisleiva\Actions\Concerns\AsAction;
|
use Lorisleiva\Actions\Concerns\AsAction;
|
||||||
|
|
||||||
class UpdateAction
|
class UpdateAction
|
||||||
{
|
{
|
||||||
use AsAction;
|
use AsAction;
|
||||||
|
use ValidatesRequests;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array<string, mixed> $input
|
* @param array<string, mixed> $input
|
||||||
*/
|
*/
|
||||||
public function handle(Mailgateway $mailgateway, array $input): void
|
public function handle(Mailgateway $mailgateway, array $input): void
|
||||||
{
|
{
|
||||||
if (!app($input['type']['cls'])->setParams($input['type']['params'])->works()) {
|
$this->checkIfWorks($input);
|
||||||
throw ValidationException::withMessages(['connection' => 'Verbindung fehlgeschlagen.']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$mailgateway->update($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
|
public function asController(Mailgateway $mailgateway, ActionRequest $request): void
|
||||||
{
|
{
|
||||||
$this->handle($mailgateway, $request->validated());
|
$this->handle($mailgateway, $request->validated());
|
||||||
|
|
|
@ -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.']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -28,6 +28,10 @@ class MailgatewayResource extends JsonResource
|
||||||
'type_human' => $this->type::name(),
|
'type_human' => $this->type::name(),
|
||||||
'works' => $this->type->works(),
|
'works' => $this->type->works(),
|
||||||
'type' => $this->type->toResource(),
|
'type' => $this->type->toResource(),
|
||||||
|
'id' => $this->id,
|
||||||
|
'links' => [
|
||||||
|
'update' => route('mailgateway.update', ['mailgateway' => $this->getModel()]),
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
<template #toolbar>
|
<template #toolbar>
|
||||||
<page-toolbar-button @click.prevent="model = {...data.meta.default}" color="primary" icon="plus">Neue Verbindung</page-toolbar-button>
|
<page-toolbar-button @click.prevent="model = {...data.meta.default}" color="primary" icon="plus">Neue Verbindung</page-toolbar-button>
|
||||||
</template>
|
</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">
|
<form @submit.prevent="submit">
|
||||||
<section class="grid grid-cols-2 gap-3 mt-6">
|
<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>
|
<f-text v-model="model.name" name="name" id="name" label="Bezeichnung" required></f-text>
|
||||||
|
@ -101,7 +101,7 @@ export default {
|
||||||
},
|
},
|
||||||
async submit() {
|
async submit() {
|
||||||
try {
|
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.reload();
|
||||||
this.model = null;
|
this.model = null;
|
||||||
|
|
|
@ -32,7 +32,7 @@ class IndexTest extends TestCase
|
||||||
public function testItDisplaysLocalGateways(): void
|
public function testItDisplaysLocalGateways(): void
|
||||||
{
|
{
|
||||||
$this->withoutExceptionHandling();
|
$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');
|
$response = $this->get('/setting/mailgateway');
|
||||||
|
|
||||||
|
@ -40,6 +40,8 @@ class IndexTest extends TestCase
|
||||||
$this->assertInertiaHas('Lore', $response, 'data.data.0.name');
|
$this->assertInertiaHas('Lore', $response, 'data.data.0.name');
|
||||||
$this->assertInertiaHas('Lokal', $response, 'data.data.0.type_human');
|
$this->assertInertiaHas('Lokal', $response, 'data.data.0.type_human');
|
||||||
$this->assertInertiaHas(true, $response, 'data.data.0.works');
|
$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
|
public function testItDisplaysMailmanGateways(): void
|
||||||
|
|
Loading…
Reference in New Issue