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

Write test code of repositories #74

Merged
merged 12 commits into from
Jan 8, 2024
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.whatever.raisedragon.applicationservice
package com.whatever.raisedragon.applicationservice.auth

import com.whatever.raisedragon.applicationservice.auth.dto.LoginResponse
import com.whatever.raisedragon.applicationservice.auth.dto.LoginServiceRequest
import com.whatever.raisedragon.applicationservice.auth.dto.TokenRefreshResponse
import com.whatever.raisedragon.common.exception.BaseException
import com.whatever.raisedragon.common.exception.ExceptionCode
import com.whatever.raisedragon.controller.auth.LoginResponse
import com.whatever.raisedragon.controller.auth.TokenRefreshResponse
import com.whatever.raisedragon.domain.auth.AuthService
import com.whatever.raisedragon.domain.refreshtoken.RefreshToken
import com.whatever.raisedragon.domain.refreshtoken.RefreshTokenService
Expand All @@ -26,8 +27,8 @@ class AuthApplicationService(
) {

@Transactional
fun kakaoLogin(accessToken: String): LoginResponse {
val kakaoId = authService.verifyKaKao(accessToken)
fun kakaoLogin(request: LoginServiceRequest): LoginResponse {
val kakaoId = authService.verifyKaKao(request.accessToken)
val user = userService.loadByOAuthPayload(kakaoId)

if (user == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.whatever.raisedragon.applicationservice.auth.dto

import io.swagger.v3.oas.annotations.media.Schema

@Schema(description = "[Response] 카카오 로그인 응답")
data class LoginResponse(
@Schema(description = "id")
val userId: Long,

@Schema(description = "닉네임")
val nickname: String,

@Schema(description = "액세스 토큰")
val accessToken: String,

@Schema(description = "리프레시 토큰")
val refreshToken: String,

@Schema(description = "닉네임 변경 이력")
val nicknameIsModified: Boolean
)

@Schema(description = "[Response] 토큰 갱신 응답")
data class TokenRefreshResponse(
@Schema(description = "액세스 토큰")
val accessToken: String,

@Schema(description = "리프레시 토큰")
val refreshToken: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.whatever.raisedragon.applicationservice.auth.dto

data class LoginServiceRequest(
val accessToken: String
)
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.whatever.raisedragon.applicationservice
package com.whatever.raisedragon.applicationservice.betting

import com.whatever.raisedragon.applicationservice.betting.dto.BettingCreateServiceRequest
import com.whatever.raisedragon.applicationservice.betting.dto.BettingCreateUpdateResponse
import com.whatever.raisedragon.applicationservice.betting.dto.BettingRetrieveResponse
import com.whatever.raisedragon.applicationservice.betting.dto.BettingUpdateServiceRequest
import com.whatever.raisedragon.common.exception.BaseException
import com.whatever.raisedragon.common.exception.ExceptionCode
import com.whatever.raisedragon.controller.betting.BettingCreateUpdateResponse
import com.whatever.raisedragon.controller.betting.BettingRetrieveResponse
import com.whatever.raisedragon.domain.betting.Betting
import com.whatever.raisedragon.domain.betting.BettingService
import com.whatever.raisedragon.domain.betting.PredictionType
import com.whatever.raisedragon.domain.goal.GoalService
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -20,25 +21,21 @@ class BettingApplicationService(
) {

@Transactional
fun create(
userId: Long,
goalId: Long,
predictionType: PredictionType
): BettingCreateUpdateResponse {
if (canBet(userId, goalId)) {
fun create(request: BettingCreateServiceRequest): BettingCreateUpdateResponse {
if (canBet(request.userId, request.goalId)) {
val betting = bettingService.create(
userId = userId,
goalId = goalId,
predictionType = predictionType
userId = request.userId,
goalId = request.goalId,
bettingPredictionType = request.bettingPredictionType
)

return BettingCreateUpdateResponse(
BettingRetrieveResponse(
id = betting.id,
userId = betting.userId,
goalId = betting.goalId,
predictionType = betting.predictionType,
result = betting.result
bettingPredictionType = betting.bettingPredictionType,
bettingResult = betting.bettingResult
)
)
}
Expand All @@ -54,11 +51,11 @@ class BettingApplicationService(
}

@Transactional
fun update(userId: Long, bettingId: Long, predictionType: PredictionType): BettingRetrieveResponse {
val betting = findByIdOrThrowException(bettingId)
betting.validateOwnerId(userId)
fun update(request: BettingUpdateServiceRequest): BettingRetrieveResponse {
val betting = findByIdOrThrowException(request.bettingId)
betting.validateOwnerId(request.userId)
betting.validateStartDate()
return BettingRetrieveResponse.of(bettingService.update(bettingId, predictionType))
return BettingRetrieveResponse.of(bettingService.update(request.bettingId, request.bettingPredictionType))
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.whatever.raisedragon.applicationservice.betting.dto

import com.whatever.raisedragon.domain.betting.Betting
import com.whatever.raisedragon.domain.betting.BettingPredictionType
import com.whatever.raisedragon.domain.betting.BettingResult
import io.swagger.v3.oas.annotations.media.Schema

@Schema(description = "[Response] 배팅 생성/수정")
data class BettingCreateUpdateResponse(
@Schema(description = "Goal Id")
val bettingRetrieveResponse: BettingRetrieveResponse,
)

@Schema(description = "[Response] 배팅 조회")
data class BettingRetrieveResponse(
@Schema(description = "BettingId")
val id: Long,

@Schema(description = "UserId")
val userId: Long,

@Schema(description = "GoalId")
val goalId: Long,

@Schema(description = "예측")
val bettingPredictionType: BettingPredictionType,

@Schema(description = "당첨 여부")
val bettingResult: BettingResult,
) {
companion object {
fun of(betting: Betting): BettingRetrieveResponse = BettingRetrieveResponse(
id = betting.id,
userId = betting.userId,
goalId = betting.goalId,
bettingPredictionType = betting.bettingPredictionType,
bettingResult = betting.bettingResult
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.whatever.raisedragon.applicationservice.betting.dto

import com.whatever.raisedragon.domain.betting.BettingPredictionType

data class BettingCreateServiceRequest(
val userId: Long,
val goalId: Long,
val bettingPredictionType: BettingPredictionType
)

data class BettingUpdateServiceRequest(
val userId: Long,
val bettingId: Long,
val bettingPredictionType: BettingPredictionType
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.whatever.raisedragon.applicationservice
package com.whatever.raisedragon.applicationservice.goal

import com.whatever.raisedragon.applicationservice.goal.dto.*
import com.whatever.raisedragon.common.exception.BaseException
import com.whatever.raisedragon.common.exception.ExceptionCode
import com.whatever.raisedragon.controller.goal.*
import com.whatever.raisedragon.domain.betting.BettingService
import com.whatever.raisedragon.domain.gifticon.GifticonService
import com.whatever.raisedragon.domain.goal.*
Expand All @@ -28,39 +28,32 @@ class GoalApplicationService(
) {

@Transactional
fun createGoal(
content: Content,
bettingType: BettingType,
startDate: LocalDateTime,
endDate: LocalDateTime,
userId: Long,
gifticonUrl: String? = null
): GoalResponse {
if (isNumberOfGoalUnderOneHundred(userId)) throw BaseException.of(
fun createGoal(request: GoalCreateServiceRequest): GoalResponse {
if (isNumberOfGoalUnderOneHundred(request.userId)) throw BaseException.of(
exceptionCode = ExceptionCode.E409_CONFLICT,
executionMessage = "다짐을 생성하는 중, 생성할 수 있는 다짐 갯수를 초과하였습니다."
)
if (goalService.existsByUserIdAndAnyResult(userId, Result.PROCEEDING)) throw BaseException.of(
if (goalService.existsByUserIdAndAnyResult(request.userId, GoalResult.PROCEEDING)) throw BaseException.of(
exceptionCode = ExceptionCode.E409_CONFLICT,
executionMessage = "다짐을 생성하는 중, 이미 생성한 다짐이 있어 생성이 불가합니다."
)
val goal = goalService.create(
userId = userId,
content = content,
bettingType = bettingType,
userId = request.userId,
content = request.content,
goalType = request.goalType,
threshold = Threshold(0),
startDate = startDate,
endDate = endDate
startDate = request.startDate,
endDate = request.endDate
)
if (!gifticonUrl.isNullOrBlank() && bettingType == BettingType.BILLING) {
val gifticon = gifticonService.create(userId, gifticonUrl)
if (!request.gifticonUrl.isNullOrBlank() && request.goalType == GoalType.BILLING) {
val gifticon = gifticonService.create(request.userId, request.gifticonUrl)
goalGifticonService.create(
goalId = goal.id,
gifticonId = gifticon.id,
userId = userId
userId = request.userId
)
}
val hostUser = userService.loadById(userId)
val hostUser = userService.loadById(request.userId)
return GoalResponse.of(goal, hostUser.nickname.value)
}

Expand Down Expand Up @@ -106,19 +99,19 @@ class GoalApplicationService(
val hostUser = userService.loadById(goal.userId)
val bettingList = bettingService.loadAllByGoalId(goalId)

val hostDto = GoalBettingHost(
val hostDto = GoalBettingHostResponse(
id = hostUser.id!!,
nickname = hostUser.nickname.value,
goalCreatedAt = goal.createdAt
)

val participants = bettingList.map {
GoalBettingParticipant(
GoalBettingParticipantResponse(
userId = it.userId,
nickname = userService.loadById(it.userId).nickname.value,
bettingId = it.id,
predictionType = it.predictionType,
result = it.result,
bettingPredictionType = it.bettingPredictionType,
bettingResult = it.bettingResult,
bettingCreatedAt = it.createdAt!!
)
}
Expand All @@ -136,19 +129,19 @@ class GoalApplicationService(
val goalHostUser = userService.loadById(goal.userId)
val bettingList = bettingService.loadAllByGoalId(goalId)

val hostDto = GoalBettingHost(
val hostDto = GoalBettingHostResponse(
id = goalHostUser.id!!,
nickname = goalHostUser.nickname.value,
goalCreatedAt = goal.createdAt
)

val participants = bettingList.map {
GoalBettingParticipant(
GoalBettingParticipantResponse(
userId = it.userId,
nickname = userService.loadById(it.userId).nickname.value,
bettingId = it.id,
predictionType = it.predictionType,
result = it.result,
bettingPredictionType = it.bettingPredictionType,
bettingResult = it.bettingResult,
bettingCreatedAt = it.createdAt!!
)
}
Expand Down Expand Up @@ -184,37 +177,30 @@ class GoalApplicationService(
}

@Transactional
fun modifyGoal(
userId: Long,
goalId: Long,
content: String
): GoalResponse {
val goal = goalService.loadById(goalId)
isNotUsersGoal(goal, userId)
fun modifyGoal(request: GoalModifyServiceRequest): GoalResponse {
val goal = goalService.loadById(request.goalId)
isNotUsersGoal(goal, request.userId)
isAlreadyStarted(goal)

val modifiedGoal = goalService.modify(
goal = goal,
userEntity = userService.loadById(userId).fromDto(),
content = content
userEntity = userService.loadById(request.userId).fromDto(),
content = request.content
)

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

@Transactional
fun deleteGoal(
userId: Long,
goalId: Long,
) {
val goal = goalService.loadById(goalId)
isNotUsersGoal(goal, userId)
fun deleteGoal(request: GoalDeleteServiceRequest) {
val goal = goalService.loadById(request.goalId)
isNotUsersGoal(goal, request.userId)
isAlreadyStarted(goal)

goalService.softDelete(
goal = goal,
userEntity = userService.loadById(userId).fromDto(),
userEntity = userService.loadById(request.userId).fromDto(),
)
}

Expand Down
Loading