Ad backend for index and store

This commit is contained in:
philipp lang 2023-12-22 20:40:24 +01:00
parent ae679641d7
commit 2ecd4b9643
8 changed files with 251 additions and 0 deletions

View File

@ -0,0 +1,33 @@
<?php
namespace App\Form\Actions;
use App\Form\Models\Formtemplate;
use App\Form\Resources\FormtemplateResource;
use Illuminate\Pagination\LengthAwarePaginator;
use Inertia\Inertia;
use Inertia\Response;
use Lorisleiva\Actions\Concerns\AsAction;
class FormtemplateIndexAction
{
use AsAction;
/**
* @return LengthAwarePaginator<Formtemplate>
*/
public function handle(): LengthAwarePaginator
{
return Formtemplate::paginate(15);
}
public function asController(): Response
{
session()->put('menu', 'formtemplate');
session()->put('title', 'Formular-Vorlagen');
return Inertia::render('formtemplate/Index', [
'data' => FormtemplateResource::collection($this->handle()),
]);
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Form\Actions;
use App\Form\Models\Formtemplate;
use Illuminate\Http\JsonResponse;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class FormtemplateStoreAction
{
use AsAction;
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'config' => '',
];
}
/**
* @param array<string, mixed> $attributes
*/
public function handle(array $attributes): Formtemplate
{
return Formtemplate::create($attributes);
}
public function asController(ActionRequest $request): JsonResponse
{
$this->handle($request->validated());
return response()->json([]);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace App\Form\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Formtemplate extends Model
{
use HasFactory;
public $guarded = [];
public $casts = [
'config' => 'json',
];
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Form\Resources;
use App\Lib\HasMeta;
use Illuminate\Http\Resources\Json\JsonResource;
class FormtemplateResource extends JsonResource
{
use HasMeta;
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array<string, mixed>
*/
public function toArray($request)
{
return parent::toArray($request);
}
/**
* @return array<string, mixed>
*/
public static function meta(): array
{
return [
'fields' => [
[
'id' => 'TextField',
'name' => 'Text',
]
],
'links' => [
'store' => route('formtemplate.store'),
],
'default' => [
'name' => '',
'config' => [
'sections' => [],
]
],
'section_default' => [
'fields' => [],
]
];
}
}

View File

@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('formtemplates', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->json('config');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('formtemplates');
}
};

View File

@ -22,6 +22,8 @@ use App\Efz\ShowEfzDocumentAction;
use App\Group\Actions\GroupApiIndexAction;
use App\Group\Actions\GroupBulkstoreAction;
use App\Group\Actions\GroupIndexAction;
use App\Form\Actions\FormtemplateIndexAction;
use App\Form\Actions\FormtemplateStoreAction;
use App\Initialize\Actions\InitializeAction;
use App\Initialize\Actions\InitializeFormAction;
use App\Initialize\Actions\NamiGetSearchLayerAction;
@ -140,4 +142,8 @@ Route::group(['middleware' => 'auth:web'], function (): void {
Route::post('/member/{member}/course', CourseStoreAction::class)->name('member.course.store');
Route::patch('/course/{course}', CourseUpdateAction::class)->name('course.update');
Route::delete('/course/{course}', CourseDestroyAction::class)->name('course.destroy');
// ------------------------------------ form -----------------------------------
Route::get('/formtemplate', FormtemplateIndexAction::class)->name('formtemplate.index');
Route::post('/formtemplate', FormtemplateStoreAction::class)->name('formtemplate.store');
});

View File

@ -0,0 +1,33 @@
<?php
namespace Tests\Feature\Form;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class FormtemplateIndexActionTest extends TestCase
{
use DatabaseTransactions;
public function testItDisplaysIndexPage(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
$this->get(route('formtemplate.index'))
->assertInertiaPath('data.meta.fields.0', [
'id' => 'TextField',
'name' => 'Text',
])
->assertInertiaPath('data.meta.default', [
'name' => '',
'config' => [
'sections' => [],
]
])
->assertInertiaPath('data.meta.links.store', route('formtemplate.store'))
->assertInertiaPath('data.meta.section_default', [
'fields' => [],
]);
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace Tests\Feature\Form;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class FormtemplateStoreActionTest extends TestCase
{
use DatabaseTransactions;
public function testItStoresTemplates(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
$this->postJson(route('formtemplate.store'), [
'name' => 'Testname',
'config' => [
'sections' => [
['name' => 'Persönliches', 'fields' => []]
]
]
])->assertOk();
$this->assertDatabaseHas('formtemplates', [
'name' => 'Testname',
'config' => json_encode(['sections' => [['name' => 'Persönliches', 'fields' => []]]]),
]);
}
public function testNameIsRequired(): void
{
$this->login()->loginNami();
$this->postJson(route('formtemplate.store'), [
'name' => '',
'config' => [
'sections' => []
]
])->assertJsonValidationErrors(['name' => 'Name ist erforderlich']);
}
}