Skip to content

Commit

Permalink
refactor: (#286) Schedule Aggregate 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
softpeanut committed Jan 10, 2023
1 parent 56a3336 commit 3247be6
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import team.comit.simtong.global.annotation.UseCase
*
* @author Chokyunghyeon
* @date 2022/11/26
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class AddIndividualScheduleUseCase(
Expand All @@ -32,7 +32,7 @@ class AddIndividualScheduleUseCase(
?: throw UserExceptions.NotFound()

commandSchedulePort.save(
Schedule(
Schedule.of(
userId = currentUserId,
spotId = user.spotId,
title = title,
Expand All @@ -43,5 +43,4 @@ class AddIndividualScheduleUseCase(
)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import team.comit.simtong.global.annotation.UseCase
* 지점 일정 추가를 담당하는 AddSpotScheduleUseCase
*
* @author Chokyunghyeon
* @author kimbeomjin
* @date 2022/11/21
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class AddSpotScheduleUseCase(
Expand All @@ -37,7 +38,7 @@ class AddSpotScheduleUseCase(
}

commandSchedulePort.save(
Schedule(
Schedule.of(
userId = currentUserId,
spotId = spotId,
title = title,
Expand All @@ -47,5 +48,4 @@ class AddSpotScheduleUseCase(
)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import team.comit.simtong.global.annotation.UseCase
* 개인 일정 정보 변경 요청을 담당하는 ChangeIndividualScheduleUseCase
*
* @author Chokyunghyeon
* @author kimbeomjin
* @date 2022/11/27
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class ChangeIndividualScheduleUseCase(
Expand All @@ -35,18 +36,14 @@ class ChangeIndividualScheduleUseCase(
val user = queryUserPort.queryUserById(currentUserId)
?: throw UserExceptions.NotFound()

if (user.id != schedule.userId) {
throw ScheduleExceptions.NotScheduleOwner()
}

commandSchedulePort.save(
schedule.copy(
schedule.changeIndividualSchedule(
title = title,
startAt = startAt,
endAt = endAt,
alarmTime = alarm
alarmTime = alarm,
userId = user.id
)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import team.comit.simtong.global.annotation.UseCase
* 지점 일정 변경 기능을 담당하는 ChangeSpotScheduleUseCase
*
* @author Chokyunghyeon
* @author kimbeomjin
* @date 2022/11/22
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class ChangeSpotScheduleUseCase(
Expand All @@ -36,15 +37,12 @@ class ChangeSpotScheduleUseCase(
val schedule = querySchedulePort.queryScheduleById(request.scheduleId)
?: throw ScheduleExceptions.NotFound()

when {
Scope.ENTIRE != schedule.scope -> throw ScheduleExceptions.NotScheduleOwner()

user.spotId != schedule.spotId && Authority.ROLE_SUPER != user.authority ->
throw UserExceptions.NotEnoughPermission("같은 지점 관리자이거나 최고 관리자이어야 합니다.")
if (schedule.isSameSpot(user.spotId) && user.authority != Authority.ROLE_SUPER) {
throw UserExceptions.NotEnoughPermission("같은 지점 관리자이거나 최고 관리자이어야 합니다.")
}

commandSchedulePort.save(
schedule.copy(
schedule.changeEntireSchedule(
title = request.title,
startAt = request.startAt,
endAt = request.endAt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ class QueryEntireSpotScheduleUseCase(

return QueryEntireSpotScheduleResponse(response)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import java.util.UUID
*
* @author kimbeomjin
* @date 2022/12/03
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class RemoveIndividualScheduleUseCase(
Expand All @@ -35,13 +35,8 @@ class RemoveIndividualScheduleUseCase(
val schedule = querySchedulePort.queryScheduleById(scheduleId)
?: throw ScheduleExceptions.NotFound()

if (user.id != schedule.userId) {
throw ScheduleExceptions.NotScheduleOwner()
}

if (Scope.INDIVIDUAL != schedule.scope) {
throw ScheduleExceptions.DifferentScope("개인 일정이 아닙니다.")
}
schedule.checkScope(Scope.INDIVIDUAL)
schedule.checkOwner(user.id)

commandSchedulePort.delete(schedule)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.util.UUID
*
* @author Chokyunghyeon
* @date 2022/11/22
* @version 1.0.0
* @version 1.2.5
**/
@UseCase
class RemoveSpotScheduleUseCase(
Expand All @@ -36,15 +36,12 @@ class RemoveSpotScheduleUseCase(
val schedule = querySchedulePort.queryScheduleById(scheduleId)
?: throw ScheduleExceptions.NotFound()

if (user.spotId != schedule.spotId && user.authority != Authority.ROLE_SUPER) {
throw UserExceptions.NotEnoughPermission("같은 지점 관리자이거나 최고 관리자이어야 합니다.")
}
schedule.checkScope(Scope.ENTIRE)

if (Scope.ENTIRE != schedule.scope) {
throw ScheduleExceptions.DifferentScope("지점 일정이 아닙니다.")
if (schedule.isSameSpot(user.spotId) && user.authority != Authority.ROLE_SUPER) {
throw UserExceptions.NotEnoughPermission("같은 지점 관리자이거나 최고 관리자이어야 합니다.")
}

commandSchedulePort.delete(schedule)
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package team.comit.simtong.domain.schedule.model

import team.comit.simtong.domain.schedule.exception.ScheduleExceptions
import java.time.LocalDate
import java.time.LocalTime
import java.util.UUID
Expand All @@ -9,11 +10,12 @@ import java.util.UUID
* 일정 Aggregate의 Root를 담당하는 Schedule
*
* @author Chokyunghyeon
* @author kimbeomjin
* @date 2022/11/21
* @version 1.0.0
* @version 1.2.5
**/
data class Schedule(
val id: UUID = UUID(0, 0),
val id: UUID,

val userId: UUID,

Expand All @@ -27,15 +29,82 @@ data class Schedule(

val endAt: LocalDate,

val alarmTime: LocalTime = DEFAULT_ALARM_TIME
val alarmTime: LocalTime
) {

companion object {

/**
* Default AM 8:30
*/
val DEFAULT_ALARM_TIME: LocalTime = LocalTime.of(8,30)
val DEFAULT_ALARM_TIME: LocalTime = LocalTime.of(8, 30)

fun of(
id: UUID = UUID(0, 0),
userId: UUID,
spotId: UUID,
title: String,
scope: Scope,
startAt: LocalDate,
endAt: LocalDate,
alarmTime: LocalTime = DEFAULT_ALARM_TIME
) = Schedule(
id = id,
userId = userId,
spotId = spotId,
title = title,
scope = scope,
startAt = startAt,
endAt = endAt,
alarmTime = alarmTime
)
}

fun isSameSpot(spotId: UUID) = this.spotId == spotId

fun changeIndividualSchedule(
title: String,
startAt: LocalDate,
endAt: LocalDate,
alarmTime: LocalTime,
userId: UUID
): Schedule {
checkScope(Scope.INDIVIDUAL)
checkOwner(userId)

return this.copy(
title = title,
startAt = startAt,
endAt = endAt,
alarmTime = alarmTime
)
}

fun changeEntireSchedule(
title: String,
startAt: LocalDate,
endAt: LocalDate
): Schedule {
checkScope(Scope.ENTIRE)

return this.copy(
title = title,
startAt = startAt,
endAt = endAt
)
}

fun checkOwner(userId: UUID) {
if (this.userId == userId) {
throw ScheduleExceptions.NotScheduleOwner()
}
}

fun checkScope(scope: Scope) {
when (scope) {
Scope.INDIVIDUAL -> if (this.scope != Scope.INDIVIDUAL) throw ScheduleExceptions.DifferentScope("개인 일정이 아닙니다.")

Scope.ENTIRE -> if (this.scope != Scope.ENTIRE) throw ScheduleExceptions.DifferentScope("지점 일정이 아닙니다.")
}
}
}

0 comments on commit 3247be6

Please sign in to comment.