From beb6774ea9f6fe8591212a4c26116d6ce500bddb Mon Sep 17 00:00:00 2001 From: ahnyunki Date: Sun, 24 Mar 2024 14:58:51 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20TalkRoom=20Comment=20=EB=93=B1=EB=A1=9D?= =?UTF-8?q?=20API=20=EA=B5=AC=ED=98=84=20(#24)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/comment/CommentController.java | 31 +++++++++++++++ .../comment/request/CommentCreateRequest.java | 27 +++++++++++++ .../application/comment/CommentService.java | 38 +++++++++++++++++++ .../request/CommentCreateServiceRequest.java | 18 +++++++++ .../comment/response/CommentResponse.java | 28 ++++++++++++++ .../com/jisungin/domain/comment/Comment.java | 9 +++++ .../comment/repository/CommentRepository.java | 10 +++++ 7 files changed, 161 insertions(+) create mode 100644 src/main/java/com/jisungin/api/comment/CommentController.java create mode 100644 src/main/java/com/jisungin/api/comment/request/CommentCreateRequest.java create mode 100644 src/main/java/com/jisungin/application/comment/CommentService.java create mode 100644 src/main/java/com/jisungin/application/comment/request/CommentCreateServiceRequest.java create mode 100644 src/main/java/com/jisungin/application/comment/response/CommentResponse.java create mode 100644 src/main/java/com/jisungin/domain/comment/repository/CommentRepository.java diff --git a/src/main/java/com/jisungin/api/comment/CommentController.java b/src/main/java/com/jisungin/api/comment/CommentController.java new file mode 100644 index 0000000..39f8e67 --- /dev/null +++ b/src/main/java/com/jisungin/api/comment/CommentController.java @@ -0,0 +1,31 @@ +package com.jisungin.api.comment; + +import com.jisungin.api.ApiResponse; +import com.jisungin.api.comment.request.CommentCreateRequest; +import com.jisungin.api.oauth.Auth; +import com.jisungin.api.oauth.AuthContext; +import com.jisungin.application.comment.CommentService; +import com.jisungin.application.comment.response.CommentResponse; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequiredArgsConstructor +@RequestMapping("/v1") +@RestController +public class CommentController { + + private final CommentService commentService; + + @PostMapping("/talk-rooms/{talkRoomId}/comments") + public ApiResponse writeComment(@PathVariable Long talkRoomId, + @Valid @RequestBody CommentCreateRequest request, + @Auth AuthContext authContext) { + return ApiResponse.ok(commentService.writeComment(request.toService(), talkRoomId, authContext.getUserId())); + } + +} diff --git a/src/main/java/com/jisungin/api/comment/request/CommentCreateRequest.java b/src/main/java/com/jisungin/api/comment/request/CommentCreateRequest.java new file mode 100644 index 0000000..234a52e --- /dev/null +++ b/src/main/java/com/jisungin/api/comment/request/CommentCreateRequest.java @@ -0,0 +1,27 @@ +package com.jisungin.api.comment.request; + +import com.jisungin.application.comment.request.CommentCreateServiceRequest; +import jakarta.validation.constraints.NotBlank; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +public class CommentCreateRequest { + + @NotBlank(message = "내용은 필수 입니다.") + private String content; + + @Builder + private CommentCreateRequest(String content) { + this.content = content; + } + + public CommentCreateServiceRequest toService() { + return CommentCreateServiceRequest.builder() + .content(content) + .build(); + } + +} diff --git a/src/main/java/com/jisungin/application/comment/CommentService.java b/src/main/java/com/jisungin/application/comment/CommentService.java new file mode 100644 index 0000000..cb4e1b8 --- /dev/null +++ b/src/main/java/com/jisungin/application/comment/CommentService.java @@ -0,0 +1,38 @@ +package com.jisungin.application.comment; + +import com.jisungin.application.comment.request.CommentCreateServiceRequest; +import com.jisungin.application.comment.response.CommentResponse; +import com.jisungin.domain.comment.Comment; +import com.jisungin.domain.comment.repository.CommentRepository; +import com.jisungin.domain.talkroom.TalkRoom; +import com.jisungin.domain.talkroom.repository.TalkRoomRepository; +import com.jisungin.domain.user.User; +import com.jisungin.domain.user.repository.UserRepository; +import com.jisungin.exception.BusinessException; +import com.jisungin.exception.ErrorCode; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@RequiredArgsConstructor +@Service +public class CommentService { + + private final CommentRepository commentRepository; + private final TalkRoomRepository talkRoomRepository; + private final UserRepository userRepository; + + public CommentResponse writeComment(CommentCreateServiceRequest request, Long talkRoomId, Long userId) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND)); + + TalkRoom talkRoom = talkRoomRepository.findById(talkRoomId) + .orElseThrow(() -> new BusinessException(ErrorCode.TALK_ROOM_NOT_FOUND)); + + Comment comment = Comment.create(request, user, talkRoom); + + commentRepository.save(comment); + + return CommentResponse.of(comment.getContent(), user.getName()); + } + +} diff --git a/src/main/java/com/jisungin/application/comment/request/CommentCreateServiceRequest.java b/src/main/java/com/jisungin/application/comment/request/CommentCreateServiceRequest.java new file mode 100644 index 0000000..7ed4561 --- /dev/null +++ b/src/main/java/com/jisungin/application/comment/request/CommentCreateServiceRequest.java @@ -0,0 +1,18 @@ +package com.jisungin.application.comment.request; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +public class CommentCreateServiceRequest { + + private String content; + + @Builder + private CommentCreateServiceRequest(String content) { + this.content = content; + } + +} diff --git a/src/main/java/com/jisungin/application/comment/response/CommentResponse.java b/src/main/java/com/jisungin/application/comment/response/CommentResponse.java new file mode 100644 index 0000000..68627cd --- /dev/null +++ b/src/main/java/com/jisungin/application/comment/response/CommentResponse.java @@ -0,0 +1,28 @@ +package com.jisungin.application.comment.response; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +public class CommentResponse { + + private String content; + + private String userName; + + @Builder + private CommentResponse(String content, String userName) { + this.content = content; + this.userName = userName; + } + + public static CommentResponse of(String content, String name) { + return CommentResponse.builder() + .content(content) + .userName(name) + .build(); + } + +} diff --git a/src/main/java/com/jisungin/domain/comment/Comment.java b/src/main/java/com/jisungin/domain/comment/Comment.java index b8681d4..54b3a70 100644 --- a/src/main/java/com/jisungin/domain/comment/Comment.java +++ b/src/main/java/com/jisungin/domain/comment/Comment.java @@ -1,5 +1,6 @@ package com.jisungin.domain.comment; +import com.jisungin.application.comment.request.CommentCreateServiceRequest; import com.jisungin.domain.BaseEntity; import com.jisungin.domain.talkroom.TalkRoom; import com.jisungin.domain.user.User; @@ -38,4 +39,12 @@ private Comment(User user, TalkRoom talkRoom, String content) { this.content = content; } + public static Comment create(CommentCreateServiceRequest request, User user, TalkRoom talkRoom) { + return Comment.builder() + .content(request.getContent()) + .user(user) + .talkRoom(talkRoom) + .build(); + } + } diff --git a/src/main/java/com/jisungin/domain/comment/repository/CommentRepository.java b/src/main/java/com/jisungin/domain/comment/repository/CommentRepository.java new file mode 100644 index 0000000..eec459d --- /dev/null +++ b/src/main/java/com/jisungin/domain/comment/repository/CommentRepository.java @@ -0,0 +1,10 @@ +package com.jisungin.domain.comment.repository; + +import com.jisungin.domain.comment.Comment; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CommentRepository extends JpaRepository { + +}