141 lines
6.4 KiB
PHP
141 lines
6.4 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Api\Admin;
|
||
|
||
use App\Actions\Announcement\CreateAnnouncementAction;
|
||
use App\Actions\Announcement\DeleteAnnouncementAction;
|
||
use App\Actions\Announcement\UpdateAnnouncementAction;
|
||
use App\DTOs\AnnouncementData;
|
||
use App\Http\Controllers\Controller;
|
||
use App\Http\Requests\Announcement\StoreAnnouncementRequest;
|
||
use App\Http\Requests\Announcement\UpdateAnnouncementRequest;
|
||
use App\Http\Resources\AnnouncementResource;
|
||
use App\Models\Announcement;
|
||
use App\Repositories\Contracts\AnnouncementRepositoryInterface;
|
||
use Illuminate\Http\JsonResponse;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||
use OpenApi\Attributes as OA;
|
||
|
||
class AnnouncementController extends Controller
|
||
{
|
||
public function __construct(private AnnouncementRepositoryInterface $repository) {}
|
||
|
||
#[OA\Get(
|
||
path: '/api/admin/announcements',
|
||
summary: 'Duyuruları listele (Admin)',
|
||
tags: ['Admin - Announcements'],
|
||
security: [['sanctum' => []]],
|
||
parameters: [
|
||
new OA\Parameter(name: 'category', in: 'query', required: false, schema: new OA\Schema(type: 'string')),
|
||
new OA\Parameter(name: 'featured', in: 'query', required: false, schema: new OA\Schema(type: 'boolean')),
|
||
new OA\Parameter(name: 'search', in: 'query', required: false, schema: new OA\Schema(type: 'string')),
|
||
new OA\Parameter(name: 'sort', in: 'query', required: false, schema: new OA\Schema(type: 'string')),
|
||
new OA\Parameter(name: 'per_page', in: 'query', required: false, schema: new OA\Schema(type: 'integer', default: 15)),
|
||
],
|
||
responses: [new OA\Response(response: 200, description: 'Duyuru listesi')],
|
||
)]
|
||
public function index(Request $request): AnonymousResourceCollection
|
||
{
|
||
$announcements = $this->repository->paginate(
|
||
$request->only(['category', 'featured', 'search', 'sort']),
|
||
$request->integer('per_page', 15),
|
||
);
|
||
|
||
return AnnouncementResource::collection($announcements);
|
||
}
|
||
|
||
#[OA\Post(
|
||
path: '/api/admin/announcements',
|
||
summary: 'Yeni duyuru oluştur',
|
||
tags: ['Admin - Announcements'],
|
||
security: [['sanctum' => []]],
|
||
requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent(
|
||
required: ['title', 'slug', 'category', 'content'],
|
||
properties: [
|
||
new OA\Property(property: 'title', type: 'string'),
|
||
new OA\Property(property: 'slug', type: 'string'),
|
||
new OA\Property(property: 'category', type: 'string'),
|
||
new OA\Property(property: 'content', type: 'string'),
|
||
new OA\Property(property: 'excerpt', type: 'string'),
|
||
new OA\Property(property: 'image', type: 'string'),
|
||
new OA\Property(property: 'is_featured', type: 'boolean'),
|
||
new OA\Property(property: 'is_active', type: 'boolean'),
|
||
new OA\Property(property: 'published_at', type: 'string', format: 'date-time'),
|
||
new OA\Property(property: 'meta_title', type: 'string'),
|
||
new OA\Property(property: 'meta_description', type: 'string'),
|
||
],
|
||
)),
|
||
responses: [
|
||
new OA\Response(response: 201, description: 'Duyuru oluşturuldu'),
|
||
new OA\Response(response: 422, description: 'Validasyon hatası'),
|
||
],
|
||
)]
|
||
public function store(StoreAnnouncementRequest $request, CreateAnnouncementAction $action): JsonResponse
|
||
{
|
||
$dto = AnnouncementData::fromArray($request->validated());
|
||
$announcement = $action->execute($dto);
|
||
|
||
return response()->json(new AnnouncementResource($announcement), 201);
|
||
}
|
||
|
||
#[OA\Get(
|
||
path: '/api/admin/announcements/{announcement}',
|
||
summary: 'Duyuru detayı (Admin)',
|
||
tags: ['Admin - Announcements'],
|
||
security: [['sanctum' => []]],
|
||
parameters: [new OA\Parameter(name: 'announcement', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
|
||
responses: [new OA\Response(response: 200, description: 'Duyuru detayı')],
|
||
)]
|
||
public function show(Announcement $announcement): JsonResponse
|
||
{
|
||
return response()->json(new AnnouncementResource($announcement));
|
||
}
|
||
|
||
#[OA\Put(
|
||
path: '/api/admin/announcements/{announcement}',
|
||
summary: 'Duyuru güncelle',
|
||
tags: ['Admin - Announcements'],
|
||
security: [['sanctum' => []]],
|
||
parameters: [new OA\Parameter(name: 'announcement', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
|
||
requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent(
|
||
properties: [
|
||
new OA\Property(property: 'title', type: 'string'),
|
||
new OA\Property(property: 'slug', type: 'string'),
|
||
new OA\Property(property: 'category', type: 'string'),
|
||
new OA\Property(property: 'content', type: 'string'),
|
||
new OA\Property(property: 'excerpt', type: 'string'),
|
||
new OA\Property(property: 'image', type: 'string'),
|
||
new OA\Property(property: 'is_featured', type: 'boolean'),
|
||
new OA\Property(property: 'is_active', type: 'boolean'),
|
||
],
|
||
)),
|
||
responses: [
|
||
new OA\Response(response: 200, description: 'Duyuru güncellendi'),
|
||
new OA\Response(response: 422, description: 'Validasyon hatası'),
|
||
],
|
||
)]
|
||
public function update(UpdateAnnouncementRequest $request, Announcement $announcement, UpdateAnnouncementAction $action): JsonResponse
|
||
{
|
||
$dto = AnnouncementData::fromArray(array_merge($announcement->toArray(), $request->validated()));
|
||
$announcement = $action->execute($announcement, $dto);
|
||
|
||
return response()->json(new AnnouncementResource($announcement));
|
||
}
|
||
|
||
#[OA\Delete(
|
||
path: '/api/admin/announcements/{announcement}',
|
||
summary: 'Duyuru sil',
|
||
tags: ['Admin - Announcements'],
|
||
security: [['sanctum' => []]],
|
||
parameters: [new OA\Parameter(name: 'announcement', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
|
||
responses: [new OA\Response(response: 200, description: 'Duyuru silindi')],
|
||
)]
|
||
public function destroy(Announcement $announcement, DeleteAnnouncementAction $action): JsonResponse
|
||
{
|
||
$action->execute($announcement);
|
||
|
||
return response()->json(['message' => 'Duyuru silindi.']);
|
||
}
|
||
}
|