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] 게임 목록 조회 커서 페이징 쿼리 개선 #100 #101

Merged
merged 1 commit into from
Feb 2, 2024
Merged
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 @@ -9,53 +9,56 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;

import static com.sports.server.command.game.domain.QGame.game;

@Component
@RequiredArgsConstructor
public class GamesQueryConditionMapper {

private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static final OrderSpecifier<?>[] FINISHED_ORDER = {game.startTime.desc(), game.id.desc()};
private static final OrderSpecifier<?>[] NOT_FINISHED_ORDER = {game.startTime.asc(), game.id.asc()};

private final JPAQueryFactory jpaQueryFactory;

public OrderSpecifier<?> mapOrderCondition(GamesQueryRequestDto request) {
public OrderSpecifier<?>[] mapOrderCondition(GamesQueryRequestDto request) {
GameState state = GameState.from(request.getStateValue());
if (state == GameState.FINISHED) {
return game.startTime.stringValue().concat(game.id.stringValue()).desc();
return FINISHED_ORDER;
}
return game.startTime.stringValue().concat(game.id.stringValue()).asc();
return NOT_FINISHED_ORDER;
}

public BooleanBuilder mapBooleanCondition(GamesQueryRequestDto gamesQueryRequestDto,
PageRequestDto pageRequestDto) {
GameState state = GameState.from(gamesQueryRequestDto.getStateValue());
String cursor = getCursorValue(pageRequestDto.cursor());
Long cursor = pageRequestDto.cursor();
LocalDateTime cursorStartTime = getCursorStartTime(cursor);
DynamicBooleanBuilder booleanBuilder = DynamicBooleanBuilder.builder()
.and(() -> game.league.id.eq(gamesQueryRequestDto.getLeagueId()))
.and(() -> game.state.eq(state))
.and(() -> game.sport.id.in(gamesQueryRequestDto.getSportIds()));
if (state == GameState.FINISHED) {
return booleanBuilder
.and(() -> game.startTime.stringValue().concat(game.id.stringValue()).lt(cursor))
.and(() -> game.startTime.eq(cursorStartTime).and(game.id.lt(cursor))
.or(game.startTime.lt(cursorStartTime)))
.build();
}
return booleanBuilder
.and(() -> game.startTime.stringValue().concat(game.id.stringValue()).gt(cursor))
.and(() -> game.startTime.eq(cursorStartTime).and(game.id.gt(cursor))
.or(game.startTime.gt(cursorStartTime)))
.build();
}

private String getCursorValue(Long cursor) {
private LocalDateTime getCursorStartTime(Long cursor) {
if (cursor == null) {
return null;
}
return jpaQueryFactory
.select(game.startTime)
.from(game)
.where(game.id.eq(cursor))
.fetchFirst()
.format(FORMATTER) + cursor;
.fetchOne();
}
}
Loading