Skip to content
Merged
Show file tree
Hide file tree
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 @@ -57,7 +57,7 @@ class MarathonEventQueryRepositoryTest {
@DisplayName("OPEN 상태 + 지역 일치 + regEndDate 미래 - 반환")
fun open_status_matching_region_future_reg_end_date_returns_event() {
// given
save(status = MarathonEventStatus.OPEN, region = "수도권", regEndDate = now.plusDays(7))
save(status = MarathonEventStatus.OPEN, region = "서울특별시 마포구", regEndDate = now.plusDays(7))

// when
val results = repository.findOpenByRegions(listOf("수도권"))
Expand All @@ -70,7 +70,7 @@ class MarathonEventQueryRepositoryTest {
@DisplayName("UPCOMING 상태 + 지역 일치 - 반환")
fun upcoming_status_matching_region_returns_event() {
// given
save(status = MarathonEventStatus.UPCOMING, region = "수도권", regEndDate = now.plusDays(7))
save(status = MarathonEventStatus.UPCOMING, region = "경기도 성남시", regEndDate = now.plusDays(7))

// when
val results = repository.findOpenByRegions(listOf("수도권"))
Expand All @@ -83,7 +83,7 @@ class MarathonEventQueryRepositoryTest {
@DisplayName("CLOSED 상태 - 제외")
fun closed_status_excludes_event() {
// given
save(status = MarathonEventStatus.CLOSED, region = "수도권", regEndDate = now.plusDays(7))
save(status = MarathonEventStatus.CLOSED, region = "서울특별시 강남구", regEndDate = now.plusDays(7))

// when
val results = repository.findOpenByRegions(listOf("수도권"))
Expand All @@ -96,7 +96,7 @@ class MarathonEventQueryRepositoryTest {
@DisplayName("지역 불일치 - 제외")
fun mismatched_region_excludes_event() {
// given
save(status = MarathonEventStatus.OPEN, region = "충청권", regEndDate = now.plusDays(7))
save(status = MarathonEventStatus.OPEN, region = "충청북도 청주시", regEndDate = now.plusDays(7))

// when
val results = repository.findOpenByRegions(listOf("수도권"))
Expand All @@ -109,7 +109,7 @@ class MarathonEventQueryRepositoryTest {
@DisplayName("regEndDate가 과거이면 제외")
fun past_reg_end_date_excludes_event() {
// given
save(status = MarathonEventStatus.OPEN, region = "수도권", regEndDate = now.minusDays(1))
save(status = MarathonEventStatus.OPEN, region = "서울특별시 마포구", regEndDate = now.minusDays(1))

// when
val results = repository.findOpenByRegions(listOf("수도권"))
Expand All @@ -122,7 +122,7 @@ class MarathonEventQueryRepositoryTest {
@DisplayName("regEndDate가 null이면 포함")
fun null_reg_end_date_includes_event() {
// given
save(status = MarathonEventStatus.OPEN, region = "수도권", regEndDate = null)
save(status = MarathonEventStatus.OPEN, region = "인천광역시 남동구", regEndDate = null)

// when
val results = repository.findOpenByRegions(listOf("수도권"))
Expand All @@ -135,7 +135,7 @@ class MarathonEventQueryRepositoryTest {
@DisplayName("여러 지역 중 하나라도 일치하면 반환")
fun at_least_one_matching_region_returns_event() {
// given
save(status = MarathonEventStatus.OPEN, region = "충청권", regEndDate = now.plusDays(7))
save(status = MarathonEventStatus.OPEN, region = "충청북도 청주시", regEndDate = now.plusDays(7))

// when
val results = repository.findOpenByRegions(listOf("수도권", "충청권"))
Expand All @@ -148,9 +148,9 @@ class MarathonEventQueryRepositoryTest {
@DisplayName("OPEN 이벤트는 regEndDate 오름차순 정렬")
fun open_events_sorted_by_reg_end_date_ascending() {
// given
save(status = MarathonEventStatus.OPEN, region = "수도권", regEndDate = now.plusDays(10))
save(status = MarathonEventStatus.OPEN, region = "수도권", regEndDate = now.plusDays(3))
save(status = MarathonEventStatus.OPEN, region = "수도권", regEndDate = now.plusDays(7))
save(status = MarathonEventStatus.OPEN, region = "서울특별시 마포구", regEndDate = now.plusDays(10))
save(status = MarathonEventStatus.OPEN, region = "경기도 성남시", regEndDate = now.plusDays(3))
save(status = MarathonEventStatus.OPEN, region = "인천광역시 연수구", regEndDate = now.plusDays(7))

// when
val results = repository.findOpenByRegions(listOf("수도권"))
Expand All @@ -165,8 +165,8 @@ class MarathonEventQueryRepositoryTest {
@DisplayName("OPEN이 UPCOMING보다 먼저 정렬")
fun open_events_sorted_before_upcoming_events() {
// given
save(status = MarathonEventStatus.UPCOMING, region = "수도권", regEndDate = now.plusDays(10))
save(status = MarathonEventStatus.OPEN, region = "수도권", regEndDate = now.plusDays(5))
save(status = MarathonEventStatus.UPCOMING, region = "서울특별시 송파구", regEndDate = now.plusDays(10))
save(status = MarathonEventStatus.OPEN, region = "서울특별시 마포구", regEndDate = now.plusDays(5))

// when
val results = repository.findOpenByRegions(listOf("수도권"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.maggom.event.adapter.out.persistence

import com.maggom.event.domain.MarathonEventStatus
import com.maggom.event.domain.RegionGroup
import com.querydsl.core.BooleanBuilder
import com.querydsl.core.types.dsl.Expressions
import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.stereotype.Repository
Expand All @@ -24,11 +26,16 @@ class MarathonEventQueryRepository(
.`when`(event.status.eq(MarathonEventStatus.OPEN)).then(event.regEndDate)
.otherwise(event.regStartDate)

val prefixes = regions.flatMap { RegionGroup.toPrefixes(it) }
val regionPredicate = prefixes.fold(BooleanBuilder()) { builder, prefix ->
builder.or(event.region.startsWith(prefix))
}

return queryFactory
.selectFrom(event)
.where(
event.status.`in`(MarathonEventStatus.OPEN, MarathonEventStatus.UPCOMING),
event.region.`in`(regions),
regionPredicate,
event.regEndDate.isNull.or(event.regEndDate.gt(now)),
)
.orderBy(
Expand Down
15 changes: 15 additions & 0 deletions event/src/main/kotlin/com/maggom/event/domain/RegionGroup.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.maggom.event.domain

object RegionGroup {

private val PREFIXES: Map<String, List<String>> = mapOf(
"수도권" to listOf("서울", "경기도", "인천"),
"충청권" to listOf("충청북도", "충청남도", "대전", "세종"),
"경상권" to listOf("경상북도", "경상남도", "대구", "부산", "울산"),
"전라권" to listOf("전라북도", "전라남도", "전북특별자치도", "광주"),
"강원권" to listOf("강원도", "강원특별자치도"),
"제주권" to listOf("제주"),
)

fun toPrefixes(regionGroup: String): List<String> = PREFIXES[regionGroup] ?: listOf(regionGroup)
}
Loading