Skip to content

Commit

Permalink
Merge pull request #67 from whatever-mentoring/feature/create-goal-va…
Browse files Browse the repository at this point in the history
…lidation-check

Impleemnt validating duplicated and proceeding goal
  • Loading branch information
mkSpace authored Dec 26, 2023
2 parents a968392 + 12136cb commit 8ccc838
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class GoalApplicationService(
exceptionCode = ExceptionCode.E409_CONFLICT,
executionMessage = "다짐을 생성하는 중, 생성할 수 있는 다짐 갯수를 초과하였습니다."
)
if (goalService.existsByUserIdAndAnyResult(userId, Result.PROCEEDING)) throw BaseException.of(
exceptionCode = ExceptionCode.E409_CONFLICT,
executionMessage = "다짐을 생성하는 중, 이미 생성한 다짐이 있어 생성이 불가합니다."
)
val goal = goalService.create(
userId = userId,
content = content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ interface GoalRepository : JpaRepository<GoalEntity, Long> {
fun findByIdAndUserEntity(id: Long, userEntity: UserEntity): GoalEntity?

fun findAllByEndDateLessThanEqualAndResultIs(endDate: LocalDateTime, result: Result): List<GoalEntity>

fun findAllByUserEntityAndResult(userEntity: UserEntity, result: Result): List<GoalEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class GoalService(
?: throw IllegalArgumentException("다짐을 조회하는 중, 잘못된 내용을 요청하셨습니다.")
}

fun existsByUserIdAndAnyResult(userId: Long, result: Result): Boolean {
val userEntity = userRepository.findByIdOrNull(userId)
?: throw IllegalArgumentException("cannot find user $userId")
return goalRepository.findAllByUserEntityAndResult(userEntity, result).isNotEmpty()
}

fun loadAllByUserId(userId: Long): List<Goal> {
return goalRepository.findAllByUserEntity(
userRepository.findById(userId).get()
Expand Down

0 comments on commit 8ccc838

Please sign in to comment.