Skip to content

Commit

Permalink
feat: Question Member 연관관계 매핑, Question 단건, 전체 조회시 fetch join (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
toychip committed Feb 10, 2024
1 parent d31a1c2 commit ece6958
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ private List<TitleResponse> convertToTitleResponses(final List<HoneyTip> honeyTi
.toList();
}

public List<TitleResponse> getTop5() {
List<HoneyTip> top5HoneyTips = honeyTipDefaultRepository.getTop5();
return top5HoneyTips.stream()
.map(TitleResponse::honeyTipFrom)
.toList();
}

/* -------------------------------------------- 카토고리별 MAIN READ 끝 -------------------------------------------- */


/* -------------------------------------------- 좋아요 추가 & 취소 -------------------------------------------- */
@Transactional
public Message registerLike(final Long postId) {
honeyTipLikeService.register(postId);
Expand All @@ -201,11 +212,7 @@ public Message cancelLike(final Long postId) {
return Message.likePostCancel(HoneyTip.class, postId);
}

public List<TitleResponse> getTop5() {
List<HoneyTip> top5HoneyTips = honeyTipDefaultRepository.getTop5();
return top5HoneyTips.stream()
.map(TitleResponse::honeyTipFrom)
.toList();
/* -------------------------------------------- 좋아요 추가 & 취소 끝 -------------------------------------------- */


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,6 +56,9 @@ public class Member {
@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<HoneyTip> honeyTips = new ArrayList<>();

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Question> questions = new ArrayList<>();

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<HoneyTipLike> honeyTipLikes = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -45,6 +54,7 @@ public static Question from(final QuestionCreateRequest request) {
.category(request.getCategory())
.content(request.getContent())
.title(request.getTitle())
.member(getCurrentMember())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,34 @@ public class QuestionSingleResponse {
@Schema(description = "질문 카테고리")
private Category category;

@Schema(description = "댓글 수")
private int commentCount;

@Schema(description = "질문에 포함된 이미지 URL 목록")
private List<ImageResponse> imageUrls;

@Schema(description = "질문에 대한 댓글 목록")
private List<CommentResponse> commentResponses;

public static QuestionSingleResponse of(final Question question, final List<QuestionComment> activeComments) {

// 시간 포멧팅
String formattedCreatedDate = getFormattedCreatedDate(question);

// CommunityImage entity to Response
List<ImageResponse> imageResponses = getImageResponses(question);

// Comment entity to Response
List<CommentResponse> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,6 +24,7 @@ public List<Question> getHouseWork() {
.from(question)
.distinct()
.leftJoin(question.questionComments, questionComment)
.leftJoin(question.member, member)
.fetchJoin()
.where(question.category.eq(Category.HOUSEWORK))
.limit(10)
Expand All @@ -35,6 +38,7 @@ public List<Question> getRecipe() {
.from(question)
.distinct()
.leftJoin(question.questionComments, questionComment)
.leftJoin(question.member, member)
.fetchJoin()
.where(question.category.eq(Category.RECIPE))
.limit(10)
Expand All @@ -48,6 +52,7 @@ public List<Question> getSafeLiving() {
.from(question)
.distinct()
.leftJoin(question.questionComments, questionComment)
.leftJoin(question.member, member)
.fetchJoin()
.where(question.category.eq(Category.SAFE_LIVING))
.limit(10)
Expand All @@ -61,6 +66,7 @@ public List<Question> getWelfarePolicy() {
.from(question)
.distinct()
.leftJoin(question.questionComments, questionComment)
.leftJoin(question.member, member)
.fetchJoin()
.where(question.category.eq(Category.WELFARE_POLICY))
.limit(10)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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))
Expand Down

0 comments on commit ece6958

Please sign in to comment.