2022-10-02 18:38:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2023-09-08 11:00:28 +00:00
|
|
|
use App\Http\Requests\Templates\FormTemplateRequest;
|
|
|
|
use App\Http\Resources\FormTemplateResource;
|
2022-10-02 18:38:41 +00:00
|
|
|
use App\Models\Template;
|
2023-09-08 11:00:28 +00:00
|
|
|
use Illuminate\Http\Request;
|
2022-10-02 18:38:41 +00:00
|
|
|
|
|
|
|
class TemplateController extends Controller
|
|
|
|
{
|
2023-09-08 11:00:28 +00:00
|
|
|
public function index(Request $request)
|
2022-10-02 18:38:41 +00:00
|
|
|
{
|
2023-09-08 11:00:28 +00:00
|
|
|
$limit = null;
|
|
|
|
if ($request->offsetExists('limit') && $request->get('limit') > 0) {
|
|
|
|
$limit = (int) $request->get('limit');
|
|
|
|
}
|
|
|
|
return FormTemplateResource::collection(
|
|
|
|
Template::where('publicly_listed', true)
|
|
|
|
->orderByDesc('created_at')
|
|
|
|
->limit($limit)
|
|
|
|
->get()
|
|
|
|
);
|
2022-10-02 18:38:41 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 11:00:28 +00:00
|
|
|
public function create(FormTemplateRequest $request)
|
2022-10-02 18:38:41 +00:00
|
|
|
{
|
2022-11-16 12:12:48 +00:00
|
|
|
$this->authorize('create', Template::class);
|
2022-10-02 18:38:41 +00:00
|
|
|
|
|
|
|
// Create template
|
|
|
|
$template = $request->getTemplate();
|
|
|
|
$template->save();
|
|
|
|
|
|
|
|
return $this->success([
|
2023-09-08 11:00:28 +00:00
|
|
|
'message' => 'Template was created.',
|
2022-10-02 18:38:41 +00:00
|
|
|
'template_id' => $template->id
|
|
|
|
]);
|
|
|
|
}
|
2023-09-08 11:00:28 +00:00
|
|
|
|
|
|
|
public function update(FormTemplateRequest $request, string $id)
|
|
|
|
{
|
|
|
|
$template = Template::findOrFail($id);
|
|
|
|
$this->authorize('update', $template);
|
|
|
|
|
|
|
|
$template->update($request->all());
|
|
|
|
|
|
|
|
return $this->success([
|
|
|
|
'message' => 'Template was updated.',
|
|
|
|
'template_id' => $template->id,
|
|
|
|
'data' => new FormTemplateResource($template)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
|
|
|
$template = Template::findOrFail($id);
|
|
|
|
$this->authorize('delete', $template);
|
|
|
|
|
|
|
|
$template->delete();
|
|
|
|
|
|
|
|
return $this->success([
|
|
|
|
'message' => 'Template was deleted.',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function show(string $slug)
|
|
|
|
{
|
|
|
|
return new FormTemplateResource(
|
|
|
|
Template::whereSlug($slug)->firstOrFail()
|
|
|
|
);
|
|
|
|
}
|
2022-10-02 18:38:41 +00:00
|
|
|
}
|