Skip to content

Commit e74987f

Browse files
authored
refacor: 댓글/대댓글 쿼리문에서 dto로 받기 (#127)
* refacor: 댓글/대댓글 쿼리문에서 dto로 받기 * refacor: 댓글/대댓글 쿼리문에서 dto로 받기 * refactor: 좋아요 수에 따른 정렬 추가 * Feat: 자체엑세스토큰을 통해 로그인 (#124) * chore: 디렉토리 구조 수정 * feat: login By accessToken * remove: todo 주석 삭제 * refacor: 댓글/대댓글 쿼리문에서 dto로 받기 * refactor: 문자열 변수로 변경 * refactor: WriterResponse 추가
1 parent a56a913 commit e74987f

File tree

11 files changed

+303
-78
lines changed

11 files changed

+303
-78
lines changed

src/main/java/com/flint/flint/community/controller/PostCommentGetController.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.flint.flint.community.controller;
22

33
import com.flint.flint.common.ResponseForm;
4-
import com.flint.flint.community.dto.response.PostCommentGetResponse;
4+
import com.flint.flint.community.dto.response.PostCommentResponse;
55
import com.flint.flint.community.service.PostCommentGetService;
66
import lombok.RequiredArgsConstructor;
77
import org.springframework.security.access.prepost.PreAuthorize;
@@ -28,7 +28,7 @@ public class PostCommentGetController {
2828
* 특정 포스트 댓글/대댓글 조회
2929
*/
3030
@GetMapping("/{postId}")
31-
public ResponseForm<List<PostCommentGetResponse>> getPostComment(@PathVariable long postId) {
31+
public ResponseForm<List<PostCommentResponse>> getPostComment(@PathVariable long postId) {
3232
return new ResponseForm<>(postCommentGeteService.getPostComment(postId));
3333
}
3434
}

src/main/java/com/flint/flint/community/domain/post/PostComment.java

+1
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,5 @@ public void addCommentReply(PostComment reply) {
6464
public void updateContent(String contents) {
6565
this.contents = contents;
6666
}
67+
6768
}

src/main/java/com/flint/flint/community/dto/response/PostCommentGetResponse.java

-26
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.flint.flint.community.dto.response;
2+
3+
import com.querydsl.core.annotations.QueryProjection;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.io.Writer;
9+
import java.time.LocalDateTime;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
@Data
14+
@AllArgsConstructor
15+
@NoArgsConstructor
16+
public class PostCommentResponse {
17+
18+
private Long parentCommentId;
19+
private long postCommentId;
20+
private String contents;
21+
private LocalDateTime createdAt;
22+
private long memberId;
23+
private Long likeCount;
24+
private String logoUrl;
25+
private WriterResponse writerResponse;
26+
private List<PostCommentResponse> replies = new ArrayList<>();
27+
28+
@QueryProjection
29+
public PostCommentResponse(Long parentCommentId, long postCommentId, String contents, LocalDateTime createdAt, Long likeCount, WriterResponse writerResponse) {
30+
this.parentCommentId = parentCommentId;
31+
this.postCommentId = postCommentId;
32+
this.contents = contents;
33+
this.createdAt = createdAt;
34+
this.likeCount = likeCount;
35+
this.writerResponse = writerResponse;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
package com.flint.flint.community.dto.response;
22

3-
import lombok.AllArgsConstructor;
3+
import com.querydsl.core.annotations.QueryProjection;
44
import lombok.Builder;
55
import lombok.Data;
66
import lombok.NoArgsConstructor;
77

88
@Data
99
@Builder
1010
@NoArgsConstructor
11-
@AllArgsConstructor
1211
public class WriterResponse {
1312
private Long memberId;
1413
private String universityLogoUrl;
1514
private String universityName;
1615
private String major;
16+
17+
@QueryProjection
18+
public WriterResponse(Long memberId, String universityLogoUrl, String universityName, String major) {
19+
this.memberId = memberId;
20+
this.universityLogoUrl = universityLogoUrl;
21+
this.universityName = universityName;
22+
this.major = major;
23+
}
1724
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.flint.flint.community.repository.post_comment;
22

3-
import com.flint.flint.community.domain.post.PostComment;
3+
import com.flint.flint.community.dto.response.PostCommentResponse;
44
import org.springframework.stereotype.Repository;
55

66
import java.util.List;
@@ -9,7 +9,8 @@
99
* @author 정순원
1010
* @since 2023-11-06
1111
*/
12+
1213
@Repository
1314
public interface PostCommentRepositoryCustom {
14-
List<PostComment> findByPostId(Long postId);
15+
List<PostCommentResponse> findByPostId(Long postId);
1516
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
package com.flint.flint.community.repository.post_comment;
22

3-
import com.flint.flint.community.domain.post.PostComment;
3+
import com.flint.flint.community.dto.response.PostCommentResponse;
4+
import com.flint.flint.community.dto.response.QPostCommentResponse;
5+
import com.flint.flint.community.dto.response.QWriterResponse;
6+
import com.flint.flint.community.dto.response.WriterResponse;
7+
import com.querydsl.core.annotations.QueryProjection;
8+
import com.querydsl.jpa.JPAExpressions;
9+
import com.querydsl.jpa.JPQLQuery;
410
import com.querydsl.jpa.impl.JPAQueryFactory;
511
import lombok.RequiredArgsConstructor;
612
import org.springframework.stereotype.Repository;
713

14+
import java.time.LocalDateTime;
815
import java.util.List;
916

17+
import static com.flint.flint.asset.domain.QUniversityAsset.universityAsset;
1018
import static com.flint.flint.community.domain.post.QPostComment.postComment;
19+
import static com.flint.flint.community.domain.post.QPostCommentLike.postCommentLike;
20+
import static com.flint.flint.idcard.domain.QIdCard.idCard;
1121

1222
/**
1323
* @author 정순원
@@ -20,14 +30,28 @@ public class PostCommentRepositoryImpl implements PostCommentRepositoryCustom {
2030
private final JPAQueryFactory queryFactory;
2131

2232
@Override
23-
public List<PostComment> findByPostId(Long postId) {
33+
public List<PostCommentResponse> findByPostId(Long postId) {
34+
JPQLQuery<Long> likeCountSubQuery = JPAExpressions
35+
.select(postCommentLike.count())
36+
.from(postCommentLike)
37+
.where(postCommentLike.postComment.eq(postComment));
38+
2439
return queryFactory
25-
.selectFrom(postComment)
26-
.join(postComment.parentComment).fetchJoin()
27-
.where(postComment.post.id.eq(postId))
28-
.orderBy(postComment.parentComment.id.asc().nullsFirst(),
29-
postComment.createdAt.asc())
40+
.select(new QPostCommentResponse(
41+
postComment.parentComment.id,
42+
postComment.id,
43+
postComment.contents,
44+
postComment.createdAt,
45+
likeCountSubQuery,
46+
new QWriterResponse(
47+
idCard.member.id,
48+
universityAsset.logoUrl,
49+
idCard.university,
50+
idCard.major)))
51+
.from(postComment, idCard)
52+
.innerJoin(universityAsset).on(universityAsset.universityName.eq(idCard.university))
53+
.where(postComment.post.id.eq(postId).and(idCard.member.id.eq(postComment.member.id)))
54+
.orderBy(postComment.createdAt.asc())
3055
.fetch();
3156
}
32-
33-
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.flint.flint.community.service;
22

3-
import com.flint.flint.community.domain.post.PostComment;
4-
import com.flint.flint.community.dto.response.PostCommentGetResponse;
3+
4+
import com.flint.flint.community.dto.response.PostCommentResponse;
55
import com.flint.flint.community.repository.post_comment.PostCommentRepositoryCustom;
66
import lombok.RequiredArgsConstructor;
77
import org.springframework.stereotype.Service;
@@ -17,29 +17,65 @@
1717
@RequiredArgsConstructor
1818
public class PostCommentGetService {
1919

20+
private final static int PRIORITY_LIKE_CRITERIA = 3;
2021
private final PostCommentRepositoryCustom postCommentRepositoryCustom;
2122

22-
@Transactional
23-
public List<PostCommentGetResponse> getPostComment(long postId) {
24-
List<PostComment> postCommentList = postCommentRepositoryCustom.findByPostId(postId);
23+
@Transactional(readOnly = true)
24+
public List<PostCommentResponse> getPostComment(long postId) {
25+
List<PostCommentResponse> commentResponses = postCommentRepositoryCustom.findByPostId(postId);
26+
27+
// 공감수에 따라 댓글 분류
28+
List<PostCommentResponse> highLikesParentComments = new ArrayList<>();
29+
List<PostCommentResponse> lowLikesParentComments = new ArrayList<>();
30+
List<PostCommentResponse> highLikesChildComments = new ArrayList<>();
31+
List<PostCommentResponse> lowLikesChildComments = new ArrayList<>();
32+
33+
for (PostCommentResponse comment : commentResponses) {
34+
if (comment.getParentCommentId() == null) {
35+
if (comment.getLikeCount() != null && comment.getLikeCount() >= PRIORITY_LIKE_CRITERIA) {
36+
highLikesParentComments.add(comment);
37+
} else {
38+
lowLikesParentComments.add(comment);
39+
}
40+
} else {
41+
if (comment.getLikeCount() != null && comment.getLikeCount() >= PRIORITY_LIKE_CRITERIA) {
42+
highLikesChildComments.add(comment);
43+
} else {
44+
lowLikesChildComments.add(comment);
45+
}
46+
}
47+
}
48+
49+
// 공감수가 높은 댓글을 공감수 내림차순으로 정렬
50+
highLikesParentComments.sort(Comparator.comparing(PostCommentResponse::getLikeCount).reversed());
51+
highLikesChildComments.sort(Comparator.comparing(PostCommentResponse::getLikeCount).reversed());
52+
53+
// 공감수가 낮은 댓글을 생성 시간 오름차순으로 정렬
54+
lowLikesParentComments.sort(Comparator.comparing(PostCommentResponse::getCreatedAt).reversed());
55+
lowLikesChildComments.sort(Comparator.comparing(PostCommentResponse::getCreatedAt).reversed());
2556

26-
List<PostCommentGetResponse> responseList = new ArrayList<>();
27-
Map<Long, PostCommentGetResponse> responseHashMap = new HashMap<>();
2857

29-
postCommentList.forEach(postComment -> {
30-
PostCommentGetResponse postCommentGetResponse = PostCommentGetResponse.convertCommentToDTO(postComment);
31-
responseHashMap.put(postCommentGetResponse.getPostCommentId(), postCommentGetResponse);
58+
// 최종 결과 리스트 생성
59+
List<PostCommentResponse> sortedComments = new ArrayList<>();
60+
sortedComments.addAll(highLikesParentComments);
61+
sortedComments.addAll(lowLikesParentComments);
62+
sortedComments.addAll(highLikesChildComments);
63+
sortedComments.addAll(lowLikesChildComments);
3264

33-
// 대댓글인 경우 부모 댓글의 replies에 추가
34-
Optional.ofNullable(postComment.getParentComment())
35-
.map(parent -> responseHashMap.get(parent.getId()))
36-
.ifPresent(parentResponse -> parentResponse.getReplies().add(postCommentGetResponse));
65+
// 대댓글 구조 재구성
66+
Map<Long, PostCommentResponse> commentMap = new HashMap<>();
67+
List<PostCommentResponse> parentCommentResponses = new ArrayList<>();
3768

38-
// 댓글인 경우 ResponseList에 추가
39-
if (postComment.getParentComment() == null) {
40-
responseList.add(postCommentGetResponse);
69+
for (PostCommentResponse commentResponse : sortedComments) {
70+
commentMap.put(commentResponse.getPostCommentId(), commentResponse);
71+
if (commentResponse.getParentCommentId() == null) {
72+
parentCommentResponses.add(commentResponse);
73+
} else {
74+
if (commentMap.containsKey(commentResponse.getParentCommentId())) {
75+
commentMap.get(commentResponse.getParentCommentId()).getReplies().add(commentResponse);
76+
}
4177
}
42-
});
43-
return responseList;
78+
}
79+
return parentCommentResponses;
4480
}
45-
}
81+
}

src/main/java/com/flint/flint/security/oauth/KakaoOAuth2UserAttribute.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Member toEntity() {
3333

3434
return Member.builder()
3535
.providerName(KAKAO_PROVIDER_ID)
36-
.providerId(getProviderId())//띄어쓰기 포함
36+
.providerId(getProviderId())
3737
.email(getEmail())
3838
.name(getName())
3939
.gender(Gender.valueOf(getGender().toUpperCase())) //대소문자 구별하니 바꿔줘야 함
@@ -71,7 +71,6 @@ public String getBirthday() {
7171
@Override
7272
public void setUserAttributesByOauthToken(String kakaoAccessToken) {
7373

74-
7574
JSONObject response = WebClient.create()
7675
.get()
7776
.uri("https://kapi.kakao.com/v2/user/me")

src/main/java/com/flint/flint/security/oauth/NaverOAuth2UserAttribute.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Member toEntity() {
4242

4343
@Override
4444
public String getProviderId() {
45-
return "naver " + response.get("id").toString();
45+
return NAVER_PROVIDER_ID + "_" + response.get("id").toString();
4646
}
4747

4848
@Override

0 commit comments

Comments
 (0)