Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge: (#149) 전체 지점 일정 조회 #151

Merged
merged 7 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -3,8 +3,6 @@ package team.comit.simtong.domain.schedule.usecase
import team.comit.simtong.domain.schedule.dto.EntireSpotScheduleResponse
import team.comit.simtong.domain.schedule.dto.SpotScheduleResponse
import team.comit.simtong.domain.schedule.spi.QuerySchedulePort
import team.comit.simtong.domain.schedule.spi.ScheduleQuerySpotPort
import team.comit.simtong.domain.spot.exception.SpotNotFoundException
import team.comit.simtong.global.annotation.ReadOnlyUseCase
import java.time.LocalDate

Expand All @@ -18,27 +16,24 @@ import java.time.LocalDate
**/
@ReadOnlyUseCase
class EntireSpotScheduleUseCase(
private val querySchedulePort: QuerySchedulePort,
private val querySpotPort: ScheduleQuerySpotPort
private val querySchedulePort: QuerySchedulePort
) {

fun execute(date: LocalDate): EntireSpotScheduleResponse {
val list = querySchedulePort.querySchedulesByMonth(date.year, date.monthValue)
val list = querySchedulePort.querySchedulesByDateContains(date)

val response = list.map {
SpotScheduleResponse(
id = it.id,
startAt = it.startAt,
endAt = it.endAt,
title = it.title,
spot = querySpotPort.querySpotById(it.spotId)?.let { spot ->
SpotScheduleResponse.SpotElement(
id = spot.id,
name = spot.name
)
} ?: throw SpotNotFoundException.EXCEPTION
SpotScheduleResponse(
id = it.id,
startAt = it.startAt,
endAt = it.endAt,
title = it.title,
spot = SpotScheduleResponse.SpotElement(
id = it.spotId,
name = it.spotName
)
}
)
}

return EntireSpotScheduleResponse(response)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,15 @@ package team.comit.simtong.domain.schedule.usecase
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.kotlin.given
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.context.junit.jupiter.SpringExtension
import team.comit.simtong.domain.schedule.dto.EntireSpotScheduleResponse
import team.comit.simtong.domain.schedule.dto.SpotScheduleResponse
import team.comit.simtong.domain.schedule.model.Schedule
import team.comit.simtong.domain.schedule.model.Scope
import team.comit.simtong.domain.schedule.spi.QuerySchedulePort
import team.comit.simtong.domain.schedule.spi.ScheduleQuerySpotPort
import team.comit.simtong.domain.spot.exception.SpotNotFoundException
import team.comit.simtong.domain.spot.model.Spot
import team.comit.simtong.domain.schedule.vo.SpotSchedule
import java.time.LocalDate
import java.time.LocalTime
import java.util.UUID

@ExtendWith(SpringExtension::class)
Expand All @@ -26,36 +20,23 @@ class EntireSpotScheduleUseCaseTests {
@MockBean
private lateinit var querySchedulePort: QuerySchedulePort

@MockBean
private lateinit var querySpotPort: ScheduleQuerySpotPort

private lateinit var entireSpotScheduleUseCase: EntireSpotScheduleUseCase

private val date: LocalDate = LocalDate.now()

private val uuid: UUID = UUID.randomUUID()

private val scheduleListStub = listOf(
Schedule(
private val spotScheduleListStub = listOf(
SpotSchedule(
id = uuid,
userId = uuid,
spotId = uuid,
spotName = "test name",
title = "test title",
scope = Scope.ENTIRE,
startAt = date,
endAt = date,
alarmTime = LocalTime.now()
endAt = date
)
)

private val spotStub: Spot by lazy {
Spot(
id = uuid,
name = "test name",
location = "test location"
)
}

private val responseStub: EntireSpotScheduleResponse by lazy {
EntireSpotScheduleResponse(
listOf(
Expand All @@ -75,17 +56,14 @@ class EntireSpotScheduleUseCaseTests {

@BeforeEach
fun setUp() {
entireSpotScheduleUseCase = EntireSpotScheduleUseCase(querySchedulePort, querySpotPort)
entireSpotScheduleUseCase = EntireSpotScheduleUseCase(querySchedulePort)
}

@Test
fun `전체 지점 일정 조회 성공`() {
// given
given(querySchedulePort.querySchedulesByMonth(date.year, date.monthValue))
.willReturn(scheduleListStub)

given(querySpotPort.querySpotById(uuid))
.willReturn(spotStub)
given(querySchedulePort.querySchedulesByDateContains(date))
.willReturn(spotScheduleListStub)

// when
val response = entireSpotScheduleUseCase.execute(date)
Expand All @@ -94,18 +72,4 @@ class EntireSpotScheduleUseCaseTests {
assertEquals(response, responseStub)
}

@Test
fun `지점을 찾을 수 없음`() {
// given
given(querySchedulePort.querySchedulesByMonth(date.year, date.monthValue))
.willReturn(scheduleListStub)

given(querySpotPort.querySpotById(uuid))
.willReturn(null)

// when & then
assertThrows<SpotNotFoundException> {
entireSpotScheduleUseCase.execute(date)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package team.comit.simtong.domain.schedule.spi

import team.comit.simtong.domain.schedule.model.Schedule
import team.comit.simtong.domain.schedule.vo.SpotSchedule
import java.time.LocalDate
import java.util.UUID

/**
Expand All @@ -15,6 +17,6 @@ interface QuerySchedulePort {

fun queryScheduleById(id: UUID): Schedule?

fun querySchedulesByMonth(year: Int, month: Int): List<Schedule>
fun querySchedulesByDateContains(date: LocalDate): List<SpotSchedule>

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package team.comit.simtong.persistence.schedule

import com.querydsl.core.types.dsl.DatePath
import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Component
import team.comit.simtong.domain.schedule.model.Schedule
import team.comit.simtong.domain.schedule.spi.SchedulePort
import team.comit.simtong.persistence.schedule.entity.QScheduleJpaEntity.scheduleJpaEntity
import team.comit.simtong.domain.schedule.vo.SpotSchedule
import team.comit.simtong.persistence.schedule.mapper.ScheduleMapper
import team.comit.simtong.persistence.schedule.vo.QSpotScheduleVo
import java.time.LocalDate
import java.util.UUID
import team.comit.simtong.persistence.schedule.entity.QScheduleJpaEntity.scheduleJpaEntity as schedule
import team.comit.simtong.persistence.spot.entity.QSpotJpaEntity.spotJpaEntity as spot

/**
*
Expand All @@ -23,7 +25,7 @@ import java.util.UUID
class SchedulePersistenceAdapter(
private val scheduleJpaRepository: ScheduleJpaRepository,
private val scheduleMapper: ScheduleMapper,
private val jpaQueryFactory: JPAQueryFactory
private val queryFactory: JPAQueryFactory
) : SchedulePort {

override fun save(schedule: Schedule) = scheduleMapper.toDomain(
Expand All @@ -44,24 +46,21 @@ class SchedulePersistenceAdapter(
)
}

override fun querySchedulesByMonth(year: Int, month: Int): List<Schedule> {
return jpaQueryFactory
.selectFrom(scheduleJpaEntity)
override fun querySchedulesByDateContains(date: LocalDate): List<SpotSchedule> {
val startDate = date.withDayOfMonth(1)
val endDate = date.withDayOfMonth(date.lengthOfMonth())

return queryFactory
.select(QSpotScheduleVo(schedule.id, schedule.title, schedule.startAt, schedule.endAt, spot.id, spot.name))
khcho0125 marked this conversation as resolved.
Show resolved Hide resolved
.from(schedule)
.where(
scheduleJpaEntity.startAt.monthEq(month)
.and(scheduleJpaEntity.startAt.yearEq(year))
.or(scheduleJpaEntity.endAt.monthEq(month)
.and(scheduleJpaEntity.endAt.yearEq(year)))
schedule.startAt.between(startDate, endDate)
.or(schedule.endAt.between(startDate, endDate))
)
.orderBy(scheduleJpaEntity.startAt.asc())
.join(spot)
khcho0125 marked this conversation as resolved.
Show resolved Hide resolved
.on(schedule.spot.eq(spot))
.orderBy(schedule.startAt.asc())
.fetch()
.map {
scheduleMapper.toDomain(it)!!
}
}

private fun DatePath<LocalDate>.yearEq(year: Int) = year().eq(year)

private fun DatePath<LocalDate>.monthEq(month: Int) = month().eq(month)

}