-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: 댓글과 사용자를 분리 #460
Merged
Merged
feat: 댓글과 사용자를 분리 #460
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fa3e2e7
refactor: comment 내에서 member의 객체 참조 -> id 참조로 변경
kong-hana01 8bc0b95
test: 커버링 인덱스 성능 테스트를 위한 쿼리 케이스 생성
kong-hana01 1f9321f
refactor: 사용하지 않는 쿼리 및 기능 삭제
kong-hana01 1fc5fa0
chore: 인덱스 추가
kong-hana01 300641a
test: 사용하지 않는 테스트 삭제
kong-hana01 933183d
refactor: comment 패키지 생성 및 의존성 분리
kong-hana01 d1bb360
test: comment 필드 변경으로 불필요한 테스트 로직 제거
kong-hana01 2627eea
refactor: dto를 interface로 프로젝션하도록 변경
kong-hana01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 6 additions & 5 deletions
11
...ginroom/controller/CommentController.java → ...comment/controller/CommentController.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
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
13 changes: 13 additions & 0 deletions
13
backend/src/main/java/com/digginroom/digginroom/comment/repository/dto/CommentMember.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,13 @@ | ||
package com.digginroom.digginroom.comment.repository.dto; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public record CommentMember( | ||
Long id, | ||
String comment, | ||
LocalDateTime createdAt, | ||
LocalDateTime updatedAt, | ||
Long memberId, | ||
String nickname | ||
) { | ||
} |
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 |
---|---|---|
@@ -1,18 +1,19 @@ | ||
package com.digginroom.digginroom.service; | ||
package com.digginroom.digginroom.comment.service; | ||
|
||
import static com.digginroom.digginroom.exception.CommentException.InvalidCommentSizeException; | ||
import static com.digginroom.digginroom.exception.CommentException.NotOwnerException; | ||
|
||
import com.digginroom.digginroom.domain.comment.Comment; | ||
import com.digginroom.digginroom.domain.member.Member; | ||
import com.digginroom.digginroom.comment.domain.Comment; | ||
import com.digginroom.digginroom.comment.repository.CommentRepository; | ||
import com.digginroom.digginroom.comment.repository.dto.CommentMember; | ||
import com.digginroom.digginroom.comment.service.dto.CommentRequest; | ||
import com.digginroom.digginroom.comment.service.dto.CommentResponse; | ||
import com.digginroom.digginroom.comment.service.dto.CommentsResponse; | ||
import com.digginroom.digginroom.exception.CommentException.InvalidLastCommentIdException; | ||
import com.digginroom.digginroom.exception.RoomException.NotFoundException; | ||
import com.digginroom.digginroom.repository.CommentRepository; | ||
import com.digginroom.digginroom.repository.MemberRepository; | ||
import com.digginroom.digginroom.repository.RoomRepository; | ||
import com.digginroom.digginroom.service.dto.CommentRequest; | ||
import com.digginroom.digginroom.service.dto.CommentResponse; | ||
import com.digginroom.digginroom.service.dto.CommentsResponse; | ||
import com.digginroom.digginroom.repository.dto.MemberNickname; | ||
import java.util.Objects; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.PageRequest; | ||
|
@@ -40,16 +41,13 @@ public CommentsResponse getRoomComments( | |
Long resolvedLastCommentId = getLastCommentId(lastCommentId); | ||
validateLastCommentId(resolvedLastCommentId); | ||
validateCommentSize(size); | ||
|
||
Member member = memberRepository.getMemberById(memberId); | ||
Slice<Comment> comments = commentRepository.getCommentsByCursor( | ||
Slice<CommentMember> commentMembers = commentRepository.getCommentsByCursor( | ||
roomId, | ||
resolvedLastCommentId, | ||
PageRequest.of(DEFAULT_PAGE_SIZE, size) | ||
); | ||
|
||
return new CommentsResponse(comments.getContent().stream() | ||
.map(comment -> CommentResponse.of(comment, comment.isOwner(member))) | ||
return new CommentsResponse(commentMembers.getContent().stream() | ||
.map(commentMember -> CommentResponse.of(commentMember, memberId)) | ||
.toList()); | ||
} | ||
|
||
|
@@ -74,12 +72,12 @@ private Long getLastCommentId(final Long lastCommentId) { | |
|
||
public CommentResponse comment(final Long roomId, final Long memberId, final CommentRequest request) { | ||
validateExistRoom(roomId); | ||
Member member = memberRepository.getMemberById(memberId); | ||
MemberNickname memberNickname = memberRepository.getMemberNickname(memberId); | ||
|
||
Comment comment = new Comment(roomId, request.comment(), member); | ||
Comment comment = new Comment(roomId, request.comment(), memberId); | ||
commentRepository.save(comment); | ||
Comment on lines
+75
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. : 더 재활용하기 쉬운 형태를 찾아보기로 함 |
||
|
||
return CommentResponse.of(comment, comment.isOwner(member)); | ||
return CommentResponse.of(comment, comment.isOwner(memberId), memberNickname.getNickname()); | ||
} | ||
|
||
public void validateExistRoom(final Long roomId) { | ||
|
@@ -89,15 +87,14 @@ public void validateExistRoom(final Long roomId) { | |
} | ||
|
||
public void delete(final Long memberId, final Long commentId) { | ||
Member member = memberRepository.getMemberById(memberId); | ||
Comment comment = commentRepository.getCommentById(commentId); | ||
|
||
validateSameOwner(member, comment); | ||
validateSameOwner(memberId, comment); | ||
commentRepository.delete(comment); | ||
} | ||
|
||
private void validateSameOwner(final Member member, final Comment comment) { | ||
if (!comment.isOwner(member)) { | ||
private void validateSameOwner(final Long memberId, final Comment comment) { | ||
if (!comment.isOwner(memberId)) { | ||
throw new NotOwnerException(); | ||
} | ||
} | ||
|
@@ -107,9 +104,9 @@ public CommentResponse update( | |
final Long commentId, | ||
final CommentRequest request | ||
) { | ||
Member member = memberRepository.getMemberById(memberId); | ||
MemberNickname memberNickname = memberRepository.getMemberNickname(memberId); | ||
Comment comment = commentRepository.getCommentById(commentId); | ||
comment.updateComment(request.comment(), member); | ||
return CommentResponse.of(comment, comment.isOwner(member)); | ||
comment.updateComment(request.comment(), memberId); | ||
return CommentResponse.of(comment, comment.isOwner(memberId), memberNickname.getNickname()); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...igginroom/service/dto/CommentRequest.java → ...m/comment/service/dto/CommentRequest.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
38 changes: 38 additions & 0 deletions
38
backend/src/main/java/com/digginroom/digginroom/comment/service/dto/CommentResponse.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,38 @@ | ||
package com.digginroom.digginroom.comment.service.dto; | ||
|
||
import com.digginroom.digginroom.comment.domain.Comment; | ||
import com.digginroom.digginroom.comment.repository.dto.CommentMember; | ||
import java.time.LocalDateTime; | ||
import java.util.Objects; | ||
|
||
public record CommentResponse( | ||
Long id, | ||
String writer, | ||
String comment, | ||
LocalDateTime createdAt, | ||
LocalDateTime updatedAt, | ||
boolean isOwner | ||
) { | ||
|
||
public static CommentResponse of(final CommentMember commentMember, final Long memberId) { | ||
return new CommentResponse( | ||
commentMember.id(), | ||
commentMember.nickname(), | ||
commentMember.comment(), | ||
commentMember.createdAt(), | ||
commentMember.updatedAt(), | ||
Objects.equals(commentMember.memberId(), memberId) | ||
); | ||
} | ||
|
||
public static CommentResponse of(final Comment comment, boolean isOwner, String nickname) { | ||
return new CommentResponse( | ||
comment.getId(), | ||
nickname, | ||
comment.getComment(), | ||
comment.getCreatedAt(), | ||
comment.getUpdatedAt(), | ||
isOwner | ||
); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ginroom/service/dto/CommentsResponse.java → ...comment/service/dto/CommentsResponse.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
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
9 changes: 9 additions & 0 deletions
9
backend/src/main/java/com/digginroom/digginroom/repository/dto/MemberNickname.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,9 @@ | ||
package com.digginroom.digginroom.repository.dto; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
|
||
public interface MemberNickname { | ||
|
||
@Value("#{target.nickname}") | ||
String getNickname(); | ||
} |
25 changes: 0 additions & 25 deletions
25
backend/src/main/java/com/digginroom/digginroom/service/dto/CommentResponse.java
This file was deleted.
Oops, something went wrong.
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
1 change: 1 addition & 0 deletions
1
backend/src/main/resources/db/migration/V6.4__create_index_room_id_id_from_comment.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 @@ | ||
CREATE INDEX COMMENT_ROOM_ID_COMMENT_ID_IDX ON comment (room_id, id desc); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 우리가 댓글을 Id 순으로 가져오고 있었군요 |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
: comment 와 member를 조인했으므로 commentMember