Skip to content

Commit

Permalink
🐛 Fix get algorithm order by week
Browse files Browse the repository at this point in the history
  • Loading branch information
baebae02 committed Sep 22, 2024
1 parent f05a6bd commit 20c864b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
@Repository
public interface AlgorithmRepository extends JpaRepository<Algorithm, AlgorithmPrimaryKey> {
// 특정 season 중에서 hidden이 true인 것 중 첫 번째 항목 가져오기
Optional<Algorithm> findFirstBySeasonAndHiddenTrueOrderByCreatedAtAsc(int season);
Optional<Algorithm> findFirstBySeasonAndHiddenTrueOrderByWeekAsc(int season);

// 특정 season 중에서 hidden이 false인 것 중 가장 마지막 항목 가져오기
Optional<Algorithm> findFirstBySeasonAndHiddenFalseOrderByCreatedAtDesc(int season);
Optional<Algorithm> findFirstBySeasonAndHiddenFalseOrderByWeekDesc(int season);

List<Algorithm> findAllByHiddenIsFalseOrderByCreatedAtDesc();
List<Algorithm> findAllByHiddenIsFalseOrderByWeekDesc();

List<Algorithm> findAllBySeasonAndHiddenFalseOrderByCreatedAtDesc(int season);
List<Algorithm> findAllBySeasonAndHiddenFalseOrderByWeekDesc(int season);

@Query("SELECT a FROM Algorithm a WHERE a.name = :algorithmName AND a.season = :season")
Optional<Algorithm> findAlgorithmByNameAndSeason(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class AlgorithmService {
private int currentSeason;

public List<AlgorithmResponseDto> getAlgorithms() {
List<Algorithm> algorithms = algorithmRepository.findAllByHiddenIsFalseOrderByCreatedAtDesc();
List<Algorithm> algorithms = algorithmRepository.findAllByHiddenIsFalseOrderByWeekDesc();
// 시즌별로 그룹화
Map<Integer, List<AlgorithmDto>> groupedBySeason = algorithms.stream()
.collect(Collectors.groupingBy(
Expand All @@ -48,28 +48,28 @@ public List<AlgorithmResponseDto> getAlgorithms() {
}

public AlgorithmResponseDto getAlgorithmsBySeason(int season) {
List<Algorithm> algorithms = algorithmRepository.findAllBySeasonAndHiddenFalseOrderByCreatedAtDesc(season);
List<Algorithm> algorithms = algorithmRepository.findAllBySeasonAndHiddenFalseOrderByWeekDesc(season);
return AlgorithmResponseDto.builder()
.season(season)
.algorithms(AlgorithmDto.listOf(algorithms))
.build();
}

public Optional<Algorithm> getWeeklyAlgorithmBySeason(int season) {
return algorithmRepository.findFirstBySeasonAndHiddenTrueOrderByCreatedAtAsc(season);
return algorithmRepository.findFirstBySeasonAndHiddenTrueOrderByWeekAsc(season);
}

public void saveAlgorithm(Algorithm algorithm) {
algorithmRepository.save(algorithm);
}

public Algorithm findWeeklyAlgorithm() {
return algorithmRepository.findFirstBySeasonAndHiddenTrueOrderByCreatedAtAsc(currentSeason)
return algorithmRepository.findFirstBySeasonAndHiddenTrueOrderByWeekAsc(currentSeason)
.orElseThrow(() -> new NoSuchElementException("해당 시즌의 공개되지 않은 알고리즘이 존재하지 않습니다."));
}

public Algorithm findDailyAlgorithm() {
return algorithmRepository.findFirstBySeasonAndHiddenFalseOrderByCreatedAtDesc(currentSeason)
return algorithmRepository.findFirstBySeasonAndHiddenFalseOrderByWeekDesc(currentSeason)
.orElseThrow(() -> new NoSuchElementException("공개된 알고리즘이 존재하지 않습니다."));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void init() {
@Test
@DisplayName("특정 season 중에서 hidden이 true인 것 중 첫 번째 항목 가져오기")
void findFirstBySeasonAndHiddenTrueOrderByIdAsc() {
Optional<Algorithm> result = algorithmRepository.findFirstBySeasonAndHiddenTrueOrderByCreatedAtAsc(2);
Optional<Algorithm> result = algorithmRepository.findFirstBySeasonAndHiddenTrueOrderByWeekAsc(2);

assertNotNull(result);
assertEquals("그리디 알고리즘", result.get().getName());
Expand All @@ -84,7 +84,7 @@ void findFirstBySeasonAndHiddenTrueOrderByIdAsc() {
@Test
@DisplayName("특정 season 중에서 hidden이 false인 것 중 가장 마지막 항목 가져오기")
void findFirstBySeasonAndHiddenFalseOrderByIdDesc() {
Optional<Algorithm> result = algorithmRepository.findFirstBySeasonAndHiddenFalseOrderByCreatedAtDesc(1);
Optional<Algorithm> result = algorithmRepository.findFirstBySeasonAndHiddenFalseOrderByWeekDesc(1);

assertNotNull(result);
assertEquals("수학", result.get().getName());
Expand All @@ -93,7 +93,7 @@ void findFirstBySeasonAndHiddenFalseOrderByIdDesc() {
@Test
@DisplayName("최신 순으로 특정 시즌의 공개된 알고리즘 목록 불러오기")
void findAllBySeasonOrderByCreatedAtDesc() {
List<Algorithm> result = algorithmRepository.findAllBySeasonAndHiddenFalseOrderByCreatedAtDesc(1);
List<Algorithm> result = algorithmRepository.findAllBySeasonAndHiddenFalseOrderByWeekDesc(1);

assertNotNull(result);
assertEquals(3, result.get(0).getWeek());
Expand All @@ -102,7 +102,7 @@ void findAllBySeasonOrderByCreatedAtDesc() {
@Test
@DisplayName("최신 순으로 모든 공개된 알고리즘 목록 불러오기")
void findAllByOrderByCreatedAtDesc() {
List<Algorithm> result = algorithmRepository.findAllByHiddenIsFalseOrderByCreatedAtDesc();
List<Algorithm> result = algorithmRepository.findAllByHiddenIsFalseOrderByWeekDesc();

assertNotNull(result);
assertEquals(3, result.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void setUp() {
@DisplayName("모든 알고리즘 조회 성공")
void getAlgorithms() {
// given
when(algorithmRepository.findAllByHiddenIsFalseOrderByCreatedAtDesc())
when(algorithmRepository.findAllByHiddenIsFalseOrderByWeekDesc())
.thenReturn(algorithms);

// when
Expand All @@ -75,7 +75,7 @@ void getAlgorithms() {
void getAlgorithmsBySeason() {
// given
int season = 2;
when(algorithmRepository.findAllBySeasonAndHiddenFalseOrderByCreatedAtDesc(season))
when(algorithmRepository.findAllBySeasonAndHiddenFalseOrderByWeekDesc(season))
.thenReturn(algorithms.subList(0, 3));

// when
Expand Down

0 comments on commit 20c864b

Please sign in to comment.