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

Impleemnt validating duplicated and proceeding goal #67

Merged
merged 1 commit into from
Dec 26, 2023
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 @@ -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