diff --git a/src/main/java/org/runimo/runimo/records/controller/request/MyRecordPageRequest.java b/src/main/java/org/runimo/runimo/records/controller/request/MyRecordPageRequest.java index f645ce5..d1420ff 100644 --- a/src/main/java/org/runimo/runimo/records/controller/request/MyRecordPageRequest.java +++ b/src/main/java/org/runimo/runimo/records/controller/request/MyRecordPageRequest.java @@ -14,20 +14,33 @@ @Setter public class MyRecordPageRequest { - @Parameter(description = "페이지 번호", example = "0") - @Min(0) - private Integer page = 0; - @Parameter(description = "페이지 당 크기", example = "10") - @Min(1) - @Max(20) - private Integer size = 10; - @Parameter(description = "시작 날짜(기본값 : 요청 시점)", example = "2024-04-04") - private LocalDate startDate = LocalDate.now(); - @Parameter(description = "종료 날짜(기본값 : 요청 시점)", example = "2024-04-04") - private LocalDate endDate = LocalDate.now(); + @Parameter(description = "페이지 번호", example = "0") + @Min(0) + private Integer page = 0; + @Parameter(description = "페이지 당 크기", example = "10") + @Min(1) + @Max(20) + private Integer size = 10; + @Parameter(description = "시작 날짜(기본값 : 요청 시점)", example = "2024-04-04") + private LocalDate startDate = LocalDate.now(); + @Parameter(description = "종료 날짜(기본값 : 요청 시점)", example = "2024-04-04") + private LocalDate endDate = LocalDate.now(); + @Parameter(description = "정렬 기준", example = "createdAt") + @Schema(allowableValues = {"startedAt", "createdAt"}) + private String sortBy = "startedAt"; + @Parameter(description = "정렬 방향", example = "asc") + @Schema(allowableValues = {"asc", "desc"}) + private String sortDirection = "desc"; - public static RecordQuery toQuery(MyRecordPageRequest request, Long userId) { - return new RecordQuery(userId, request.getPage(), request.getSize(), request.getStartDate(), - request.getEndDate()); - } + public static RecordQuery toQuery(MyRecordPageRequest request, Long userId) { + return RecordQuery.builder() + .userId(userId) + .page(request.getPage()) + .startDate(request.getStartDate()) + .endDate(request.getEndDate()) + .size(request.getSize()) + .sortBy(request.getSortBy()) + .sortDirection(request.getSortDirection()) + .build(); + } } diff --git a/src/main/java/org/runimo/runimo/records/repository/RecordRepository.java b/src/main/java/org/runimo/runimo/records/repository/RecordRepository.java index 89716da..545f3c8 100644 --- a/src/main/java/org/runimo/runimo/records/repository/RecordRepository.java +++ b/src/main/java/org/runimo/runimo/records/repository/RecordRepository.java @@ -16,43 +16,42 @@ @Repository public interface RecordRepository extends JpaRepository { - Optional findByRecordPublicId(String id); - - @Query("SELECT r FROM RunningRecord r " + - "WHERE r.userId = :userId " + - "AND r.startedAt BETWEEN :startOfWeek AND :now") - Slice findFirstRunOfWeek( - @Param("userId") Long userId, - @Param("startOfWeek") LocalDateTime startOfWeek, - @Param("now") LocalDateTime now, - Pageable pageable - ); - - @Query("SELECT COUNT(r.id) FROM RunningRecord r WHERE r.userId = :id") - Long countByUserId(Long id); - - @Query("select r from RunningRecord r where r.userId = :userId") - Slice findLatestByUserId(Long userId, Pageable pageRequest); - - @Query("select new org.runimo.runimo.records.service.dto.RecordStatDto(" + - "r.startedAt, " + - "r.endAt, " + - "r.totalDistance.amount) " + - "from RunningRecord r " + - "where r.userId = :userId " + - "and r.startedAt between :startOfWeek and :now " + - "order by r.startedAt asc") - List findRecordStatByUserIdAndBetween(Long userId, LocalDateTime startOfWeek, - LocalDateTime now); - - Page findRecordByUserIdOrderByStartedAtDesc(Long id, Pageable pageable); - - - @Query("select r " + - "from RunningRecord r " + - "where r.userId = :userId " + - "and r.startedAt between :from and :to " + - "order by r.startedAt asc") - Page findRecordByUserIdAndBetween(Long userId, LocalDateTime from, - LocalDateTime to, Pageable pageRequest); + Optional findByRecordPublicId(String id); + + @Query("SELECT r FROM RunningRecord r " + + "WHERE r.userId = :userId " + + "AND r.startedAt BETWEEN :startOfWeek AND :now") + Slice findFirstRunOfWeek( + @Param("userId") Long userId, + @Param("startOfWeek") LocalDateTime startOfWeek, + @Param("now") LocalDateTime now, + Pageable pageable + ); + + @Query("SELECT COUNT(r.id) FROM RunningRecord r WHERE r.userId = :id") + Long countByUserId(Long id); + + @Query("select r from RunningRecord r where r.userId = :userId") + Slice findLatestByUserId(Long userId, Pageable pageRequest); + + @Query("select new org.runimo.runimo.records.service.dto.RecordStatDto(" + + "r.startedAt, " + + "r.endAt, " + + "r.totalDistance.amount) " + + "from RunningRecord r " + + "where r.userId = :userId " + + "and r.startedAt between :startOfWeek and :now " + + "order by r.startedAt asc") + List findRecordStatByUserIdAndBetween(Long userId, LocalDateTime startOfWeek, + LocalDateTime now); + + Page findRecordByUserIdOrderByStartedAtDesc(Long id, Pageable pageable); + + + @Query("select r " + + "from RunningRecord r " + + "where r.userId = :userId " + + "and r.startedAt between :from and :to") + Page findRecordByUserIdAndBetween(Long userId, LocalDateTime from, + LocalDateTime to, Pageable pageRequest); } diff --git a/src/main/java/org/runimo/runimo/records/service/dto/RecordQuery.java b/src/main/java/org/runimo/runimo/records/service/dto/RecordQuery.java index 28301d4..6da15b6 100644 --- a/src/main/java/org/runimo/runimo/records/service/dto/RecordQuery.java +++ b/src/main/java/org/runimo/runimo/records/service/dto/RecordQuery.java @@ -2,21 +2,27 @@ import java.time.LocalDate; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +@Builder @Getter @AllArgsConstructor public class RecordQuery { - private final Long userId; - private final Integer page; - private final Integer size; - private final LocalDate startDate; - private final LocalDate endDate; + private final Long userId; + private final Integer page; + private final Integer size; + private final LocalDate startDate; + private final LocalDate endDate; + private final String sortBy; + private final String sortDirection; - public Pageable toPageable() { - return PageRequest.of(page, size); - } + public Pageable toPageable() { + Sort sort = Sort.by(Sort.Direction.fromString(sortDirection), sortBy); + return PageRequest.of(page, size, sort); + } }