Skip to content

Commit

Permalink
merge: (#224) 기간 매개변수 변경 (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
khcho0125 authored Dec 17, 2022
2 parents fa5689c + b048a34 commit 8071da0
Show file tree
Hide file tree
Showing 22 changed files with 160 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package team.comit.simtong.domain.menu.dto

import java.io.File
import java.util.UUID

/**
*
* 메뉴 저장 요청 정보를 전달하는 SaveMenuRequest
*
* @author Chokyunghyeon
* @date 2022/12/17
* @version 1.0.0
**/
data class SaveMenuRequest(
val file: File,

val year: Int,

val month: Int,

val spotId: UUID
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class QueryMenuByMonthUseCase(
private val menuSecurityPort: MenuSecurityPort
) {

fun execute(date: LocalDate): MenuResponse {
fun execute(startAt: LocalDate, endAt: LocalDate): MenuResponse {
val currentUserId = menuSecurityPort.getCurrentUserId()
val user = queryUserPort.queryUserById(currentUserId) ?: throw UserExceptions.NotFound()

val menu = queryMenuPort.queryMenusByMonthAndSpotId(date, user.spotId)
val menu = queryMenuPort.queryMenusByPeriodAndSpotId(startAt, endAt, user.spotId)
val result = menu.map { MenuResponse.MenuElement(it.date, it.meal) }

return MenuResponse(result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class QueryPublicMenuUseCase(
private val queryMenuPort: QueryMenuPort,
) {

fun execute(date: LocalDate): MenuResponse {
val menu = queryMenuPort.queryMenusByMonthAndSpotName(date, Spot.HEAD_SHOP)
fun execute(startAt: LocalDate, endAt: LocalDate): MenuResponse {
val menu = queryMenuPort.queryMenusByPeriodAndSpotName(startAt, endAt, Spot.HEAD_SHOP)
val result = menu.map { MenuResponse.MenuElement(it.date, it.meal) }

return MenuResponse(result)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package team.comit.simtong.domain.menu.usecase

import team.comit.simtong.domain.menu.dto.SaveMenuRequest
import team.comit.simtong.domain.menu.exception.MenuExceptions
import team.comit.simtong.domain.menu.spi.CommandMenuPort
import team.comit.simtong.domain.menu.spi.ParseMenuFilePort
import team.comit.simtong.domain.menu.spi.QueryMenuPort
import team.comit.simtong.global.annotation.UseCase
import java.io.File
import java.time.LocalDate
import java.util.UUID

/**
*
Expand All @@ -24,10 +23,12 @@ class SaveMenuUseCase(
private val commandMenuPort: CommandMenuPort
) {

fun execute(file: File, year: Int, month: Int, spotId: UUID) {
fun execute(request: SaveMenuRequest) {
val (file, year, month, spotId) = request

val menu = parseMenuFilePort.importMenu(file, year, month, spotId)

if (queryMenuPort.queryMenusByMonthAndSpotId(LocalDate.of(year, month, 1), spotId).isNotEmpty()) {
if (queryMenuPort.existsMenuByMonthAndSpotId(LocalDate.of(year, month, 1), spotId)) {
throw MenuExceptions.AlreadyExistsSameMonth("${year}${month}월 메뉴가 이미 존재합니다.")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class QueryEntireSpotScheduleUseCase(
private val querySchedulePort: QuerySchedulePort
) {

fun execute(date: LocalDate): QueryEntireSpotScheduleResponse {
val list = querySchedulePort.querySpotSchedulesByMonthAndScope(date, Scope.ENTIRE)
fun execute(startAt: LocalDate, endAt: LocalDate): QueryEntireSpotScheduleResponse {
val list = querySchedulePort.querySpotSchedulesByPeriodAndScope(startAt, endAt, Scope.ENTIRE)

val response = list.map {
SpotScheduleResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ class QueryIndividualSpotScheduleUseCase(
private val securityPort: ScheduleSecurityPort
) {

fun execute(date: LocalDate): QueryIndividualSpotScheduleResponse {
fun execute(startAt: LocalDate, endAt: LocalDate): QueryIndividualSpotScheduleResponse {
val user = queryUserPort.queryUserById(securityPort.getCurrentUserId())
?: throw UserExceptions.NotFound()

val individualSchedules = querySchedulePort.querySchedulesByMonthAndUserIdAndScope(
date, user.id, Scope.INDIVIDUAL
val individualSchedules = querySchedulePort.querySchedulesByPeriodAndUserIdAndScope(
startAt, endAt, user.id, Scope.INDIVIDUAL
)

val ownSpotSchedules = querySchedulePort.querySchedulesByMonthAndSpotIdAndScope(
date, user.spotId, Scope.ENTIRE
val ownSpotSchedules = querySchedulePort.querySchedulesByPeriodAndSpotIdAndScope(
startAt, endAt, user.spotId, Scope.ENTIRE
)

val schedules = (ownSpotSchedules + individualSchedules) // 개인 일정과 소속 지점 일정 합치기
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class QueryIndividualHolidayUseCaseTests {

private val userId: UUID = UUID.randomUUID()

private val dateStub: LocalDate = LocalDate.now()
private val date: LocalDate = LocalDate.now()

private val holidaysStub: List<Holiday> by lazy {
listOf(
Expand Down Expand Up @@ -66,11 +66,11 @@ class QueryIndividualHolidayUseCaseTests {
given(securityPort.getCurrentUserId())
.willReturn(userId)

given(queryHolidayPort.queryHolidaysByPeriodAndUserId(dateStub, dateStub, userId))
given(queryHolidayPort.queryHolidaysByPeriodAndUserId(date, date, userId))
.willReturn(holidaysStub)

// when
val response = queryIndividualHolidayUseCase.execute(dateStub, dateStub)
val response = queryIndividualHolidayUseCase.execute(date, date)

// then
assertEquals(response, responseStub)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ class QueryMenuByMonthUseCaseTests {
given(queryUserPort.queryUserById(currentUserId))
.willReturn(userStub)

given(queryMenuPort.queryMenusByMonthAndSpotId(now, userStub.spotId))
given(queryMenuPort.queryMenusByPeriodAndSpotId(now, now, userStub.spotId))
.willReturn(
listOf(menuStub, menuStub2)
)

// when
val response = queryMenuByMonthUseCase.execute(now)
val response = queryMenuByMonthUseCase.execute(now, now)

// then
assertThat(response).isNotNull
Expand All @@ -103,7 +103,7 @@ class QueryMenuByMonthUseCaseTests {

// when & then
assertThrows<UserExceptions.NotFound> {
queryMenuByMonthUseCase.execute(now)
queryMenuByMonthUseCase.execute(now, now)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ class QueryPublicMenuUseCaseTests {
fun `메뉴 조회 성공`() {
// given
val now = LocalDate.now()
given(queryMenuPort.queryMenusByMonthAndSpotName(now, Spot.HEAD_SHOP))
given(queryMenuPort.queryMenusByPeriodAndSpotName(now, now, Spot.HEAD_SHOP))
.willReturn(
listOf(menuStub, menuStub2)
)

// when
val response = queryPublicMenuUseCase.execute(now)
val response = queryPublicMenuUseCase.execute(now, now)

// then
Assertions.assertThat(response).isNotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows
import org.mockito.kotlin.given
import org.springframework.boot.test.mock.mockito.MockBean
import team.comit.simtong.domain.menu.dto.SaveMenuRequest
import team.comit.simtong.domain.menu.exception.MenuExceptions
import team.comit.simtong.domain.menu.model.Menu
import team.comit.simtong.domain.menu.spi.CommandMenuPort
Expand Down Expand Up @@ -43,6 +44,15 @@ class SaveMenuUseCaseTests {
)
}

private val requestStub: SaveMenuRequest by lazy {
SaveMenuRequest(
file = fileStub,
year = year,
month = month,
spotId = spotId
)
}

@BeforeEach
fun setUp() {
saveMenuUseCase = SaveMenuUseCase(
Expand All @@ -58,12 +68,12 @@ class SaveMenuUseCaseTests {
given(parseMenuFilePort.importMenu(fileStub, year, month, spotId))
.willReturn(listOf(menuStub))

given(queryMenuPort.queryMenusByMonthAndSpotId(LocalDate.of(year, month, 1), spotId))
.willReturn(emptyList())
given(queryMenuPort.existsMenuByMonthAndSpotId(LocalDate.of(year, month, 1), spotId))
.willReturn(false)

// when & then
assertDoesNotThrow {
saveMenuUseCase.execute(fileStub, year, month, spotId)
saveMenuUseCase.execute(requestStub)
}
}

Expand All @@ -73,12 +83,12 @@ class SaveMenuUseCaseTests {
given(parseMenuFilePort.importMenu(fileStub, year, month, spotId))
.willReturn(listOf(menuStub))

given(queryMenuPort.queryMenusByMonthAndSpotId(LocalDate.of(year, month, 1), spotId))
.willReturn(listOf(menuStub))
given(queryMenuPort.existsMenuByMonthAndSpotId(LocalDate.of(year, month, 1), spotId))
.willReturn(true)

// when & then
assertThrows<MenuExceptions.AlreadyExistsSameMonth> {
saveMenuUseCase.execute(fileStub, year, month, spotId)
saveMenuUseCase.execute(requestStub)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ class QueryEntireSpotScheduleUseCaseTests {
@Test
fun `전체 지점 일정 조회 성공`() {
// given
given(querySchedulePort.querySpotSchedulesByMonthAndScope(date, Scope.ENTIRE))
given(querySchedulePort.querySpotSchedulesByPeriodAndScope(date, date, Scope.ENTIRE))
.willReturn(spotScheduleListStub)

// when
val response = queryEntireSpotScheduleUseCase.execute(date)
val response = queryEntireSpotScheduleUseCase.execute(date, date)

// then
assertEquals(response, responseStub)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,18 @@ class QueryIndividualSpotScheduleUseCaseTests {
given(queryUserPort.queryUserById(userId))
.willReturn(userStub)

given(querySchedulePort.querySchedulesByMonthAndUserIdAndScope(date, userStub.id, Scope.INDIVIDUAL))
given(querySchedulePort.querySchedulesByPeriodAndUserIdAndScope(date, date, userStub.id, Scope.INDIVIDUAL))
.willReturn(
listOf(individualScheduleStub)
)

given(querySchedulePort.querySchedulesByMonthAndSpotIdAndScope(date, userStub.spotId, Scope.ENTIRE))
given(querySchedulePort.querySchedulesByPeriodAndSpotIdAndScope(date, date, userStub.spotId, Scope.ENTIRE))
.willReturn(
listOf(entireScheduleStub)
)

// when
val response = queryIndividualSpotScheduleUseCase.execute(date)
val response = queryIndividualSpotScheduleUseCase.execute(date, date)

// then
assertThat(response).isEqualTo(responseStub)
Expand All @@ -152,7 +152,7 @@ class QueryIndividualSpotScheduleUseCaseTests {

// when & then
assertThrows<UserExceptions.NotFound> {
queryIndividualSpotScheduleUseCase.execute(date)
queryIndividualSpotScheduleUseCase.execute(date, date)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import java.util.UUID
**/
interface QueryMenuPort {

fun queryMenusByMonthAndSpotId(date: LocalDate, spotId: UUID): List<Menu>
fun existsMenuByMonthAndSpotId(date: LocalDate, spotId: UUID): Boolean

fun queryMenusByMonthAndSpotName(date: LocalDate, spotName: String): List<Menu>
fun queryMenusByPeriodAndSpotId(startAt: LocalDate, endAt: LocalDate, spotId: UUID): List<Menu>

fun queryMenusByPeriodAndSpotName(startAt: LocalDate, endAt: LocalDate, spotName: String): List<Menu>

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ interface QuerySchedulePort {

fun queryScheduleById(id: UUID): Schedule?

fun querySpotSchedulesByMonthAndScope(date: LocalDate, scope: Scope): List<SpotSchedule>
fun querySpotSchedulesByPeriodAndScope(startAt: LocalDate, endAt: LocalDate, scope: Scope): List<SpotSchedule>

fun querySchedulesByMonthAndSpotIdAndScope(date: LocalDate, spotId: UUID, scope: Scope): List<Schedule>
fun querySchedulesByPeriodAndSpotIdAndScope(startAt: LocalDate, endAt: LocalDate, spotId: UUID, scope: Scope): List<Schedule>

fun querySchedulesByMonthAndUserIdAndScope(date: LocalDate, userId: UUID, scope: Scope): List<Schedule>
fun querySchedulesByPeriodAndUserIdAndScope(startAt: LocalDate, endAt: LocalDate, userId: UUID, scope: Scope): List<Schedule>

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class SecurityConfig(

// menu
.antMatchers(HttpMethod.GET, "/menu/public").permitAll()
.antMatchers(HttpMethod.POST, "/menu").permitAll()
.antMatchers(HttpMethod.POST, "/menu/{spot-id}").permitAll()

// files
.antMatchers(HttpMethod.POST, "/files").permitAll()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package team.comit.simtong.persistence.menu

import com.querydsl.core.types.dsl.BooleanExpression
import team.comit.simtong.persistence.menu.entity.QMenuJpaEntity.menuJpaEntity as menu
import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.stereotype.Component
import team.comit.simtong.domain.menu.model.Menu
import team.comit.simtong.domain.menu.spi.MenuPort
import team.comit.simtong.persistence.QuerydslExtensionUtils.sameMonthFilter
import team.comit.simtong.persistence.menu.entity.QMenuJpaEntity.menuJpaEntity
import team.comit.simtong.persistence.menu.mapper.MenuMapper
import team.comit.simtong.persistence.menu.repository.MenuJpaRepository
import team.comit.simtong.persistence.spot.entity.QSpotJpaEntity.spotJpaEntity
import java.time.LocalDate
import java.util.UUID

Expand All @@ -29,28 +27,36 @@ class MenuPersistenceAdapter(
private val queryFactory: JPAQueryFactory
) : MenuPort {

override fun queryMenusByMonthAndSpotId(date: LocalDate, spotId: UUID): List<Menu> {
override fun existsMenuByMonthAndSpotId(date: LocalDate, spotId: UUID): Boolean {
return queryFactory
.selectFrom(menuJpaEntity)
.join(menuJpaEntity.spot, spotJpaEntity)
.on(spotJpaEntity.id.eq(spotId))
.selectFrom(menu)
.where(
sameMonthMenuFilter(date)
menu.menuId.spotId.eq(spotId),
menu.menuId.date.sameMonthFilter(date)
)
.orderBy(menuJpaEntity.menuId.date.asc())
.fetchOne() != null
}

override fun queryMenusByPeriodAndSpotId(startAt: LocalDate, endAt: LocalDate, spotId: UUID): List<Menu> {
return queryFactory
.selectFrom(menu)
.where(
menu.menuId.spotId.eq(spotId),
menu.menuId.date.between(startAt, endAt)
)
.orderBy(menu.menuId.date.asc())
.fetch()
.map { menuMapper.toDomain(it)!! }
}

override fun queryMenusByMonthAndSpotName(date: LocalDate, spotName: String): List<Menu> {
override fun queryMenusByPeriodAndSpotName(startAt: LocalDate, endAt: LocalDate, spotName: String): List<Menu> {
return queryFactory
.selectFrom(menuJpaEntity)
.join(menuJpaEntity.spot, spotJpaEntity)
.on(spotJpaEntity.name.eq(spotName))
.selectFrom(menu)
.where(
sameMonthMenuFilter(date)
menu.spot.name.eq(spotName),
menu.menuId.date.between(startAt, endAt)
)
.orderBy(menuJpaEntity.menuId.date.asc())
.orderBy(menu.menuId.date.asc())
.fetch()
.map { menuMapper.toDomain(it)!! }
}
Expand All @@ -61,8 +67,4 @@ class MenuPersistenceAdapter(
)
}

private fun sameMonthMenuFilter(date: LocalDate) : BooleanExpression {
return menuJpaEntity.menuId.date.sameMonthFilter(date)
}

}
Loading

0 comments on commit 8071da0

Please sign in to comment.