chore: update composer dependencies to include phppresentation chore: update composer.lock after adding phppresentation feat: create categories migration for category management feat: create templates migration for template storage feat: create template generations migration for tracking template usage feat: create template keys migration for template customization feat: create notifications migration for user notifications feat: update API routes for categories, templates, and user profile management docs: update API documentation to include new endpoints for profile and wallet
105 lines
3.7 KiB
PHP
105 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Resources\TemplateResource;
|
|
use App\Models\Template;
|
|
use App\Traits\ApiResponse;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use OpenApi\Attributes as OA;
|
|
|
|
class TemplateController extends Controller
|
|
{
|
|
use ApiResponse;
|
|
|
|
#[OA\Get(
|
|
path: '/templates',
|
|
description: 'List active templates, optionally filtered by category',
|
|
summary: 'List templates',
|
|
tags: ['Templates'],
|
|
parameters: [
|
|
new OA\Parameter(name: 'category', description: 'Category slug', in: 'query', schema: new OA\Schema(type: 'string')),
|
|
],
|
|
responses: [
|
|
new OA\Response(
|
|
response: 200,
|
|
description: 'Templates fetched successfully',
|
|
content: new OA\JsonContent(
|
|
properties: [
|
|
new OA\Property(property: 'message', type: 'string', example: 'قالبها با موفقیت دریافت شد.'),
|
|
new OA\Property(
|
|
property: 'data',
|
|
properties: [
|
|
new OA\Property(
|
|
property: 'templates',
|
|
type: 'array',
|
|
items: new OA\Items(ref: '#/components/schemas/Template')
|
|
),
|
|
],
|
|
type: 'object'
|
|
),
|
|
]
|
|
)
|
|
),
|
|
]
|
|
)]
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$templates = Template::query()
|
|
->with('category')
|
|
->where('is_active', true)
|
|
->when(
|
|
$request->string('category')->isNotEmpty(),
|
|
fn ($query) => $query->whereHas(
|
|
'category',
|
|
fn ($categoryQuery) => $categoryQuery->where('slug', $request->string('category'))
|
|
)
|
|
)
|
|
->orderBy('title')
|
|
->get();
|
|
|
|
return $this->success('قالبها با موفقیت دریافت شد.', [
|
|
'templates' => TemplateResource::collection($templates),
|
|
]);
|
|
}
|
|
|
|
#[OA\Get(
|
|
path: '/templates/{slug}',
|
|
description: 'Get a template details including its fillable keys',
|
|
summary: 'Show template',
|
|
tags: ['Templates'],
|
|
parameters: [
|
|
new OA\Parameter(name: 'slug', description: 'Template slug', in: 'path', required: true, schema: new OA\Schema(type: 'string')),
|
|
],
|
|
responses: [
|
|
new OA\Response(
|
|
response: 200,
|
|
description: 'Template fetched successfully',
|
|
content: new OA\JsonContent(
|
|
properties: [
|
|
new OA\Property(property: 'message', type: 'string', example: 'قالب با موفقیت دریافت شد.'),
|
|
new OA\Property(
|
|
property: 'data',
|
|
properties: [
|
|
new OA\Property(property: 'template', ref: '#/components/schemas/Template'),
|
|
],
|
|
type: 'object'
|
|
),
|
|
]
|
|
)
|
|
),
|
|
new OA\Response(response: 404, description: 'Template not found'),
|
|
]
|
|
)]
|
|
public function show(Template $template): JsonResponse
|
|
{
|
|
$template->load('category', 'keys');
|
|
|
|
return $this->success('قالب با موفقیت دریافت شد.', [
|
|
'template' => new TemplateResource($template),
|
|
]);
|
|
}
|
|
}
|