taktik - laravel
This commit is contained in:
59
app/Http/Controllers/CommentController.php
Normal file
59
app/Http/Controllers/CommentController.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?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),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user