Wires PostController (recent, show, create, delete, listByUser) and CommentController (listForPost, create, delete) to the existing use cases. Posts and comments expose author display names alongside user IDs. CommentRepository binding added to RepositoryServiceProvider.
124 lines
3.8 KiB
PHP
124 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Comment\Comment;
|
|
use App\Comment\UseCases\CreateComment\CreateComment;
|
|
use App\Comment\UseCases\CreateComment\CreateCommentRequest;
|
|
use App\Comment\UseCases\DeleteComment\DeleteComment;
|
|
use App\Comment\UseCases\DeleteComment\DeleteCommentRequest;
|
|
use App\Comment\UseCases\ListCommentsForPost\ListCommentsForPost;
|
|
use App\Comment\UseCases\ListCommentsForPost\ListCommentsForPostRequest;
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Exceptions\ForbiddenException;
|
|
use App\User\User;
|
|
use App\User\UserRepository;
|
|
use DomainException;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class CommentController
|
|
{
|
|
public function __construct(
|
|
private CreateComment $createComment,
|
|
private DeleteComment $deleteComment,
|
|
private ListCommentsForPost $listCommentsForPost,
|
|
private UserRepository $userRepo,
|
|
) {}
|
|
|
|
public function listForPost(Request $request, int $postId): JsonResponse
|
|
{
|
|
try {
|
|
$comments = $this->listCommentsForPost->execute(
|
|
new ListCommentsForPostRequest(postId: $postId),
|
|
);
|
|
} catch (BadRequestException $exception) {
|
|
return new JsonResponse(
|
|
['error' => $exception->getMessage()], 400,
|
|
);
|
|
}
|
|
|
|
return new JsonResponse([
|
|
'comments' => array_map(
|
|
function (Comment $comment) {
|
|
return $this->serialize($comment);
|
|
},
|
|
$comments,
|
|
),
|
|
], 200);
|
|
}
|
|
|
|
public function create(Request $request, int $postId): JsonResponse
|
|
{
|
|
/** @var User $user */
|
|
$user = $request->attributes->get('user');
|
|
try {
|
|
$comment = $this->createComment->execute(new CreateCommentRequest(
|
|
postId: $postId,
|
|
userId: $user->getId(),
|
|
body: $request->input('body'),
|
|
));
|
|
} catch (BadRequestException $exception) {
|
|
return new JsonResponse(
|
|
['error' => $exception->getMessage()], 400,
|
|
);
|
|
} catch (DomainException $exception) {
|
|
return new JsonResponse(
|
|
['error' => $exception->getMessage()], 404,
|
|
);
|
|
}
|
|
|
|
return new JsonResponse([
|
|
'comment' => $this->serialize($comment),
|
|
], 201);
|
|
}
|
|
|
|
public function delete(Request $request, int $id): JsonResponse
|
|
{
|
|
/** @var User $user */
|
|
$user = $request->attributes->get('user');
|
|
try {
|
|
$this->deleteComment->execute(new DeleteCommentRequest(
|
|
commentId: $id,
|
|
requesterId: $user->getId(),
|
|
requesterIsAdmin: $user->isAdmin(),
|
|
));
|
|
} catch (BadRequestException $exception) {
|
|
return new JsonResponse(
|
|
['error' => $exception->getMessage()], 400,
|
|
);
|
|
} catch (ForbiddenException $exception) {
|
|
return new JsonResponse(
|
|
['error' => $exception->getMessage()], 403,
|
|
);
|
|
}
|
|
|
|
return new JsonResponse(null, 204);
|
|
}
|
|
|
|
/**
|
|
* @return array{
|
|
* id: int,
|
|
* postId: int,
|
|
* userId: int,
|
|
* authorDisplayName: string,
|
|
* body: string,
|
|
* createdAt: string
|
|
* }
|
|
*/
|
|
private function serialize(Comment $comment): array
|
|
{
|
|
$author = $this->userRepo->find($comment->getUserId());
|
|
|
|
return [
|
|
'id' => $comment->getId(),
|
|
'postId' => $comment->getPostId(),
|
|
'userId' => $comment->getUserId(),
|
|
'authorDisplayName' => $author === null
|
|
? ''
|
|
: $author->getDisplayName(),
|
|
'body' => $comment->getBody(),
|
|
'createdAt' => $comment->getCreatedAt()->format(DATE_ATOM),
|
|
];
|
|
}
|
|
}
|