-
Notifications
You must be signed in to change notification settings - Fork 6
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
feat: offset 기반 리크루트 목록 조회 추가 #282
Merged
khs960616
merged 10 commits into
SSAF-SOUND:dev
from
khs960616:feature/recruit-paging-#280
Oct 23, 2023
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c2fe088
fix: recruit 목록 조회 페이징 필드 네이밍 수정 cursor -> next
khs960616 9c8f489
fix: 커서 기반 리크루트 목록 조회 네이밍 수정 및 페이징 기반 리크루트 목록 조회 인터페이스에 추가
khs960616 49139a6
fix: 커서기반 리크루트 조회 메서드 네이밍 수정 반영
khs960616 fba8aae
refactor: boolean expression 분리 및 페이징 기반 조회 추가
khs960616 ea74d66
fix: 페이징과 같이 쓸수 있도록 리크루트 목록 조회시 next의 타입 Integer로 변경
khs960616 e21b597
fix: 타입 수정에 따른 테스트 코드 수정
khs960616 e7b92f5
feature: offset기반 페이지 네이션 추가
khs960616 c0819c8
fix: 페이징 넘버 +1 처리
khs960616 0a256d6
fix: 페이징 OFFSET 관련 +1 처리 추가
khs960616 c44fed1
docs: 커서기반 페이지, 오프셋 기반 페이징 DOCS 반영
khs960616 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/com/ssafy/ssafsound/domain/recruit/dto/GetRecruitOffsetResDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.ssafy.ssafsound.domain.recruit.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.ssafy.ssafsound.domain.recruit.domain.Recruit; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.springframework.data.domain.Page; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import java.util.stream.Collectors; | ||
|
||
@Getter | ||
@Builder | ||
public class GetRecruitOffsetResDto implements AddParticipantDto { | ||
private List<RecruitElement> recruits; | ||
private int currentPage; | ||
private int totalPageCount; | ||
@JsonIgnore | ||
public List<Long> getRecruitsId() { | ||
return recruits.stream().map(RecruitElement::getRecruitId).collect(Collectors.toList()); | ||
} | ||
|
||
@JsonIgnore | ||
public Map<Long, Map<String, RecruitParticipant>> getRecruitParticipantMapByRecruitIdAndRecruitType() { | ||
Map<Long, Map<String, RecruitParticipant>> result = new TreeMap<>(); | ||
for(RecruitElement recruitElement: recruits) { | ||
result.put(recruitElement.getRecruitId(), recruitElement.getRecruitParticipantMap()); | ||
} | ||
return result; | ||
} | ||
|
||
public static GetRecruitOffsetResDto fromPageAndMemberId(Page<Recruit> recruitPages, Long memberId) { | ||
List<RecruitElement> recruits = recruitPages.getContent() | ||
.stream() | ||
.map((recruit -> RecruitElement.fromRecruitAndLoginMemberId(recruit, memberId))) | ||
.collect(Collectors.toList()); | ||
|
||
return GetRecruitOffsetResDto.builder() | ||
.recruits(recruits) | ||
.currentPage(recruitPages.getNumber()) | ||
.totalPageCount(recruitPages.getTotalPages()) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,9 +17,8 @@ | |
import lombok.RequiredArgsConstructor; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.data.domain.SliceImpl; | ||
import org.springframework.data.domain.*; | ||
import org.springframework.data.support.PageableExecutionUtils; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.util.StringUtils; | ||
|
||
|
@@ -44,63 +43,100 @@ public class RecruitDynamicQueryRepositoryImpl implements RecruitDynamicQueryRep | |
private final JPAQueryFactory jpaQueryFactory; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JPAQueryFactory 가져다 쓰면 따로 RecruitDynamicQueryRepository를 통해 Override 할 필요가 없지 않을까 해서 의견 남겨봅니다 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 배포후 제거해도 문제가없으면 삭제할게요. |
||
|
||
@Override | ||
public Slice<Recruit> findRecruitByGetRecruitsReqDto(GetRecruitsReqDto dto, Pageable pageable) { | ||
// cursor base pagination (value -1 or null ignore search condition) | ||
Long cursor = dto.getCursor(); | ||
|
||
// recruit category (STUDY | PROJECT) | ||
BooleanExpression categoryEq = dto.getCategory() == null ? null : recruit.category.eq(Category.valueOf(dto.getCategory().toUpperCase())); | ||
|
||
// recruit title contains search keyword | ||
String keyword = dto.getKeyword(); | ||
BooleanExpression titleEq = StringUtils.hasText(keyword) ? recruit.title.contains(keyword) : null; | ||
|
||
JPAQuery<Recruit> recruitDynamicQuery = jpaQueryFactory.selectFrom(recruit) | ||
.where(recruitIdLtThanCursor(cursor), categoryEq, titleEq); | ||
|
||
// recruit skill | ||
List<String> skills = dto.getSkills(); | ||
if(skills!=null && skills.size() > 0) { | ||
String metaDataType = MetaDataType.SKILL.name(); | ||
List<MetaData> containSkills = skills.stream() | ||
.map(skillName->metaDataConsumer.getMetaData(metaDataType, skillName)) | ||
.collect(Collectors.toList()); | ||
|
||
JPQLQuery<Long> recruitSkillContainRecruitIds = JPAExpressions | ||
.select(recruitSkill.recruit.id) | ||
.from(recruitSkill) | ||
.innerJoin(recruitSkill.recruit, recruit) | ||
.where(recruitSkill.skill.in(containSkills)); | ||
|
||
recruitDynamicQuery.where(recruit.id.in(recruitSkillContainRecruitIds)); | ||
} | ||
|
||
// recruit types limitation | ||
List<String> recruitTypes = dto.getRecruitTypes(); | ||
if(dto.getCategory() != null && dto.getCategory().toUpperCase().equals(Category.PROJECT.name()) && recruitTypes!=null && !recruitTypes.isEmpty()) { | ||
String metaDataType = MetaDataType.RECRUIT_TYPE.name(); | ||
List<MetaData> containRecruitTypes = recruitTypes.stream() | ||
.map(recruitType->metaDataConsumer.getMetaData(metaDataType, recruitType)) | ||
.collect(Collectors.toList()); | ||
|
||
JPQLQuery<Long> limitationContainRecruitIds = JPAExpressions | ||
.select(recruitLimitation.recruit.id) | ||
.from(recruitLimitation) | ||
.innerJoin(recruitLimitation.recruit, recruit) | ||
.where(recruitLimitation.type.in(containRecruitTypes)); | ||
|
||
recruitDynamicQuery.where(recruit.id.in(limitationContainRecruitIds)); | ||
} | ||
|
||
public Slice<Recruit> findRecruitSliceByGetRecruitsReqDto(GetRecruitsReqDto dto, Pageable pageable) { | ||
JPAQuery<Recruit> recruitDynamicQuery = findRecruitByGetRecruitsReqDto(dto); | ||
Integer cursor = dto.getNext(); | ||
List<Recruit> recruits = recruitDynamicQuery | ||
.where(recruit.finishedRecruit.eq(dto.isFinished())) | ||
.where(recruitIdLtThanCursor(Long.valueOf(cursor))) | ||
.limit(pageable.getPageSize()+1) | ||
.orderBy(recruit.id.desc()) | ||
.fetch(); | ||
boolean hasNext = pageable.isPaged() && recruits.size() > pageable.getPageSize(); | ||
return new SliceImpl<>(hasNext ? recruits.subList(0, pageable.getPageSize()) : recruits, pageable, hasNext); | ||
} | ||
|
||
@Override | ||
public Page<Recruit> findRecruitPageByGetRecruitsReqDto(GetRecruitsReqDto dto, Pageable pageable) { | ||
JPAQuery<Recruit> recruitDynamicQuery = findRecruitByGetRecruitsReqDto(dto); | ||
List<Recruit> recruits = recruitDynamicQuery | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.orderBy(recruit.id.desc()) | ||
.fetch(); | ||
|
||
JPAQuery<Long> countQuery = jpaQueryFactory | ||
.select(recruit.count()) | ||
.from(recruit) | ||
.where( | ||
recruitCategoryEq(dto.getCategory()), | ||
recruitTitleEq(dto.getKeyword()), | ||
recruit.finishedRecruit.eq(dto.isFinished()), | ||
recruitTypeContains(dto.getCategory(), dto.getRecruitTypes()), | ||
recruitSkillContains(dto.getSkills()) | ||
); | ||
|
||
return PageableExecutionUtils.getPage(recruits, pageable, countQuery::fetchOne); | ||
} | ||
|
||
private JPAQuery<Recruit> findRecruitByGetRecruitsReqDto(GetRecruitsReqDto dto) { | ||
JPAQuery<Recruit> recruitDynamicQuery = jpaQueryFactory.selectFrom(recruit); | ||
recruitDynamicQuery | ||
.where( | ||
recruitCategoryEq(dto.getCategory()), | ||
recruitTitleEq(dto.getKeyword()), | ||
recruit.finishedRecruit.eq(dto.isFinished()), | ||
recruitTypeContains(dto.getCategory(), dto.getRecruitTypes()), | ||
recruitSkillContains(dto.getSkills()) | ||
); | ||
return recruitDynamicQuery; | ||
} | ||
|
||
private BooleanExpression recruitCategoryEq(String category) { | ||
return category == null ? null : recruit.category.eq(Category.valueOf(category.toUpperCase())); | ||
} | ||
private BooleanExpression recruitTitleEq(String keyword) { | ||
return StringUtils.hasText(keyword) ? recruit.title.contains(keyword) : null; | ||
} | ||
private BooleanExpression recruitTypeContains(String category, List<String> recruitTypes) { | ||
boolean isNotProject = category == null || category.toUpperCase().equals(Category.PROJECT.name()); | ||
boolean isEmpty = recruitTypes == null || recruitTypes.isEmpty(); | ||
if(isNotProject || isEmpty) { | ||
return null; | ||
} | ||
|
||
String metaDataType = MetaDataType.RECRUIT_TYPE.name(); | ||
List<MetaData> containRecruitTypes = recruitTypes.stream() | ||
.map(recruitType->metaDataConsumer.getMetaData(metaDataType, recruitType)) | ||
.collect(Collectors.toList()); | ||
|
||
JPQLQuery<Long> limitationContainRecruitIds = JPAExpressions | ||
.select(recruitLimitation.recruit.id) | ||
.from(recruitLimitation) | ||
.innerJoin(recruitLimitation.recruit, recruit) | ||
.where(recruitLimitation.type.in(containRecruitTypes)); | ||
|
||
return recruit.id.in(limitationContainRecruitIds); | ||
} | ||
|
||
private BooleanExpression recruitSkillContains(List<String> skills) { | ||
boolean isEmpty = skills==null || skills.isEmpty(); | ||
if(isEmpty) { | ||
return null; | ||
} | ||
|
||
String metaDataType = MetaDataType.SKILL.name(); | ||
List<MetaData> containSkills = skills.stream() | ||
.map(skillName->metaDataConsumer.getMetaData(metaDataType, skillName)) | ||
.collect(Collectors.toList()); | ||
|
||
JPQLQuery<Long> recruitSkillContainRecruitIds = JPAExpressions | ||
.select(recruitSkill.recruit.id) | ||
.from(recruitSkill) | ||
.innerJoin(recruitSkill.recruit, recruit) | ||
.where(recruitSkill.skill.in(containSkills)); | ||
return recruit.id.in(recruitSkillContainRecruitIds); | ||
} | ||
|
||
@Override | ||
public Slice<Recruit> findMemberJoinRecruitWithCursorAndPageable(Long memberId, String category, Long cursor, Pageable pageable) { | ||
BooleanExpression applicationRecruitCategoryEq = | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pageble 또는 PageRequest의 page는 0부터 시작이니까 1을 빼주는 로직이 들어가야 할 것 같은데 cursor기반과 offset 기반의 페이지네이션을 하나의 dto로 관리하게 되면 문제가 발생하지 않을까 합니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Long.valueOf로 해당 부분을 감싸서 처리하고 있으므로 별도의 에러는 발생하지 않습니다.
-> Integer 범위를 넘어설 정도로 게시글이 등록되는 경우 문제가 되겠지만, 현실적으로 불가능한 수치이므로 문제가 없다고 생각해요.
Cursor
-> PageRequest로 만들때, 해당 부분을 별도 처리하도록 수정하겠습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
프론트 요청 사항으로 해당 부분 cursor, page로 스타일 일관되게 맞춘후, 해당 부분 인터페이스로 따로 빼서 쿼리 공유해서 쓸수 있게 해볼게요.