Skip to content

Commit

Permalink
Merge pull request #101 from hufscheer/refactor/#100-query-games
Browse files Browse the repository at this point in the history
[REFACTOR] 게임 목록 조회 커서 페이징 쿼리 개선 #100
  • Loading branch information
ldk980130 authored Feb 2, 2024
2 parents c50cecc + 9567e34 commit 3f87b07
Showing 1 changed file with 14 additions and 11 deletions.
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();
}
}

0 comments on commit 3f87b07

Please sign in to comment.