Skip to content

Commit

Permalink
Merge pull request #158 from Gongjakso/refactor/qa
Browse files Browse the repository at this point in the history
refactor: pagination과 fetch join간의 문제점 해결하기 위한 쿼리 튜닝 진행
  • Loading branch information
dl-00-e8 authored May 27, 2024
2 parents eda7f94 + 67d7050 commit 6ef5fff
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public ApplicationResponse<ApplyPageRes> getApplyList(@AuthenticationPrincipal P
//내가 참여한 공고 정보 요청 api
@Operation(summary = "내가 참여한 공고 정보 API", description = "내가 참여한 공고 정보")
@GetMapping("/my-participation-post")
public ApplicationResponse<ParticipationPageRes> getMyParticipationPostList(@AuthenticationPrincipal PrincipalDetails principalDetails, @RequestParam(name = "page", defaultValue = "0") int page, @RequestParam(name = "size", defaultValue = "6") int size){
return ApplicationResponse.ok(applyService.myParticipationPostListPage(principalDetails.getMember(),page,size));
public ApplicationResponse<ParticipationPageRes> getMyParticipationPostList(@AuthenticationPrincipal PrincipalDetails principalDetails, @PageableDefault(size = 6) Pageable pageable){
return ApplicationResponse.ok(applyService.myParticipationPostListPage(principalDetails.getMember(), pageable));
}
//지원서 열람 요청 api
@Operation(summary = "지원서 열람 API", description = "내가 모집 중인 팀 페이지에서 지원서 열람 시")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface ApplyRepository extends JpaRepository<Apply,Long> {
countQuery = "SELECT COUNT(DISTINCT a) FROM Apply a WHERE a.member = :member AND a.post.status IN :postStatus AND a.deletedAt IS NULL AND a.isCanceled = false")
Page<Apply> findAllByMemberAndPostStatusInAndDeletedAtIsNullAndIsCanceledFalseOrderByCreatedAtDesc(@Param("member") Member member, @Param("postStatus") List<PostStatus> postStatusList, Pageable pageable);

Apply findApplyByMemberAndPost(Member member,Post post);
Optional<Apply> findApplyByMemberAndPostAndDeletedAtIsNullAndIsCanceledFalse(Member member,Post post);

Optional<Apply> findApplyByApplyIdAndDeletedAtIsNull(Long applyId);
Optional<Apply> findApplyByApplyIdAndDeletedAtIsNullAndIsCanceledFalse(Long applyId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,15 @@ public ApplyPageRes applyListPage(Member member, long postId, int page, int size
return ApplyPageRes.of(applyLists, pageNo, size, totalPages, last);
}

public ParticipationPageRes myParticipationPostListPage(Member member, int page, int size) {
Pageable pageable = PageRequest.of(page, size, Sort.by("createdAt").descending());
List<Apply> applyList = applyRepository.findApplyByApplyTypeAndMemberAndIsCanceledFalse(ApplyType.PASS,member);
List<Long> postIdList = applyList.stream()
.filter(apply -> apply.getPost().getStatus().equals(PostStatus.ACTIVE) || apply.getPost().getStatus().equals(PostStatus.COMPLETE))
.map(Apply::getApplyId)
.toList();
Page<Post> postPage = postRepository.findAllByPostIdInOrMemberAndDeletedAtIsNull(postIdList, member, pageable);
public ParticipationPageRes myParticipationPostListPage(Member member, Pageable page) {
Pageable pageable = PageRequest.of(page.getPageNumber(), page.getPageSize(), Sort.by("createdAt").descending());
System.out.println(pageable.getOffset());
List<PostStatus> postStatusList = Arrays.asList(ACTIVE, COMPLETE);
Page<Post> postPage = postRepository.findPostsByMemberIdAndPostStatusInOrderByCreatedAtDesc(member.getMemberId(), postStatusList, pageable);
List<ParticipationList> participationLists = postPage.getContent().stream()
.map(ParticipationList::of)
.collect(Collectors.toList());
int pageNo = postPage.getNumber();
int totalPages = postPage.getTotalPages();
boolean last = postPage.isLast();
return ParticipationPageRes.of(participationLists, pageNo, size, totalPages, last);
return ParticipationPageRes.of(participationLists, postPage.getNumber(), postPage.getSize(), postPage.getTotalPages(), postPage.isLast());
}

private String decisionState(Apply apply) {
Expand Down Expand Up @@ -281,39 +275,32 @@ public Page<MyPageRes> getMyApplyList(Member member, Pageable pageable) {
}

public ApplicationRes getMyApplication(Member member, Long postId){
Post post = postRepository.findByPostId(postId);
if (post == null) {
throw new ApplicationException(ErrorCode.NOT_FOUND_POST_EXCEPTION);
}else{
Apply apply = applyRepository.findApplyByMemberAndPost(member, post);
if(apply == null){
throw new ApplicationException(ErrorCode.NOT_FOUND_APPLY_EXCEPTION);
}else {
//Change List Type
List<String> categoryList = changeCategoryType(post);
List<String> stackNameList;
List<String> applyStackList = null;
if(post.isPostType()){
stackNameList = changeStackNameType(post);
System.out.println("change stack name");
List<ApplyStack> applyStacks = applyStackRepository.findAllByApply(apply);
applyStackList = new ArrayList<>();
for(ApplyStack applyStack : applyStacks){
applyStackList.add(applyStack.getStackName().getStackNameType());
}
}else {
stackNameList= null;
}
Post post = postRepository.findByPostId(postId).orElseThrow(() -> new ApplicationException(ErrorCode.NOT_FOUND_POST_EXCEPTION));

return ApplicationRes.of(apply, categoryList, stackNameList,applyStackList);
Apply apply = applyRepository.findApplyByMemberAndPostAndDeletedAtIsNullAndIsCanceledFalse(member, post).orElseThrow(() -> new ApplicationException(ErrorCode.NOT_FOUND_APPLY_EXCEPTION));

// Change List Type
List<String> categoryList = changeCategoryType(post);
List<String> stackNameList;
List<String> applyStackList = null;
if(post.isPostType()){
stackNameList = changeStackNameType(post);
List<ApplyStack> applyStacks = applyStackRepository.findAllByApply(apply);
applyStackList = new ArrayList<>();
for(ApplyStack applyStack : applyStacks){
applyStackList.add(applyStack.getStackName().getStackNameType());
}
}else {
stackNameList= null;
}

return ApplicationRes.of(apply, categoryList, stackNameList, applyStackList);
}

@Transactional
public PatchApplyRes cancelApply(Member member, Long applyId) {
// Validation: 논리적 삭제 데이터이거나 신청자 본인이 아닌 경우에 대한 유효성 검증 + 공고의 모집 기한 확인
Apply apply = applyRepository.findApplyByApplyIdAndDeletedAtIsNull(applyId).orElseThrow(() -> new ApplicationException(ErrorCode.ALREADY_DELETE_EXCEPTION));
Apply apply = applyRepository.findApplyByApplyIdAndDeletedAtIsNullAndIsCanceledFalse(applyId).orElseThrow(() -> new ApplicationException(ErrorCode.ALREADY_DELETE_EXCEPTION));
if (!apply.getMember().getMemberId().equals(member.getMemberId())) {
throw new ApplicationException(ErrorCode.UNAUTHORIZED_EXCEPTION);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

public interface PostRepository extends JpaRepository<Post, Long> {
Post findByPostId(Long postId);
Optional<Post> findByPostId(Long postId);

Optional<Post> findWithStackNameAndCategoryUsingFetchJoinByPostId(Long postId);

Expand Down Expand Up @@ -132,5 +133,26 @@ public interface PostRepository extends JpaRepository<Post, Long> {

List<Post> findAllByMemberAndStatusInAndDeletedAtIsNullOrderByCreatedAtDesc(Member member, List<PostStatus> statusList);

Page<Post> findAllByPostIdInOrMemberAndDeletedAtIsNull(List<Long> postIdList, Member member, Pageable pageable);
@Query(value = "SELECT DISTINCT p " +
"FROM Post p " +
"LEFT JOIN FETCH p.member m " +
"LEFT JOIN FETCH p.categories c " +
"LEFT JOIN Apply a ON p.postId = a.post.postId " +
"WHERE ((a.member.memberId = :memberId " +
"AND a.deletedAt IS NULL " +
"AND a.isCanceled = false " +
"AND a.applyType = 'PASS') OR (p.member.memberId = :memberId)) " +
"AND p.deletedAt IS NULL " +
"AND p.status IN :postStatus " +
"ORDER BY p.createdAt DESC",
countQuery = "SELECT COUNT(DISTINCT p) " +
"FROM Post p " +
"LEFT JOIN Apply a ON p.postId = a.post.postId " +
"WHERE ((a.member.memberId = :memberId " +
"AND a.deletedAt IS NULL " +
"AND a.isCanceled = false " +
"AND a.applyType = 'PASS') OR (p.member.memberId = :memberId)) " +
"AND p.deletedAt IS NULL " +
"AND p.status IN :postStatus")
Page<Post> findPostsByMemberIdAndPostStatusInOrderByCreatedAtDesc(@Param("memberId") Long memberId, @Param("postStatus") List<PostStatus> postStatusList, Pageable pageable);
}

0 comments on commit 6ef5fff

Please sign in to comment.