-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: specification 정의 * feat: FestivalFilter 정의 * feat: 축제 진행 상태에 따른 Controller, Service 생성 * refactor: private 생성자를 lombok 을 통해 생성 * chore: 괄호 제거 * chore: 변수 상수화 * chore: given 절 타입 명시 * feat: 축제 조회 ALL 삭제 및 기본값을 진행 중으로 변경 * feat: 축제 당일이 Progress에 포함되도록 변경 및 Spec 리팩터링 * feat: 축제 진행 상황별 정렬 조건 추가 * chore: 메서드 순서 변경 * chore: index 추가 * chore: 에러 메시지 변경 * chore: test 개행 변경 및 변수 재활용
- Loading branch information
1 parent
6288aff
commit 5e1f8df
Showing
11 changed files
with
322 additions
and
27 deletions.
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
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
33 changes: 33 additions & 0 deletions
33
backend/src/main/java/com/festago/festival/repository/FestivalFilter.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,33 @@ | ||
package com.festago.festival.repository; | ||
|
||
import com.festago.common.exception.BadRequestException; | ||
import com.festago.common.exception.ErrorCode; | ||
import com.festago.festival.domain.Festival; | ||
import java.time.LocalDate; | ||
import java.util.function.Function; | ||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
public enum FestivalFilter { | ||
PROGRESS(FestivalSpecification::progress), | ||
PLANNED(FestivalSpecification::planned), | ||
END(FestivalSpecification::end); | ||
|
||
private final Function<LocalDate, Specification<Festival>> filter; | ||
|
||
FestivalFilter(Function<LocalDate, Specification<Festival>> filter) { | ||
this.filter = filter; | ||
} | ||
|
||
public static FestivalFilter from(String filterName) { | ||
return switch (filterName.toUpperCase()) { | ||
case "PROGRESS" -> PROGRESS; | ||
case "PLANNED" -> PLANNED; | ||
case "END" -> END; | ||
default -> throw new BadRequestException(ErrorCode.INVALID_FESTIVAL_FILTER); | ||
}; | ||
} | ||
|
||
public Specification<Festival> getSpecification(LocalDate currentTime) { | ||
return filter.apply(currentTime); | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
backend/src/main/java/com/festago/festival/repository/FestivalSpecification.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,38 @@ | ||
package com.festago.festival.repository; | ||
|
||
import com.festago.festival.domain.Festival; | ||
import java.time.LocalDate; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class FestivalSpecification { | ||
|
||
private static final String START_DATE = "startDate"; | ||
private static final String END_DATE = "endDate"; | ||
|
||
public static Specification<Festival> progress(LocalDate currentTime) { | ||
return (root, query, criteriaBuilder) -> { | ||
query.orderBy(criteriaBuilder.asc(root.get(START_DATE))); | ||
return criteriaBuilder.and( | ||
criteriaBuilder.lessThanOrEqualTo(root.get(START_DATE), currentTime), | ||
criteriaBuilder.greaterThanOrEqualTo(root.get(END_DATE), currentTime) | ||
); | ||
}; | ||
} | ||
|
||
public static Specification<Festival> planned(LocalDate currentTime) { | ||
return (root, query, criteriaBuilder) -> { | ||
query.orderBy(criteriaBuilder.asc(root.get(START_DATE))); | ||
return criteriaBuilder.greaterThan(root.get(START_DATE), currentTime); | ||
}; | ||
} | ||
|
||
public static Specification<Festival> end(LocalDate currentTime) { | ||
return (root, query, criteriaBuilder) -> { | ||
query.orderBy(criteriaBuilder.desc(root.get(END_DATE))); | ||
return criteriaBuilder.lessThan(root.get(END_DATE), currentTime); | ||
}; | ||
} | ||
} |
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
4 changes: 4 additions & 0 deletions
4
backend/src/main/resources/db/migration/V9__festival_date_index.sql
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,4 @@ | ||
create index festival_start_date_index | ||
on festival (start_date); | ||
create index festival_end_date_index | ||
on festival (end_date desc); |
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
54 changes: 54 additions & 0 deletions
54
backend/src/test/java/com/festago/festival/domain/FestivalFilterTest.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,54 @@ | ||
package com.festago.festival.domain; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import com.festago.common.exception.BadRequestException; | ||
import com.festago.festival.repository.FestivalFilter; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
@DisplayNameGeneration(ReplaceUnderscores.class) | ||
@SuppressWarnings("NonAsciiCharacters") | ||
class FestivalFilterTest { | ||
|
||
@Test | ||
void 유효하지_않은_name_이면_예외() { | ||
// given && when && then | ||
assertThatThrownBy(() -> FestivalFilter.from("unvalid")) | ||
.isInstanceOf(BadRequestException.class); | ||
} | ||
|
||
@ValueSource(strings = {"progress", "Progress", "PROGRESS"}) | ||
@ParameterizedTest | ||
void PROGRESS_반환(String value) { | ||
// given && when | ||
FestivalFilter filter = FestivalFilter.from(value); | ||
|
||
// then | ||
assertThat(filter).isEqualTo(FestivalFilter.PROGRESS); | ||
} | ||
|
||
@ValueSource(strings = {"planned", "Planned", "PLANNED"}) | ||
@ParameterizedTest | ||
void PLANNED_반환(String value) { | ||
// given && when | ||
FestivalFilter filter = FestivalFilter.from(value); | ||
|
||
// then | ||
assertThat(filter).isEqualTo(FestivalFilter.PLANNED); | ||
} | ||
|
||
@ValueSource(strings = {"end", "End", "END"}) | ||
@ParameterizedTest | ||
void END_반환(String value) { | ||
// given && when | ||
FestivalFilter filter = FestivalFilter.from(value); | ||
|
||
// then | ||
assertThat(filter).isEqualTo(FestivalFilter.END); | ||
} | ||
} |
Oops, something went wrong.