feat: add ApiResponse trait for standardized API responses

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
This commit is contained in:
2026-07-07 01:03:12 +03:30
parent cf0e6f82fd
commit ae974c2afa
68 changed files with 3594 additions and 100 deletions

View File

@@ -0,0 +1,155 @@
<?php
namespace App\Http\Controllers\Api;
use App\Enums\TemplateGenerationStatus;
use App\Enums\TemplateKeyType;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\GenerateTemplateRequest;
use App\Http\Resources\TemplateGenerationResource;
use App\Jobs\GenerateTemplateJob;
use App\Models\Template;
use App\Models\TemplateGeneration;
use App\Traits\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use OpenApi\Attributes as OA;
use Symfony\Component\HttpFoundation\StreamedResponse;
class TemplateGenerationController extends Controller
{
use ApiResponse;
#[OA\Post(
path: '/templates/{slug}/generate',
description: 'Start generating a filled presentation from a template',
summary: 'Generate presentation',
security: [['sanctum' => []]],
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: 202,
description: 'Generation started',
content: new OA\JsonContent(
properties: [
new OA\Property(property: 'message', type: 'string', example: 'درخواست تولید پاورپوینت ثبت شد.'),
new OA\Property(
property: 'data',
properties: [
new OA\Property(property: 'generation', ref: '#/components/schemas/TemplateGeneration'),
],
type: 'object'
),
]
)
),
new OA\Response(response: 401, description: 'Unauthenticated'),
new OA\Response(response: 422, description: 'Validation error'),
]
)]
public function store(GenerateTemplateRequest $request, Template $template): JsonResponse
{
$uuid = (string) Str::uuid();
$values = $request->validated('values', []);
$storedValues = [];
foreach ($template->keys as $key) {
if (! array_key_exists($key->key, $values)) {
continue;
}
if ($key->type === TemplateKeyType::Image) {
$storedValues[$key->key] = $request->file('values.'.$key->key)
->store('templates/generations/inputs/'.$uuid, 'local');
continue;
}
$storedValues[$key->key] = $values[$key->key];
}
$generation = TemplateGeneration::query()->create([
'template_id' => $template->id,
'uuid' => $uuid,
'status' => TemplateGenerationStatus::Pending,
'values' => $storedValues,
'requested_by' => $request->user()->id,
]);
GenerateTemplateJob::dispatch($generation);
return $this->success('درخواست تولید پاورپوینت ثبت شد.', [
'generation' => new TemplateGenerationResource($generation),
], 202);
}
#[OA\Get(
path: '/generations/{uuid}',
description: 'Get the status of a presentation generation request',
summary: 'Show generation status',
security: [['sanctum' => []]],
tags: ['Templates'],
parameters: [
new OA\Parameter(name: 'uuid', description: 'Generation uuid', in: 'path', required: true, schema: new OA\Schema(type: 'string')),
],
responses: [
new OA\Response(
response: 200,
description: 'Generation status 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: 'generation', ref: '#/components/schemas/TemplateGeneration'),
],
type: 'object'
),
]
)
),
new OA\Response(response: 401, description: 'Unauthenticated'),
new OA\Response(response: 403, description: 'Forbidden'),
new OA\Response(response: 404, description: 'Not found'),
]
)]
public function show(TemplateGeneration $generation): JsonResponse
{
abort_if($generation->requested_by !== request()->user()->id, 403);
return $this->success('وضعیت تولید با موفقیت دریافت شد.', [
'generation' => new TemplateGenerationResource($generation),
]);
}
#[OA\Get(
path: '/generations/{uuid}/download',
description: 'Download the generated presentation file',
summary: 'Download generated presentation',
security: [['sanctum' => []]],
tags: ['Templates'],
parameters: [
new OA\Parameter(name: 'uuid', description: 'Generation uuid', in: 'path', required: true, schema: new OA\Schema(type: 'string')),
],
responses: [
new OA\Response(response: 200, description: 'File download'),
new OA\Response(response: 401, description: 'Unauthenticated'),
new OA\Response(response: 403, description: 'Forbidden'),
new OA\Response(response: 404, description: 'Not found or not ready'),
]
)]
public function download(TemplateGeneration $generation): StreamedResponse
{
abort_if($generation->requested_by !== request()->user()->id, Response::HTTP_FORBIDDEN);
abort_if($generation->status !== TemplateGenerationStatus::Completed || ! $generation->output_path, Response::HTTP_NOT_FOUND);
return Storage::disk('local')->download($generation->output_path, $generation->template->slug.'.pptx');
}
}