Cleaner templates

This commit is contained in:
Julien Nahum 2022-12-01 13:13:32 +01:00
parent 567b7a44dc
commit 75d3892e07
3 changed files with 68 additions and 7 deletions

View File

@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests\Templates\CreateTemplateRequest; use App\Http\Requests\Templates\CreateTemplateRequest;
use App\Http\Resources\TemplateResource;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\Template; use App\Models\Template;
@ -11,8 +12,7 @@ class TemplateController extends Controller
{ {
public function index() public function index()
{ {
// TODO: create resource return TemplateResource::collection(Template::all());
return Template::all();
} }
public function create(CreateTemplateRequest $request) public function create(CreateTemplateRequest $request)

View File

@ -7,6 +7,44 @@ use Illuminate\Foundation\Http\FormRequest;
class CreateTemplateRequest extends FormRequest class CreateTemplateRequest extends FormRequest
{ {
const IGNORED_KEYS = [
'id',
'creator',
'cleanings',
'closes_at',
'created_at',
'deleted_at',
'updated_at',
'form_pending_submission_key',
'is_closed',
'is_pro',
'is_password_protected',
'last_edited_human',
'max_number_of_submissions_reached',
'notifies',
'notification_body',
'notification_emails',
'notification_sender',
'notification_subject',
'notifications_include_submission',
'notifies_slack',
'slack_webhook_url',
'removed_properties',
'creator',
'creator_id',
'created_at',
'updated_at',
'extra',
'workspace',
'workspace_id',
'submissions',
'submissions_count',
'views',
'views_count',
'visibility',
'webhook_url',
];
/** /**
* Get the validation rules that apply to the request. * Get the validation rules that apply to the request.
* *
@ -24,13 +62,12 @@ class CreateTemplateRequest extends FormRequest
]; ];
} }
public function getTemplate() : Template public function getTemplate(): Template
{ {
$structure = $this->form; $structure = $this->form;
$ignoreKeys = ['id','creator','creator_id','created_at','updated_at','extra','workspace','submissions','submissions_count','views','views_count']; foreach ($structure as $key => $val) {
foreach($structure as $key=>$val){ if (in_array($key, self::IGNORED_KEYS)) {
if(in_array($key, $ignoreKeys)){ unset($structure[$key]);
$structure[$key] = null;
} }
} }
return new Template([ return new Template([

View File

@ -0,0 +1,24 @@
<?php
namespace App\Http\Resources;
use App\Http\Requests\Templates\CreateTemplateRequest;
use Illuminate\Http\Resources\Json\JsonResource;
class TemplateResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
$data = parent::toArray($request);
foreach (CreateTemplateRequest::IGNORED_KEYS as $key) {
unset($data[$key]);
}
return $data;
}
}