-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FIX]: 회원 - 댓글 연결
- Loading branch information
Showing
10 changed files
with
196 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/mvp/deplog/domain/comment/application/CreateMemberCommentServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package mvp.deplog.domain.comment.application; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import mvp.deplog.domain.comment.domain.Comment; | ||
import mvp.deplog.domain.comment.domain.repository.CommentRepository; | ||
import mvp.deplog.domain.comment.dto.request.CreateCommentReq; | ||
import mvp.deplog.domain.member.domain.Member; | ||
import mvp.deplog.domain.member.domain.Role; | ||
import mvp.deplog.domain.member.domain.repository.MemberRepository; | ||
import mvp.deplog.domain.post.domain.Post; | ||
import mvp.deplog.domain.post.domain.repository.PostRepository; | ||
import mvp.deplog.global.common.Message; | ||
import mvp.deplog.global.common.SuccessResponse; | ||
import mvp.deplog.global.security.UserDetailsImpl; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
@Service | ||
public class CreateMemberCommentServiceImpl implements CreateCommentService { | ||
|
||
private final PostRepository postRepository; | ||
private final CommentRepository commentRepository; | ||
private final MemberRepository memberRepository; | ||
|
||
@Override | ||
public boolean supports(UserDetailsImpl userDetails, Long parentCommentId) { | ||
return (userDetails != null && !userDetails.getMember().getRole().equals(Role.APPLICANT)) && parentCommentId == null; | ||
} | ||
|
||
|
||
@Override | ||
@Transactional | ||
public SuccessResponse<Message> createComment(UserDetailsImpl userDetails, CreateCommentReq createCommentReq) { | ||
Long postId = createCommentReq.getPostId(); | ||
Post post = postRepository.findById(postId) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 아이디의 게시글을 찾을 수 없습니다: " + postId)); | ||
|
||
Member member = memberRepository.findById(userDetails.getMember().getId()) | ||
.orElseThrow(() -> new IllegalArgumentException("해당되는 아이디의 회원이 존재하지 않습니다.")); | ||
|
||
Comment comment = Comment.memberCommentBuilder() | ||
.post(post) | ||
.member(member) | ||
.content(createCommentReq.getContent()) | ||
.nickname(createCommentReq.getNickname()) | ||
.avatarImage(createCommentReq.getAvatarImage()) | ||
.build(); | ||
|
||
commentRepository.save(comment); | ||
|
||
Message message = Message.builder() | ||
.message("댓글 작성이 완료되었습니다.") | ||
.build(); | ||
|
||
return SuccessResponse.of(message); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/mvp/deplog/domain/comment/application/CreateMemberReplyServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package mvp.deplog.domain.comment.application; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import mvp.deplog.domain.comment.domain.Comment; | ||
import mvp.deplog.domain.comment.domain.repository.CommentRepository; | ||
import mvp.deplog.domain.comment.dto.request.CreateCommentReq; | ||
import mvp.deplog.domain.member.domain.Member; | ||
import mvp.deplog.domain.member.domain.Role; | ||
import mvp.deplog.domain.member.domain.repository.MemberRepository; | ||
import mvp.deplog.domain.post.domain.Post; | ||
import mvp.deplog.domain.post.domain.repository.PostRepository; | ||
import mvp.deplog.global.common.Message; | ||
import mvp.deplog.global.common.SuccessResponse; | ||
import mvp.deplog.global.security.UserDetailsImpl; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
@Service | ||
public class CreateMemberReplyServiceImpl implements CreateCommentService { | ||
|
||
private final PostRepository postRepository; | ||
private final CommentRepository commentRepository; | ||
private final MemberRepository memberRepository; | ||
|
||
@Override | ||
public boolean supports(UserDetailsImpl userDetails, Long parentCommentId) { | ||
return (userDetails != null && !userDetails.getMember().getRole().equals(Role.APPLICANT)) && parentCommentId != null; | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public SuccessResponse<Message> createComment(UserDetailsImpl userDetails, CreateCommentReq createCommentReq) { | ||
Long postId = createCommentReq.getPostId(); | ||
Post post = postRepository.findById(postId) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 아이디의 게시글을 찾을 수 없습니다: " + postId)); | ||
|
||
Comment parentComment = commentRepository.findById(createCommentReq.getParentCommentId()) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 아이디의 댓글을 찾을 수 없습니다: " + createCommentReq.getParentCommentId())); | ||
|
||
Member member = memberRepository.findById(userDetails.getMember().getId()) | ||
.orElseThrow(() -> new IllegalArgumentException("해당되는 아이디의 회원이 존재하지 않습니다.")); | ||
|
||
Comment reply = Comment.memberReplyBuilder() | ||
.parentComment(parentComment) | ||
.post(post) | ||
.member(member) | ||
.content(createCommentReq.getContent()) | ||
.nickname(createCommentReq.getNickname()) | ||
.avatarImage(createCommentReq.getAvatarImage()) | ||
.build(); | ||
|
||
commentRepository.save(reply); | ||
|
||
Message message = Message.builder() | ||
.message("대댓글 작성이 완료되었습니다.") | ||
.build(); | ||
|
||
return SuccessResponse.of(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/resources/db/migration/V7__add_fk_comment_member.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
ALTER TABLE comment | ||
ADD COLUMN member_id BIGINT NULL; | ||
|
||
ALTER TABLE comment | ||
ADD CONSTRAINT fk_comment_to_member | ||
FOREIGN KEY (member_id) | ||
REFERENCES member (id); |