Skip to content

Commit

Permalink
Merge pull request #82 from whatever-mentoring/feature/user-service-test
Browse files Browse the repository at this point in the history
Write test codes of UserService
  • Loading branch information
mkSpace authored Jan 24, 2024
2 parents f4cc2e3 + 5272fe6 commit 35b66d6
Show file tree
Hide file tree
Showing 7 changed files with 359 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ class AuthApplicationService(
@Transactional
fun kakaoLogin(request: LoginServiceRequest): LoginResponse {
val kakaoId = authService.verifyKaKao(request.accessToken)
val user = userService.loadByOAuthPayload(kakaoId)
val user = userService.findByOAuthPayload(kakaoId)

if (user == null) {
val newUser = userService.create(
User(
oauthTokenPayload = kakaoId,
fcmTokenPayload = null,
nickname = Nickname.generateRandomNickname()
)
oauthTokenPayload = kakaoId,
fcmTokenPayload = null,
nickname = Nickname.generateRandomNickname()
)
return buildLoginResponseByNewUser(newUser)
}
Expand Down Expand Up @@ -90,7 +88,7 @@ class AuthApplicationService(
)

val userId = refreshTokenVo.userId
val user = userService.loadById(userId)
val user = userService.findById(userId)
val jwtToken = jwtAgent.reissueToken(refreshToken, user)

refreshTokenVo.payload = jwtToken.refreshToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ class GoalApplicationService(
gifticonId = gifticon.id
)
}
val hostUser = userService.loadById(request.userId)
val hostUser = userService.findById(request.userId)
return GoalResponse.of(goal, hostUser.nickname.value)
}

fun retrieveGoal(goalId: Long): GoalResponse {
val goal = goalService.findById(goalId)
val hostUser = userService.loadById(goal.userId)
val hostUser = userService.findById(goal.userId)
return GoalResponse.of(goal, hostUser.nickname.value)
}

fun retrieveGoalDetail(goalId: Long, userId: Long): GoalWithBettingResponse {
val goal = goalService.findById(goalId)
val hostUser = userService.loadById(goal.userId)
val hostUser = userService.findById(goal.userId)
val betting = bettingService.loadUserAndGoal(userId, goalId)
val goalProofs = goalProofService.findAllByGoalIdAndUserId(goalId, userId)
val winnerNickname = winnerService.findWinnerNicknameByGoalId(goalId)?.value
Expand Down Expand Up @@ -94,7 +94,7 @@ class GoalApplicationService(
goalId: Long
): GoalRetrieveParticipantResponse {
val goal = goalService.findById(goalId)
val hostUser = userService.loadById(goal.userId)
val hostUser = userService.findById(goal.userId)
val bettingList = bettingService.findAllByGoalId(goalId)

val hostDto = GoalBettingHostResponse(
Expand All @@ -106,7 +106,7 @@ class GoalApplicationService(
val participants = bettingList.map {
GoalBettingParticipantResponse(
userId = it.userId,
nickname = userService.loadById(it.userId).nickname.value,
nickname = userService.findById(it.userId).nickname.value,
bettingId = it.id,
bettingPredictionType = it.bettingPredictionType,
bettingResult = it.bettingResult,
Expand All @@ -124,7 +124,7 @@ class GoalApplicationService(
goalId: Long
): GoalRetrieveParticipantResponse {
val goal = goalService.findById(goalId)
val goalHostUser = userService.loadById(goal.userId)
val goalHostUser = userService.findById(goal.userId)
val bettingList = bettingService.findAllByGoalId(goalId)

val hostDto = GoalBettingHostResponse(
Expand All @@ -136,7 +136,7 @@ class GoalApplicationService(
val participants = bettingList.map {
GoalBettingParticipantResponse(
userId = it.userId,
nickname = userService.loadById(it.userId).nickname.value,
nickname = userService.findById(it.userId).nickname.value,
bettingId = it.id,
bettingPredictionType = it.bettingPredictionType,
bettingResult = it.bettingResult,
Expand Down Expand Up @@ -185,7 +185,7 @@ class GoalApplicationService(
content = request.content
)

val hostUser = userService.loadById(request.userId)
val hostUser = userService.findById(request.userId)
return GoalResponse.of(modifiedGoal, hostUser.nickname.value)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class GoalGifticonApplicationService(

@Transactional
fun updateGifticonURLByGoalId(request: GoalGifticonUpdateServiceRequest): GoalGifticonResponse {
val userEntity = userService.loadById(request.userId).fromDto()
val userEntity = userService.findById(request.userId).fromDto()
val goal = goalService.findById(request.goalId).fromDto(userEntity).toDto()
val goalGifticon = goalGifticonService.findByGoalId(goal.id)
?: throw BaseException.of(ExceptionCode.E404_NOT_FOUND, "다짐에 등록된 기프티콘을 찾을 수 없습니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class UserApplicationService(
) {

fun retrieve(id: Long): UserRetrieveResponse {
val user = userService.loadById(id)
val user = userService.findById(id)
return UserRetrieveResponse(
userId = user.id!!,
nickname = user.nickname,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TestController(
@Operation(description = "테스트 토큰 발급")
@GetMapping("/token/{userId}")
fun getToken(@PathVariable userId: Long): Response<String> {
return Response.success(jwtAgent.provide(userService.loadById(userId)).accessToken)
return Response.success(jwtAgent.provide(userService.findById(userId)).accessToken)
}

@Operation(description = "단일 다짐에 대한 결과 확정")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,35 @@ class UserService(
private val userRepository: UserRepository,
) {

fun loadByOAuthPayload(payload: String): User? {
return userRepository.findByOauthTokenPayload(payload)?.toDto()
@Transactional
fun create(
oauthTokenPayload: String?,
fcmTokenPayload: String?,
nickname: Nickname,
): User {
return userRepository.save(
UserEntity(
oauthTokenPayload = oauthTokenPayload,
fcmTokenPayload = fcmTokenPayload,
nickname = nickname
)
).toDto()
}

fun loadById(id: Long): User {
fun findById(id: Long): User {
return userRepository.findById(id).orElseThrow(notFoundExceptionSupplier).toDto()
}

fun findAllByIdInIds(ids: Set<Long>): List<User> {
return userRepository.findAllById(ids).map { it.toDto() }
}

fun isNicknameDuplicated(nickname: String): Boolean {
return userRepository.existsByNickname(Nickname(nickname))
fun findByOAuthPayload(payload: String): User? {
return userRepository.findByOauthTokenPayload(payload)?.toDto()
}

@Transactional
fun create(user: User): User {
return userRepository.save(user.fromDto()).toDto()
fun isNicknameDuplicated(nickname: String): Boolean {
return userRepository.existsByNickname(Nickname(nickname))
}

@Transactional
Expand All @@ -42,20 +52,14 @@ class UserService(
}

@Transactional
fun softDelete(id: Long) {
val userEntity = loadById(id).fromDto()
userEntity.disable()
fun convertBySoftDeleteToEntity(id: Long) {
val userEntity = userRepository.findById(id).orElseThrow(notFoundExceptionSupplier)
userEntity.able()
}

@Transactional
fun hardDeleteById(id: Long) {
val userEntity = loadById(id).fromDto()
val userEntity = userRepository.findById(id).orElseThrow(notFoundExceptionSupplier)
userRepository.delete(userEntity)
}

@Transactional
fun convertBySoftDeleteToEntity(id: Long) {
val userEntity = loadById(id).fromDto()
userEntity.able()
}
}
Loading

0 comments on commit 35b66d6

Please sign in to comment.