Skip to content

Commit

Permalink
Modify User Delete
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Diger committed Dec 29, 2023
1 parent 22df797 commit b2a50d8
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class GoalApplicationService(
isNotUsersGoal(goal, userId)
isAlreadyStarted(goal)

goalService.delete(
goalService.softDelete(
goal = goal,
userEntity = userService.loadById(userId).fromDto(),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
package com.whatever.raisedragon.applicationservice

import com.whatever.raisedragon.common.exception.BaseException
import com.whatever.raisedragon.common.exception.ExceptionCode
import com.whatever.raisedragon.controller.user.UserNicknameDuplicatedResponse
import com.whatever.raisedragon.controller.user.UserRetrieveResponse
import com.whatever.raisedragon.domain.betting.BettingService
import com.whatever.raisedragon.domain.gifticon.GifticonService
import com.whatever.raisedragon.domain.goal.GoalService
import com.whatever.raisedragon.domain.goalproof.GoalProofService
import com.whatever.raisedragon.domain.refreshtoken.RefreshTokenService
import com.whatever.raisedragon.domain.user.UserService
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional(readOnly = true)
class UserApplicationService(
private val userService: UserService
private val userService: UserService,
private val refreshTokenService: RefreshTokenService,
private val goalService: GoalService,
private val goalProofService: GoalProofService,
private val gifticonService: GifticonService,
private val bettingService: BettingService,
) {

fun retrieve(id: Long): UserRetrieveResponse {
Expand All @@ -31,8 +43,21 @@ class UserApplicationService(
)
}

@Transactional
fun delete(id: Long) {
userService.softDelete(id)
val user = userService.loadById(id)
if (goalService.findProceedingGoalIsExistsByUser(user)) {
throw BaseException.of(
exceptionCode = ExceptionCode.E400_BAD_REQUEST,
executionMessage = "아직 진행중인 다짐이 있어 회원탈퇴에 실패했습니다."
)
}
userService.hardDelete(id)
refreshTokenService.hardDelete(user)
goalService.hardDelete(user)
goalProofService.hardDelete(user)
gifticonService.hardDelete(user)
bettingService.hardDelete(user)
}

fun isNicknameDuplicated(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.whatever.raisedragon.domain.betting

import com.whatever.raisedragon.domain.goal.GoalRepository
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
Expand Down Expand Up @@ -97,4 +99,10 @@ class BettingService(
?: throw IllegalStateException("Cannot find betting $bettingId")
betting.deletedAt = LocalDateTime.now()
}

@Transactional
fun hardDelete(user: User) {
val bettings = bettingRepository.findAllByUserEntity(user.fromDto())
bettingRepository.deleteAll(bettings)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.whatever.raisedragon.domain.gifticon

import com.whatever.raisedragon.domain.user.UserEntity
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository

@Repository
interface GifticonRepository : JpaRepository<GifticonEntity, Long>
interface GifticonRepository : JpaRepository<GifticonEntity, Long> {
fun findAllByUserEntity(userEntity: UserEntity): List<GifticonEntity>
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
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.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Transactional
@Transactional(readOnly = true)
@Service
class GifticonService(
private val gifticonRepository: GifticonRepository,
private val userRepository: UserRepository
) {

@Transactional
fun create(
userId: Long,
url: String,
Expand All @@ -27,4 +30,10 @@ class GifticonService(
fun loadById(gifticonId: Long): Gifticon {
return gifticonRepository.findById(gifticonId).get().toDto()
}

@Transactional
fun hardDelete(user: User) {
val gifticons = gifticonRepository.findAllByUserEntity(user.fromDto())
gifticonRepository.deleteAll(gifticons)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ interface GoalRepository : JpaRepository<GoalEntity, Long> {
fun findAllByEndDateLessThanEqualAndResultIs(endDate: LocalDateTime, result: Result): List<GoalEntity>

fun findAllByUserEntityAndResult(userEntity: UserEntity, result: Result): List<GoalEntity>
fun existsByUserEntityAndEndDateIsAfter(userEntity: UserEntity, now: LocalDateTime): Boolean
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.whatever.raisedragon.domain.goal

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
Expand Down Expand Up @@ -83,7 +85,7 @@ class GoalService(
}

@Transactional
fun delete(
fun softDelete(
goal: Goal,
userEntity: UserEntity,
): Goal {
Expand All @@ -93,8 +95,19 @@ class GoalService(
return goalEntity.toDto()
}

@Transactional
fun hardDelete(user: User) {
val goals = goalRepository.findAllByUserEntity(user.fromDto())
goalRepository.deleteAll(goals)
}

@Transactional
fun increaseThreshold(goal: Goal, userEntity: UserEntity) {
val goalEntity = goal.fromDto(userEntity)
goalEntity.threshold = Threshold(goalEntity.threshold.value + 1)
}

fun findProceedingGoalIsExistsByUser(user: User): Boolean {
return goalRepository.existsByUserEntityAndEndDateIsAfter(user.fromDto(), LocalDateTime.now())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ import org.springframework.stereotype.Repository
interface GoalProofRepository : JpaRepository<GoalProofEntity, Long> {
fun findAllByUserEntityAndGoalEntity(userEntity: UserEntity, goalEntity: GoalEntity): List<GoalProofEntity>

fun findAllByUserEntity(userEntity: UserEntity): List<GoalProofEntity>

fun countAllByGoalEntity(goalEntity: GoalEntity): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,10 @@ class GoalProofService(
comment?.let { goalProof.comment = it }
return goalProof.toDto()
}

@Transactional
fun hardDelete(user: User) {
val goalProofs = goalProofRepository.findAllByUserEntity(user.fromDto())
goalProofRepository.deleteAll(goalProofs)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ class RefreshTokenService(
fun updatePayloadByVo(refreshToken: RefreshToken, userEntity: UserEntity) {
val refreshTokenEntity = refreshToken.fromDto(userEntity)
}

@Transactional
fun hardDelete(user: User) {
val refreshTokenEntity = refreshTokenRepository.findByUserEntity(user.fromDto())!!
refreshTokenRepository.delete(refreshTokenEntity)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class UserService(
userEntity.disable()
}

@Transactional
fun hardDelete(id: Long) {
val userEntity = loadById(id).fromDto()
userRepository.delete(userEntity)
}

@Transactional
fun convertBySoftDeleteToEntity(id: Long) {
val userEntity = loadById(id).fromDto()
Expand Down

0 comments on commit b2a50d8

Please sign in to comment.