56 lines
1.9 KiB
PHP
56 lines
1.9 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Api\V1;
|
||
|
||
use App\Enums\FaqCategory;
|
||
use App\Http\Controllers\Controller;
|
||
use App\Http\Resources\FaqResource;
|
||
use App\Repositories\Contracts\FaqRepositoryInterface;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||
use OpenApi\Attributes as OA;
|
||
|
||
class FaqController extends Controller
|
||
{
|
||
public function __construct(private FaqRepositoryInterface $repository) {}
|
||
|
||
#[OA\Get(
|
||
path: '/api/v1/faqs/{category?}',
|
||
summary: 'SSS listesi',
|
||
description: 'Tüm veya kategoriye göre filtrelenmiş SSS listesi döndürür. ?limit=6 ile anasayfa için sınırlanabilir.',
|
||
tags: ['FAQs'],
|
||
parameters: [
|
||
new OA\Parameter(name: 'category', in: 'path', required: false, schema: new OA\Schema(type: 'string'), description: 'FAQ kategori filtresi'),
|
||
new OA\Parameter(name: 'limit', in: 'query', required: false, schema: new OA\Schema(type: 'integer'), description: 'Dönen SSS sayısını sınırla (anasayfa için)'),
|
||
],
|
||
responses: [
|
||
new OA\Response(response: 200, description: 'SSS listesi'),
|
||
new OA\Response(response: 404, description: 'Kategori bulunamadı'),
|
||
],
|
||
)]
|
||
public function index(Request $request, ?string $category = null): AnonymousResourceCollection
|
||
{
|
||
$limit = $request->integer('limit', 0);
|
||
|
||
if ($category) {
|
||
$faqCategory = FaqCategory::tryFrom($category);
|
||
|
||
if (! $faqCategory) {
|
||
abort(404, 'FAQ kategorisi bulunamadı.');
|
||
}
|
||
|
||
$faqs = $this->repository->getByCategory($faqCategory);
|
||
|
||
if ($limit > 0) {
|
||
$faqs = $faqs->take($limit);
|
||
}
|
||
|
||
return FaqResource::collection($faqs);
|
||
}
|
||
|
||
$perPage = $limit > 0 ? $limit : 100;
|
||
|
||
return FaqResource::collection($this->repository->paginate([], $perPage));
|
||
}
|
||
}
|