From ece6958ecaff90e7054a01da16688830b2b435d1 Mon Sep 17 00:00:00 2001 From: toychip Date: Sat, 10 Feb 2024 16:03:06 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Question=20Member=20=EC=97=B0=EA=B4=80?= =?UTF-8?q?=EA=B4=80=EA=B3=84=20=EB=A7=A4=ED=95=91,=20Question=20=EB=8B=A8?= =?UTF-8?q?=EA=B1=B4,=20=EC=A0=84=EC=B2=B4=20=EC=A1=B0=ED=9A=8C=EC=8B=9C?= =?UTF-8?q?=20fetch=20join=20(#51)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/service/HoneyTipPostService.java | 19 +++++++++++++------ .../main/dto/response/TitleResponse.java | 6 ++++-- .../ttoklip/domain/member/domain/Member.java | 4 ++++ .../domain/question/post/domain/Question.java | 10 ++++++++++ .../dto/response/QuestionSingleResponse.java | 10 +++++++--- .../repository/QuestionDefaultRepository.java | 6 ++++++ .../repository/QuestionRepositoryImpl.java | 2 ++ 7 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/api/ttoklip/domain/honeytip/post/service/HoneyTipPostService.java b/src/main/java/com/api/ttoklip/domain/honeytip/post/service/HoneyTipPostService.java index d2cbf9fe..94473d77 100644 --- a/src/main/java/com/api/ttoklip/domain/honeytip/post/service/HoneyTipPostService.java +++ b/src/main/java/com/api/ttoklip/domain/honeytip/post/service/HoneyTipPostService.java @@ -189,6 +189,17 @@ private List convertToTitleResponses(final List honeyTi .toList(); } + public List getTop5() { + List top5HoneyTips = honeyTipDefaultRepository.getTop5(); + return top5HoneyTips.stream() + .map(TitleResponse::honeyTipFrom) + .toList(); + } + + /* -------------------------------------------- 카토고리별 MAIN READ 끝 -------------------------------------------- */ + + + /* -------------------------------------------- 좋아요 추가 & 취소 -------------------------------------------- */ @Transactional public Message registerLike(final Long postId) { honeyTipLikeService.register(postId); @@ -201,11 +212,7 @@ public Message cancelLike(final Long postId) { return Message.likePostCancel(HoneyTip.class, postId); } - public List getTop5() { - List top5HoneyTips = honeyTipDefaultRepository.getTop5(); - return top5HoneyTips.stream() - .map(TitleResponse::honeyTipFrom) - .toList(); + /* -------------------------------------------- 좋아요 추가 & 취소 끝 -------------------------------------------- */ + - } } diff --git a/src/main/java/com/api/ttoklip/domain/main/dto/response/TitleResponse.java b/src/main/java/com/api/ttoklip/domain/main/dto/response/TitleResponse.java index c1248d8a..b099124b 100644 --- a/src/main/java/com/api/ttoklip/domain/main/dto/response/TitleResponse.java +++ b/src/main/java/com/api/ttoklip/domain/main/dto/response/TitleResponse.java @@ -23,13 +23,15 @@ public class TitleResponse { public static TitleResponse questionOf(final Question question) { LocalDateTime createdDate = question.getCreatedDate(); String formattedCreatedDate = getFormattedCreatedDate(createdDate); + int commentCount = question.getQuestionComments().size(); + String writer = question.getMember().getName(); return TitleResponse.builder() .honeyTipId(question.getId()) .title(question.getTitle()) .content(question.getContent()) -// .writer(quesion.member.getName()) - .commentCount(question.getQuestionComments().size()) + .writer(writer) + .commentCount(commentCount) .writtenTime(formattedCreatedDate) .build(); } diff --git a/src/main/java/com/api/ttoklip/domain/member/domain/Member.java b/src/main/java/com/api/ttoklip/domain/member/domain/Member.java index 0cdcb2ec..a933d3d9 100644 --- a/src/main/java/com/api/ttoklip/domain/member/domain/Member.java +++ b/src/main/java/com/api/ttoklip/domain/member/domain/Member.java @@ -4,6 +4,7 @@ import com.api.ttoklip.domain.honeytip.like.domain.HoneyTipLike; import com.api.ttoklip.domain.honeytip.post.domain.HoneyTip; import com.api.ttoklip.domain.profile.domain.Profile; +import com.api.ttoklip.domain.question.post.domain.Question; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; import jakarta.persistence.EnumType; @@ -55,6 +56,9 @@ public class Member { @OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true) private List honeyTips = new ArrayList<>(); + @OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true) + private List questions = new ArrayList<>(); + @OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true) private List honeyTipLikes = new ArrayList<>(); diff --git a/src/main/java/com/api/ttoklip/domain/question/post/domain/Question.java b/src/main/java/com/api/ttoklip/domain/question/post/domain/Question.java index 784939a5..1d389e2c 100644 --- a/src/main/java/com/api/ttoklip/domain/question/post/domain/Question.java +++ b/src/main/java/com/api/ttoklip/domain/question/post/domain/Question.java @@ -1,8 +1,11 @@ package com.api.ttoklip.domain.question.post.domain; +import static com.api.ttoklip.global.util.SecurityUtil.getCurrentMember; + import com.api.ttoklip.domain.common.Category; import com.api.ttoklip.domain.common.base.BaseEntity; import com.api.ttoklip.domain.common.report.domain.Report; +import com.api.ttoklip.domain.member.domain.Member; import com.api.ttoklip.domain.question.comment.domain.QuestionComment; import com.api.ttoklip.domain.question.image.domain.QuestionImage; import com.api.ttoklip.domain.question.post.dto.request.QuestionCreateRequest; @@ -14,6 +17,8 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; import java.util.ArrayList; import java.util.List; @@ -37,6 +42,10 @@ public class Question extends BaseEntity { @Enumerated(value = EnumType.STRING) private Category category; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "member_id") + private Member member; + private String title; private String content; @@ -45,6 +54,7 @@ public static Question from(final QuestionCreateRequest request) { .category(request.getCategory()) .content(request.getContent()) .title(request.getTitle()) + .member(getCurrentMember()) .build(); } diff --git a/src/main/java/com/api/ttoklip/domain/question/post/dto/response/QuestionSingleResponse.java b/src/main/java/com/api/ttoklip/domain/question/post/dto/response/QuestionSingleResponse.java index 7549f5bf..acdbf959 100644 --- a/src/main/java/com/api/ttoklip/domain/question/post/dto/response/QuestionSingleResponse.java +++ b/src/main/java/com/api/ttoklip/domain/question/post/dto/response/QuestionSingleResponse.java @@ -38,6 +38,9 @@ public class QuestionSingleResponse { @Schema(description = "질문 카테고리") private Category category; + @Schema(description = "댓글 수") + private int commentCount; + @Schema(description = "질문에 포함된 이미지 URL 목록") private List imageUrls; @@ -45,23 +48,24 @@ public class QuestionSingleResponse { private List commentResponses; public static QuestionSingleResponse of(final Question question, final List activeComments) { - // 시간 포멧팅 String formattedCreatedDate = getFormattedCreatedDate(question); // CommunityImage entity to Response List imageResponses = getImageResponses(question); - // Comment entity to Response List commentResponses = getCommentResponses(activeComments); + int commentCount = commentResponses.size(); + return QuestionSingleResponse.builder() .questionId(question.getId()) .title(question.getTitle()) .content(question.getContent()) -// .writer(question.getMember().getName) ToDo Member 엔티티 연결 후 수정 + .writer(question.getMember().getName()) .writtenTime(formattedCreatedDate) .category(question.getCategory()) // 한글 카테고리 이름으로 반환 + .commentCount(commentCount) .imageUrls(imageResponses) .commentResponses(commentResponses) .build(); diff --git a/src/main/java/com/api/ttoklip/domain/question/post/repository/QuestionDefaultRepository.java b/src/main/java/com/api/ttoklip/domain/question/post/repository/QuestionDefaultRepository.java index 37b73e3e..0bd64a68 100644 --- a/src/main/java/com/api/ttoklip/domain/question/post/repository/QuestionDefaultRepository.java +++ b/src/main/java/com/api/ttoklip/domain/question/post/repository/QuestionDefaultRepository.java @@ -1,9 +1,11 @@ package com.api.ttoklip.domain.question.post.repository; +import static com.api.ttoklip.domain.member.domain.QMember.*; import static com.api.ttoklip.domain.question.comment.domain.QQuestionComment.questionComment; import static com.api.ttoklip.domain.question.post.domain.QQuestion.question; import com.api.ttoklip.domain.common.Category; +import com.api.ttoklip.domain.member.domain.QMember; import com.api.ttoklip.domain.question.post.domain.Question; import com.querydsl.jpa.impl.JPAQueryFactory; import java.util.List; @@ -22,6 +24,7 @@ public List getHouseWork() { .from(question) .distinct() .leftJoin(question.questionComments, questionComment) + .leftJoin(question.member, member) .fetchJoin() .where(question.category.eq(Category.HOUSEWORK)) .limit(10) @@ -35,6 +38,7 @@ public List getRecipe() { .from(question) .distinct() .leftJoin(question.questionComments, questionComment) + .leftJoin(question.member, member) .fetchJoin() .where(question.category.eq(Category.RECIPE)) .limit(10) @@ -48,6 +52,7 @@ public List getSafeLiving() { .from(question) .distinct() .leftJoin(question.questionComments, questionComment) + .leftJoin(question.member, member) .fetchJoin() .where(question.category.eq(Category.SAFE_LIVING)) .limit(10) @@ -61,6 +66,7 @@ public List getWelfarePolicy() { .from(question) .distinct() .leftJoin(question.questionComments, questionComment) + .leftJoin(question.member, member) .fetchJoin() .where(question.category.eq(Category.WELFARE_POLICY)) .limit(10) diff --git a/src/main/java/com/api/ttoklip/domain/question/post/repository/QuestionRepositoryImpl.java b/src/main/java/com/api/ttoklip/domain/question/post/repository/QuestionRepositoryImpl.java index 47689274..9e3cb04f 100644 --- a/src/main/java/com/api/ttoklip/domain/question/post/repository/QuestionRepositoryImpl.java +++ b/src/main/java/com/api/ttoklip/domain/question/post/repository/QuestionRepositoryImpl.java @@ -1,5 +1,6 @@ package com.api.ttoklip.domain.question.post.repository; +import static com.api.ttoklip.domain.member.domain.QMember.member; import static com.api.ttoklip.domain.question.comment.domain.QQuestionComment.questionComment; import static com.api.ttoklip.domain.question.image.domain.QQuestionImage.questionImage; import static com.api.ttoklip.domain.question.post.domain.QQuestion.question; @@ -24,6 +25,7 @@ public Question findByIdFetchJoin(final Long questionPostId) { Question findQuestion = jpaQueryFactory .selectFrom(question) .distinct() + .leftJoin(question.member, member) .leftJoin(question.questionImages, questionImage) .fetchJoin() .where(question.id.eq(questionPostId))