From e8d2ff3fdfb02014c5e0576a06beab3fd380dcf4 Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Wed, 6 May 2026 22:14:55 +0300 Subject: [PATCH] implement CreateComment use case --- .../UseCases/CreateComment/CreateComment.php | 49 +++++++++++++++++++ .../CreateComment/CreateCommentRequest.php | 12 +++++ 2 files changed, 61 insertions(+) create mode 100644 backend/app/Comment/UseCases/CreateComment/CreateComment.php create mode 100644 backend/app/Comment/UseCases/CreateComment/CreateCommentRequest.php diff --git a/backend/app/Comment/UseCases/CreateComment/CreateComment.php b/backend/app/Comment/UseCases/CreateComment/CreateComment.php new file mode 100644 index 0000000..1987b5d --- /dev/null +++ b/backend/app/Comment/UseCases/CreateComment/CreateComment.php @@ -0,0 +1,49 @@ +postId <= 0) { + throw new BadRequestException('postId must be positive'); + } + if ($request->userId <= 0) { + throw new BadRequestException('userId must be positive'); + } + $body = $request->body === null ? '' : trim($request->body); + if ($body === '') { + throw new BadRequestException('body is required'); + } + + if ($this->postRepo->find($request->postId) === null) { + throw new DomainException('post not found'); + } + + return $this->commentRepo->create(new CreateCommentDto( + postId: $request->postId, + userId: $request->userId, + body: $body, + createdAt: $this->clock->now(), + )); + } +} diff --git a/backend/app/Comment/UseCases/CreateComment/CreateCommentRequest.php b/backend/app/Comment/UseCases/CreateComment/CreateCommentRequest.php new file mode 100644 index 0000000..d2984bb --- /dev/null +++ b/backend/app/Comment/UseCases/CreateComment/CreateCommentRequest.php @@ -0,0 +1,12 @@ +