diff --git a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/BettingApplicationService.kt b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/BettingApplicationService.kt index 242c280..391c804 100644 --- a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/BettingApplicationService.kt +++ b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/BettingApplicationService.kt @@ -66,7 +66,7 @@ class BettingApplicationService( val betting = findByIdOrThrowException(bettingId) betting.validateOwnerId(userId) betting.validateStartDate() - bettingService.softDelete(bettingId) + bettingService.hardDelete(bettingId) } private fun findByIdOrThrowException(bettingId: Long): Betting { diff --git a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/UserApplicationService.kt b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/UserApplicationService.kt index 5f96f1e..e9174db 100644 --- a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/UserApplicationService.kt +++ b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/UserApplicationService.kt @@ -52,12 +52,18 @@ class UserApplicationService( executionMessage = "아직 진행중인 다짐이 있어 회원탈퇴에 실패했습니다." ) } - userService.hardDelete(id) - refreshTokenService.hardDelete(user) - goalService.hardDelete(user) - goalProofService.hardDelete(user) - gifticonService.hardDelete(user) - bettingService.hardDelete(user) + else if (bettingService.existsBettingParticipantUser(id)) { + throw BaseException.of( + exceptionCode = ExceptionCode.E400_BAD_REQUEST, + executionMessage = "아직 진행중인 다짐에 대한 내기가 있어 회원탈퇴에 실패했습니다." + ) + } + bettingService.hardDeleteByUserId(id) + gifticonService.hardDeleteByUserId(id) + goalProofService.hardDeleteByUserId(id) + goalService.hardDeleteByUserId(id) + refreshTokenService.hardDeleteByUserId(id) + userService.hardDeleteById(id) } fun isNicknameDuplicated( diff --git a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/betting/BettingService.kt b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/betting/BettingService.kt index daefcb1..786a7a9 100644 --- a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/betting/BettingService.kt +++ b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/betting/BettingService.kt @@ -68,6 +68,17 @@ class BettingService( return bettingRepository.findAllByUserEntity(userEntity).map { it.toDto() } } + fun existsBettingParticipantUser(userId: Long): Boolean { + val userEntity = + userRepository.findByIdOrNull(userId) ?: throw IllegalStateException("cannot find user $userId") + val bettings = bettingRepository.findAllByUserEntity(userEntity) + + for (betting in bettings) { + if (betting.goalEntity.endDate > LocalDateTime.now()) return true + } + return false + } + @Transactional fun update(bettingId: Long, predictionType: PredictionType): Betting { val betting = bettingRepository.findByIdOrNull(bettingId) @@ -101,8 +112,17 @@ class BettingService( } @Transactional - fun hardDelete(user: User) { - val bettings = bettingRepository.findAllByUserEntity(user.fromDto()) + fun hardDelete(bettingId: Long) { + val betting = bettingRepository.findByIdOrNull(bettingId) + ?: throw IllegalStateException("Cannot find betting $bettingId") + bettingRepository.delete(betting) + } + + @Transactional + fun hardDeleteByUserId(userId: Long) { + val bettings = bettingRepository.findAllByUserEntity( + userEntity = userRepository.findByIdOrNull(userId) ?: throw IllegalArgumentException("Cannot find user $userId") + ) bettingRepository.deleteAll(bettings) } } \ No newline at end of file diff --git a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/gifticon/GifticonService.kt b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/gifticon/GifticonService.kt index d1ae56a..914f34a 100644 --- a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/gifticon/GifticonService.kt +++ b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/gifticon/GifticonService.kt @@ -1,8 +1,7 @@ package com.whatever.raisedragon.domain.gifticon -import com.whatever.raisedragon.domain.user.User import com.whatever.raisedragon.domain.user.UserRepository -import com.whatever.raisedragon.domain.user.fromDto +import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -32,8 +31,10 @@ class GifticonService( } @Transactional - fun hardDelete(user: User) { - val gifticons = gifticonRepository.findAllByUserEntity(user.fromDto()) + fun hardDeleteByUserId(userId: Long) { + val gifticons = gifticonRepository.findAllByUserEntity( + userEntity = userRepository.findByIdOrNull(userId) ?: throw IllegalArgumentException("Cannot find user $userId") + ) gifticonRepository.deleteAll(gifticons) } } \ No newline at end of file diff --git a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/goal/GoalService.kt b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/goal/GoalService.kt index db57d82..c82c868 100644 --- a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/goal/GoalService.kt +++ b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/goal/GoalService.kt @@ -96,8 +96,19 @@ class GoalService( } @Transactional - fun hardDelete(user: User) { - val goals = goalRepository.findAllByUserEntity(user.fromDto()) + fun hardDelete( + goal: Goal, + userEntity: UserEntity, + ) { + val goalEntity = goal.fromDto(userEntity) + goalRepository.delete(goalEntity) + } + + @Transactional + fun hardDeleteByUserId(userId: Long) { + val goals = goalRepository.findAllByUserEntity( + userEntity = userRepository.findByIdOrNull(userId) ?: throw IllegalArgumentException("Cannot find user $userId") + ) goalRepository.deleteAll(goals) } @@ -108,6 +119,10 @@ class GoalService( } fun findProceedingGoalIsExistsByUser(user: User): Boolean { - return goalRepository.existsByUserEntityAndEndDateIsAfter(user.fromDto(), LocalDateTime.now()) + val existsByUserEntityAndEndDateIsAfter = goalRepository.existsByUserEntityAndEndDateIsAfter( + userEntity = user.fromDto(), + now = LocalDateTime.now() + ) + return existsByUserEntityAndEndDateIsAfter } } \ No newline at end of file diff --git a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/goalproof/GoalProofService.kt b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/goalproof/GoalProofService.kt index 512a465..3e7d398 100644 --- a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/goalproof/GoalProofService.kt +++ b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/goalproof/GoalProofService.kt @@ -66,8 +66,10 @@ class GoalProofService( } @Transactional - fun hardDelete(user: User) { - val goalProofs = goalProofRepository.findAllByUserEntity(user.fromDto()) + fun hardDeleteByUserId(userId: Long) { + val goalProofs = goalProofRepository.findAllByUserEntity( + userEntity = userRepository.findByIdOrNull(userId) ?: throw IllegalArgumentException("Cannot find user $userId") + ) goalProofRepository.deleteAll(goalProofs) } } \ No newline at end of file diff --git a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/refreshtoken/RefreshTokenService.kt b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/refreshtoken/RefreshTokenService.kt index 829e36a..c538523 100644 --- a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/refreshtoken/RefreshTokenService.kt +++ b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/refreshtoken/RefreshTokenService.kt @@ -2,14 +2,17 @@ package com.whatever.raisedragon.domain.refreshtoken import com.whatever.raisedragon.domain.user.User import com.whatever.raisedragon.domain.user.UserEntity +import com.whatever.raisedragon.domain.user.UserRepository import com.whatever.raisedragon.domain.user.fromDto +import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @Service @Transactional(readOnly = true) class RefreshTokenService( - private val refreshTokenRepository: RefreshTokenRepository + private val refreshTokenRepository: RefreshTokenRepository, + private val userRepository: UserRepository ) { @Transactional @@ -31,8 +34,10 @@ class RefreshTokenService( } @Transactional - fun hardDelete(user: User) { - val refreshTokenEntity = refreshTokenRepository.findByUserEntity(user.fromDto())!! + fun hardDeleteByUserId(userId: Long) { + val refreshTokenEntity = refreshTokenRepository.findByUserEntity( + userEntity = userRepository.findByIdOrNull(userId) ?: throw IllegalArgumentException("Cannot find user $userId") + )!! refreshTokenRepository.delete(refreshTokenEntity) } } \ No newline at end of file diff --git a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/user/UserService.kt b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/user/UserService.kt index 5582c6b..8123df1 100644 --- a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/user/UserService.kt +++ b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/user/UserService.kt @@ -45,7 +45,7 @@ class UserService( } @Transactional - fun hardDelete(id: Long) { + fun hardDeleteById(id: Long) { val userEntity = loadById(id).fromDto() userRepository.delete(userEntity) }