Skip to content
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

[refactor#148] 답변 채택 API 성능 최적화 #155

Merged
merged 17 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public ResponseEntity<PageResponse<AnswerDetailResponse>> getAnswersByQuestionPo
@ApiResponse(useReturnTypeSchema = true)
@PostMapping("/api/question-posts/answers/{answerId}")
public ResponseEntity<AnswerDetailResponse> getAnswersByQuestionPostId(
@PathVariable Long answerId,
@PathVariable("answerId") Long answerId,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정 감사합니다~

@AuthenticationPrincipal Member member
) {
AnswerDetailResponse response = answerService.chooseAnswer(answerId, member);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.dnd.gongmuin.answer.dto;

import com.dnd.gongmuin.question_post.dto.response.MemberInfo;
import com.dnd.gongmuin.member.dto.response.MemberInfo;

public record AnswerDetailResponse(
Long answerId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.dnd.gongmuin.answer.domain.Answer;
import com.dnd.gongmuin.member.domain.Member;
import com.dnd.gongmuin.question_post.dto.response.MemberInfo;
import com.dnd.gongmuin.member.dto.response.MemberInfo;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand Down
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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.dnd.gongmuin.answer.repository;

import java.util.Optional;

import org.springframework.stereotype.Repository;

import com.dnd.gongmuin.answer.domain.Answer;

import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;

@Repository
@RequiredArgsConstructor
public class AnswerSimpleQueryRepository {

private final EntityManager em;

public Optional<Answer> findAnswerById(Long answerId) {
Answer answer = em.createQuery(
"select a from Answer a" +
" join fetch a.member m" +
" where a.id = :answerId", Answer.class
)
.setParameter("answerId", answerId)
.getSingleResult();
return Optional.of(answer);
}
}
17 changes: 11 additions & 6 deletions src/main/java/com/dnd/gongmuin/answer/service/AnswerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public AnswerDetailResponse registerAnswer(
RegisterAnswerRequest request,
Member member
) {
QuestionPost questionPost = findQuestionPostById(questionPostId);
QuestionPost questionPost = getQuestionPostById(questionPostId);
Answer answer = AnswerMapper.toAnswer(questionPostId, questionPost.isQuestioner(member.getId()), request,
member);
Answer savedAnswer = answerRepository.save(answer);
Expand All @@ -73,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 @@ -99,13 +99,18 @@ private void validateIfQuestionPostExists(Long questionPostId) {
}
}

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

private QuestionPost findQuestionPostById(Long questionPostId) {
private QuestionPost getQuestionPostById(Long questionPostId) {
return questionPostRepository.findById(questionPostId)
.orElseThrow(() -> new NotFoundException(QuestionPostErrorCode.NOT_FOUND_QUESTION_POST));
}

private QuestionPost getQuestionPostWithMember(Long questionPostId) {
return questionPostRepository.findByIdWithMember(questionPostId)
.orElseThrow(() -> new NotFoundException(QuestionPostErrorCode.NOT_FOUND_QUESTION_POST));
}
}
14 changes: 7 additions & 7 deletions src/main/java/com/dnd/gongmuin/auth/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ public TempSignResponse tempSignUp(TempSignUpRequest tempSignUpRequest, HttpServ
throw new NotFoundException(MemberErrorCode.NOT_FOUND_MEMBER);
}

memberRepository.save(member);
Member savedMember = memberRepository.save(member);

AuthInfo authInfo = AuthInfo.of(member.getSocialName(), member.getSocialEmail(), member.getRole());
CustomOauth2User customOauth2User = new CustomOauth2User(authInfo);

tokenProvider.generateRefreshToken(customOauth2User, now);
String accessToken = tokenProvider.generateAccessToken(customOauth2User, now);
tokenProvider.generateRefreshToken(savedMember, customOauth2User, now);
String accessToken = tokenProvider.generateAccessToken(savedMember, customOauth2User, now);
response.addCookie(cookieUtil.createCookie(accessToken));

return new TempSignResponse(true);
Expand All @@ -98,8 +98,8 @@ public TempSignResponse tempSignIn(TempSignInRequest tempSignInRequest, HttpServ
AuthInfo authInfo = AuthInfo.of(member.getSocialName(), member.getSocialEmail(), member.getRole());
CustomOauth2User customOauth2User = new CustomOauth2User(authInfo);

tokenProvider.generateRefreshToken(customOauth2User, now);
String accessToken = tokenProvider.generateAccessToken(customOauth2User, now);
tokenProvider.generateRefreshToken(member, customOauth2User, now);
String accessToken = tokenProvider.generateAccessToken(member, customOauth2User, now);
response.addCookie(cookieUtil.createCookie(accessToken));

return new TempSignResponse(true);
Expand Down Expand Up @@ -175,8 +175,8 @@ public ReissueResponse reissue(HttpServletRequest request, HttpServletResponse r

CustomOauth2User customUser = new CustomOauth2User(
AuthInfo.of(member.getSocialName(), member.getSocialEmail(), member.getRole()));
String reissuedAccessToken = tokenProvider.generateAccessToken(customUser, new Date());
tokenProvider.generateRefreshToken(customUser, new Date());
String reissuedAccessToken = tokenProvider.generateAccessToken(member, customUser, new Date());
tokenProvider.generateRefreshToken(member, customUser, new Date());

response.addCookie(cookieUtil.createCookie(reissuedAccessToken));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.web.bind.annotation.RestController;

import com.dnd.gongmuin.chat_inquiry.dto.AcceptChatResponse;
import com.dnd.gongmuin.chat_inquiry.dto.ChatInquiryDetailResponse;
import com.dnd.gongmuin.chat_inquiry.dto.ChatInquiryResponse;
import com.dnd.gongmuin.chat_inquiry.dto.CreateChatInquiryRequest;
import com.dnd.gongmuin.chat_inquiry.dto.CreateChatInquiryResponse;
Expand Down Expand Up @@ -42,7 +43,17 @@ public ResponseEntity<CreateChatInquiryResponse> createChatInquiry(
return ResponseEntity.ok(response);
}

@Operation(summary = "채팅방 요청 목록 조회 API", description = "회원의 채팅방 목록을 조회한다.")
@Operation(summary = "채팅 요청 상세 조회 API", description = "채팅방 요청을 조회한다.")
@GetMapping("/api/chat/inquiries/{chatInquiryId}")
public ResponseEntity<ChatInquiryDetailResponse> getChatInquiryById(
@PathVariable("chatInquiryId") Long chatInquiryId,
@AuthenticationPrincipal Member member
) {
ChatInquiryDetailResponse response = chatInquiryService.getChatInquiryById(chatInquiryId, member);
return ResponseEntity.ok(response);
}

@Operation(summary = "채팅 요청 목록 조회 API", description = "회원의 채팅 목록을 조회한다.")
@GetMapping("/api/chat/inquiries")
public ResponseEntity<PageResponse<ChatInquiryResponse>> getChatInquiresByMember(
@AuthenticationPrincipal Member member,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ChatInquiry extends TimeBaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "chat_proposal_id", nullable = false)
@Column(name = "chat_inquiry_id", nullable = false)
private Long id;

@ManyToOne(fetch = LAZY)
Expand Down Expand Up @@ -90,4 +90,12 @@ public void updateStatusRejected() {
status = InquiryStatus.REJECTED;
inquirer.increaseCredit(CHAT_REWARD);
}

public boolean isInquirer(Member member) {
return member.equals(this.inquirer);
}

public Member getChatPartner(Member member) {
return isInquirer(member) ? this.answerer : this.inquirer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.dnd.gongmuin.chat_inquiry.dto;

import com.dnd.gongmuin.member.dto.response.MemberInfo;

public record ChatInquiryDetailResponse(
Long chatInquiryId,
String inquiryMessage,
String inquiryStatus,
boolean isInquirer,
MemberInfo chatPartner
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.dnd.gongmuin.chat_inquiry.domain.InquiryStatus;
import com.dnd.gongmuin.chatroom.domain.ChatRoom;
import com.dnd.gongmuin.member.domain.Member;
import com.dnd.gongmuin.member.dto.response.MemberInfo;
import com.dnd.gongmuin.question_post.domain.QuestionPost;
import com.dnd.gongmuin.question_post.dto.response.MemberInfo;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -44,6 +44,24 @@ public static CreateChatInquiryResponse toCreateChatInquiryResponse(
);
}

public static ChatInquiryDetailResponse toChatInquiryDetailResponse(
ChatInquiry chatInquiry,
Member chatPartner,
boolean isInquirer
) {
return new ChatInquiryDetailResponse(chatInquiry.getId(),
chatInquiry.getMessage(),
chatInquiry.getStatus().getLabel(),
isInquirer,
new MemberInfo(
chatPartner.getId(),
chatPartner.getNickname(),
chatPartner.getJobGroup().getLabel(),
chatPartner.getProfileImageNo()
)
);
}

public static AcceptChatResponse toAcceptChatResponse(
ChatInquiry chatInquiry,
ChatRoom chatRoom
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
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.question_post.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(
Long chatInquiryId,
String inquiryMessage,
String inquiryStatus,
boolean isInquirer,
MemberInfo chatPartner
MemberInfo chatPartner,
String createdAt
) {
@QueryProjection
public ChatInquiryResponse(
Long chatInquiryId,
String inquiryMessage,
InquiryStatus inquiryStatus,
boolean isInquirer,
Long partnerId,
String partnerNickname,
JobGroup partnerJobGroup,
int partnerProfileImageNo
ChatInquiry chatInquiry,
boolean isInquirer
) {
this(
chatInquiryId,
inquiryMessage,
inquiryStatus.getLabel(),
chatInquiry.getId(),
chatInquiry.getMessage(),
chatInquiry.getStatus().getLabel(),
isInquirer,
new MemberInfo(
partnerId,
partnerNickname,
partnerJobGroup.getLabel(),
partnerProfileImageNo
)
createPartnerInfo(isInquirer, chatInquiry),
chatInquiry.getCreatedAt().toString()
);
}

private static MemberInfo createPartnerInfo(boolean isInquirer, ChatInquiry chatInquiry) {
Member partner = isInquirer ? chatInquiry.getAnswerer() : chatInquiry.getInquirer();
return new MemberInfo(
partner.getId(),
partner.getNickname(),
partner.getJobGroup().getLabel(),
partner.getProfileImageNo()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.dnd.gongmuin.chat_inquiry.dto;

import com.dnd.gongmuin.question_post.dto.response.MemberInfo;
import com.dnd.gongmuin.member.dto.response.MemberInfo;

public record CreateChatInquiryResponse(
Long chatInquiryId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,11 @@ public class ChatInquiryQueryRepositoryImpl implements ChatInquiryQueryRepositor
public Slice<ChatInquiryResponse> getChatInquiresByMember(Member member, Pageable pageable) {
List<ChatInquiryResponse> content = queryFactory
.select(new QChatInquiryResponse(
chatInquiry.id,
chatInquiry.message,
chatInquiry.status,
chatInquiry,
new CaseBuilder()
.when(chatInquiry.inquirer.id.eq(member.getId()))
.then(true)
.otherwise(false),
new CaseBuilder()
.when(chatInquiry.inquirer.id.eq(member.getId()))
.then(chatInquiry.answerer.id)
.otherwise(chatInquiry.inquirer.id),
new CaseBuilder()
.when(chatInquiry.inquirer.id.eq(member.getId()))
.then(chatInquiry.answerer.nickname)
.otherwise(chatInquiry.inquirer.nickname),
new CaseBuilder()
.when(chatInquiry.inquirer.id.eq(member.getId()))
.then(chatInquiry.answerer.jobGroup)
.otherwise(chatInquiry.inquirer.jobGroup),
new CaseBuilder()
.when(chatInquiry.inquirer.id.eq(member.getId()))
.then(chatInquiry.answerer.profileImageNo)
.otherwise(chatInquiry.inquirer.profileImageNo)
.otherwise(false)
))
.from(chatInquiry)
.where(chatInquiry.inquirer.id.eq(member.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.dnd.gongmuin.chat_inquiry.domain.ChatInquiry;
import com.dnd.gongmuin.chat_inquiry.dto.AcceptChatResponse;
import com.dnd.gongmuin.chat_inquiry.dto.ChatInquiryDetailResponse;
import com.dnd.gongmuin.chat_inquiry.dto.ChatInquiryMapper;
import com.dnd.gongmuin.chat_inquiry.dto.ChatInquiryResponse;
import com.dnd.gongmuin.chat_inquiry.dto.CreateChatInquiryRequest;
Expand Down Expand Up @@ -70,6 +71,17 @@ public CreateChatInquiryResponse createChatInquiry(CreateChatInquiryRequest requ
return ChatInquiryMapper.toCreateChatInquiryResponse(chatInquiry);
}

@Transactional(readOnly = true)
public ChatInquiryDetailResponse getChatInquiryById(Long chatInquiryId, Member member) {
ChatInquiry chatInquiry = getChatInquiryById(chatInquiryId);

return ChatInquiryMapper.toChatInquiryDetailResponse(
chatInquiry,
chatInquiry.getChatPartner(member),
chatInquiry.isInquirer(member)
);
}

@Transactional(readOnly = true)
public PageResponse<ChatInquiryResponse> getChatInquiresByMember(Member member, Pageable pageable) {
Slice<ChatInquiryResponse> responsePage = chatInquiryRepository.getChatInquiresByMember(
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/dnd/gongmuin/chatroom/domain/ChatRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ public static ChatRoom of(
) {
return new ChatRoom(questionPost, inquirer, answerer);
}

public boolean isInquirer(Member member) {
return member.equals(this.inquirer);
}

public Member getChatPartner(Member member) {
return isInquirer(member) ? this.answerer : this.inquirer;
}
}
Loading
Loading