60 lines
1.9 KiB
PHP
60 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\Comment\DestroyCommentRequest;
|
|
use App\Http\Requests\Comment\ListCommentRequest;
|
|
use App\Http\Requests\Comment\StoreCommentRequest;
|
|
use App\Http\Requests\Comment\UpdateCommentRequest;
|
|
use App\Http\Resources\CommentCollection;
|
|
use App\Http\Resources\CommentResource;
|
|
use App\Http\Resources\PaginableResource;
|
|
use App\Services\Comment\PostCommentService;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
class CommentController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly PostCommentService $commentService,
|
|
) {
|
|
}
|
|
|
|
public function list(ListCommentRequest $request, int $postId, int $page): JsonResponse
|
|
{
|
|
$comments = $this->commentService->fetchComments(
|
|
$postId,
|
|
$page,
|
|
$request->filters(),
|
|
$request->order()
|
|
);
|
|
return response()->json(PaginableResource::make($comments, CommentCollection::class));
|
|
}
|
|
|
|
public function store(StoreCommentRequest $request, int $postId): JsonResponse
|
|
{
|
|
return response()->json(CommentResource::make($this->commentService->storeComment($request->all(), $postId)), 201);
|
|
}
|
|
|
|
public function update(UpdateCommentRequest $request, int $postId, int $id): JsonResponse
|
|
{
|
|
$comment = $this->commentService->updateComment($request->all(), $postId, $id);
|
|
|
|
if ($comment === null) {
|
|
return response()->json(null, 404);
|
|
}
|
|
|
|
return response()->json(CommentResource::make($comment));
|
|
}
|
|
|
|
public function destroy(DestroyCommentRequest $post, int $postId, int $id): JsonResponse
|
|
{
|
|
$isSuccessfullyDeleted = $this->commentService->deleteComment($postId, $id);
|
|
return match ($isSuccessfullyDeleted) {
|
|
false => response()->json(null, 404),
|
|
true => response()->json(null, 204),
|
|
};
|
|
}
|
|
}
|