Add IndexAction for forms

This commit is contained in:
philipp lang 2023-12-31 22:35:13 +01:00
parent d3ad48ed30
commit 743c041c35
7 changed files with 137 additions and 7 deletions

View File

@ -0,0 +1,30 @@
<?php
namespace App\Form\Actions;
use App\Form\Models\Form;
use App\Form\Resources\FormResource;
use Illuminate\Pagination\LengthAwarePaginator;
use Inertia\Inertia;
use Inertia\Response;
use Lorisleiva\Actions\Concerns\AsAction;
class FormIndexAction
{
use AsAction;
/**
* @return LengthAwarePaginator<Form>
*/
public function handle(): LengthAwarePaginator
{
return Form::paginate(15);
}
public function asController(): Response
{
return Inertia::render('form/Index', [
'data' => FormResource::collection($this->handle()),
]);
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Form\Resources;
use App\Form\Models\Form;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* @mixin Form
*/
class FormResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array<string, mixed>
*/
public function toArray($request)
{
return [
'name' => $this->name,
'from_human' => $this->from?->format('d.m.Y'),
'to_human' => $this->to?->format('d.m.Y'),
'from' => $this->from?->format('Y-m-d'),
'to' => $this->to?->format('Y-m-d'),
'excerpt' => $this->excerpt,
'description' => $this->description,
'mail_top' => $this->mail_top,
'mail_bottom' => $this->mail_bottom,
'registration_from' => $this->registration_from?->format('Y-m-d H:i:s'),
'registration_until' => $this->registration_until?->format('Y-m-d H:i:s'),
];
}
}

View File

@ -1,12 +1,17 @@
<?php
namespace Database\Factories;
namespace Database\Factories\Form\Models;
use App\Form\Models\Form;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<Form>
* @method self name(string $name)
* @method self from(string $from)
* @method self to(string $to)
* @method self excerpt(string $excerpt)
* @method self description(string $description)
*/
class FormFactory extends Factory
{
@ -25,7 +30,32 @@ class FormFactory extends Factory
public function definition()
{
return [
//
'name' => $this->faker->words(4, true),
'description' => $this->faker->text(),
'excerpt' => $this->faker->words(10, true),
'config' => ['sections' => []],
'from' => $this->faker->dateTime()->format('Y-m-d H:i:s'),
'to' => $this->faker->dateTime()->format('Y-m-d H:i:s'),
'registration_from' => $this->faker->dateTime()->format('Y-m-d H:i:s'),
'registration_until' => $this->faker->dateTime()->format('Y-m-d H:i:s'),
'mail_top' => $this->faker->text(),
'mail_bottom' => $this->faker->text(),
];
}
/**
* @param array<int, FormtemplateSectionRequest> $sections
*/
public function sections(array $sections): self
{
return $this->state(['config.sections' => $sections]);
}
/**
* @param mixed $args
*/
public function __call($method, $parameters): self
{
return $this->state([str($method)->snake()->toString() => $parameters[0]]);
}
}

View File

@ -26,8 +26,8 @@ return new class extends Migration
$table->text('description');
$table->text('excerpt');
$table->json('config');
$table->dateTime('from');
$table->dateTime('to');
$table->date('from');
$table->date('to');
$table->dateTime('registration_from')->nullable();
$table->dateTime('registration_until')->nullable();
$table->text('mail_top')->nullable();

View File

@ -19,6 +19,7 @@ use App\Invoice\Actions\InvoiceStoreAction;
use App\Course\Actions\CourseUpdateAction;
use App\Dashboard\Actions\IndexAction as DashboardIndexAction;
use App\Efz\ShowEfzDocumentAction;
use App\Form\Actions\FormIndexAction;
use App\Group\Actions\GroupApiIndexAction;
use App\Group\Actions\GroupBulkstoreAction;
use App\Group\Actions\GroupIndexAction;
@ -146,6 +147,7 @@ Route::group(['middleware' => 'auth:web'], function (): void {
// ------------------------------------ form -----------------------------------
Route::get('/formtemplate', FormtemplateIndexAction::class)->name('formtemplate.index');
Route::get('/form', FormIndexAction::class)->name('form.index');
Route::post('/formtemplate', FormtemplateStoreAction::class)->name('formtemplate.store');
Route::patch('/formtemplate/{formtemplate}', FormtemplateUpdateAction::class)->name('formtemplate.update');
Route::post('/form', FormStoreAction::class)->name('form.store');

View File

@ -0,0 +1,33 @@
<?php
namespace Tests\Feature\Form;
use App\Form\Models\Form;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class FormIndexActionTest extends TestCase
{
use DatabaseTransactions;
public function testItDisplaysForms(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
Form::factory()->name('lala')->excerpt('fff')->description('desc')->from('2023-05-05')->to('2023-06-07')->mailTop('Guten Tag')->mailBottom('Cheers')->registrationFrom('2023-05-06 04:00:00')->registrationUntil('2023-04-01 05:00:00')->create();
$this->get(route('form.index'))
->assertOk()
->assertInertiaPath('data.data.0.name', 'lala')
->assertInertiaPath('data.data.0.from_human', '05.05.2023')
->assertInertiaPath('data.data.0.to_human', '07.06.2023')
->assertInertiaPath('data.data.0.from', '2023-05-05')
->assertInertiaPath('data.data.0.to', '2023-06-07')
->assertInertiaPath('data.data.0.excerpt', 'fff')
->assertInertiaPath('data.data.0.description', 'desc')
->assertInertiaPath('data.data.0.mail_top', 'Guten Tag')
->assertInertiaPath('data.data.0.mail_bottom', 'Cheers')
->assertInertiaPath('data.data.0.registration_from', '2023-05-06 04:00:00')
->assertInertiaPath('data.data.0.registration_until', '2023-04-01 05:00:00');
}
}

View File

@ -22,7 +22,7 @@ class FormStoreActionTest extends TestCase
->name('formname')
->description('lala ggg')
->excerpt('avff')
->registrationFrom('2023-05-04 01:00:00')->registrationUntil('2023-07-07 01:00:00')->from('2023-07-07 03:00')->to('2023-07-08 05:00')
->registrationFrom('2023-05-04 01:00:00')->registrationUntil('2023-07-07 01:00:00')->from('2023-07-07')->to('2023-07-08')
->mailTop('Guten Tag')
->mailBottom('Viele Grüße')
->sections([FormtemplateSectionRequest::new()->name('sname')->fields([FormtemplateFieldRequest::new()])])
@ -39,8 +39,8 @@ class FormStoreActionTest extends TestCase
$this->assertEquals('Viele Grüße', $form->mail_bottom);
$this->assertEquals('2023-05-04 01:00', $form->registration_from->format('Y-m-d H:i'));
$this->assertEquals('2023-07-07 01:00', $form->registration_until->format('Y-m-d H:i'));
$this->assertEquals('2023-07-07 03:00', $form->from->format('Y-m-d H:i'));
$this->assertEquals('2023-07-08 05:00', $form->to->format('Y-m-d H:i'));
$this->assertEquals('2023-07-07', $form->from->format('Y-m-d'));
$this->assertEquals('2023-07-08', $form->to->format('Y-m-d'));
Event::assertDispatched(Succeeded::class, fn (Succeeded $event) => $event->message === 'Formular gespeichert.');
}