Skip to content

Commit

Permalink
[refactor]: fetch join 함수 repository로 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
hyun2371 committed Nov 23, 2024
1 parent ba52924 commit 8086fe9
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dnd.gongmuin.answer.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -21,4 +22,8 @@ public interface AnswerRepository extends JpaRepository<Answer, Long> {
@Modifying(flushAutomatically = true, clearAutomatically = true)
@Query("UPDATE Answer a SET a.member = :member WHERE a.member.id = :memberId")
void updateAnswersMember(Long memberId, Member member);

@Query("select a from Answer a "
+ "join fetch a.member where a.id = :answerId")
Optional<Answer> findByIdWithMember(Long answerId);
}
16 changes: 6 additions & 10 deletions src/main/java/com/dnd/gongmuin/answer/service/AnswerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.dnd.gongmuin.answer.dto.RegisterAnswerRequest;
import com.dnd.gongmuin.answer.exception.AnswerErrorCode;
import com.dnd.gongmuin.answer.repository.AnswerRepository;
import com.dnd.gongmuin.answer.repository.AnswerSimpleQueryRepository;
import com.dnd.gongmuin.common.dto.PageMapper;
import com.dnd.gongmuin.common.dto.PageResponse;
import com.dnd.gongmuin.common.exception.runtime.NotFoundException;
Expand All @@ -24,7 +23,6 @@
import com.dnd.gongmuin.question_post.domain.QuestionPost;
import com.dnd.gongmuin.question_post.exception.QuestionPostErrorCode;
import com.dnd.gongmuin.question_post.repository.QuestionPostRepository;
import com.dnd.gongmuin.question_post.repository.QuestionPostSimpleQueryRepository;

import lombok.RequiredArgsConstructor;

Expand All @@ -36,8 +34,6 @@ public class AnswerService {
private final AnswerRepository answerRepository;
private final CreditHistoryService creditHistoryService;
private final ApplicationEventPublisher eventPublisher;
private final QuestionPostSimpleQueryRepository questionPostSimpleQueryRepository;
private final AnswerSimpleQueryRepository answerSimpleQueryRepository;

private static void validateIfQuestioner(Member member, QuestionPost questionPost) {
if (!questionPost.isQuestioner(member.getId())) {
Expand Down Expand Up @@ -77,8 +73,8 @@ public AnswerDetailResponse chooseAnswer(
Long answerId,
Member member
) {
Answer answer = getAnswerById(answerId);
QuestionPost questionPost = findQuestionPostById(answer.getQuestionPostId());
Answer answer = getAnswerWithMember(answerId);
QuestionPost questionPost = getQuestionPostWithMember(answer.getQuestionPostId());
validateIfQuestioner(member, questionPost);
chooseAnswer(questionPost, answer);

Expand All @@ -103,8 +99,8 @@ private void validateIfQuestionPostExists(Long questionPostId) {
}
}

private Answer getAnswerById(Long answerId) {
return answerSimpleQueryRepository.findAnswerById(answerId)
private Answer getAnswerWithMember(Long answerId) {
return answerRepository.findByIdWithMember(answerId)
.orElseThrow(() -> new NotFoundException(AnswerErrorCode.NOT_FOUND_ANSWER));
}

Expand All @@ -113,8 +109,8 @@ private QuestionPost getQuestionPostById(Long questionPostId) {
.orElseThrow(() -> new NotFoundException(QuestionPostErrorCode.NOT_FOUND_QUESTION_POST));
}

private QuestionPost findQuestionPostById(Long questionPostId) {
return questionPostSimpleQueryRepository.findQuestionPostById(questionPostId)
private QuestionPost getQuestionPostWithMember(Long questionPostId) {
return questionPostRepository.findByIdWithMember(questionPostId)
.orElseThrow(() -> new NotFoundException(QuestionPostErrorCode.NOT_FOUND_QUESTION_POST));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.dnd.gongmuin.chat_inquiry.dto;

import com.dnd.gongmuin.chat_inquiry.domain.InquiryStatus;
import com.dnd.gongmuin.member.domain.JobGroup;
import com.dnd.gongmuin.member.dto.response.MemberInfo;
import com.dnd.gongmuin.chat_inquiry.domain.ChatInquiry;
import com.dnd.gongmuin.member.domain.Member;
import com.dnd.gongmuin.member.dto.response.MemberInfo;
import com.querydsl.core.annotations.QueryProjection;

public record ChatInquiryResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dnd.gongmuin.question_post.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
Expand All @@ -16,7 +17,11 @@ public interface QuestionPostRepository extends JpaRepository<QuestionPost, Long

List<QuestionPost> findAllByMember(Member member);

@Query("select q from QuestionPost q "
+ "join fetch q.member where q.id = :questionPostId")
Optional<QuestionPost> findByIdWithMember(Long questionPostId);

@Modifying(flushAutomatically = true, clearAutomatically = true)
@Query("UPDATE QuestionPost q SET q.member = :member WHERE q.member.id = :memberId")
public void updateQuestionPostsMember(Long memberId, Member member);
void updateQuestionPostsMember(Long memberId, Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ void chooseAnswer() throws Exception {
= questionPostRepository.save(QuestionPostFixture.questionPost(loginMember));
Member answerer = memberRepository.save(MemberFixture.member2());
Answer answer = answerRepository.save(AnswerFixture.answer(questionPost.getId(), answerer));
long startTime = System.currentTimeMillis();

mockMvc.perform(post("/api/question-posts/answers/{answerId}", answer.getId())
.cookie(accessToken)
Expand All @@ -138,7 +137,5 @@ void chooseAnswer() throws Exception {
.andExpect(jsonPath("$.memberInfo.nickname").value(answerer.getNickname()))
.andExpect(jsonPath("$.memberInfo.memberJobGroup").value(answerer.getJobGroup().getLabel())
);
long endTime = System.currentTimeMillis();
System.out.println("Execution time: " + (endTime - startTime) + " ms");
}
}

0 comments on commit 8086fe9

Please sign in to comment.