Skip to content

Commit b286603

Browse files
committed
feat: #140 루트 구매 API 포인트로 구매 시, 보유 포인트가 부족한 경우에 대한 예외처리 추가
1 parent b89c2cc commit b286603

6 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/main/kotlin/com/routebox/routebox/application/route/PurchaseRouteUseCase.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.routebox.routebox.domain.user_point_history.UserPointHistory
1313
import com.routebox.routebox.domain.user_point_history.UserPointHistoryService
1414
import com.routebox.routebox.exception.coupon.NoAvailableCouponException
1515
import com.routebox.routebox.exception.route.RouteNotFoundException
16+
import com.routebox.routebox.exception.user.InsufficientPointException
1617
import com.routebox.routebox.exception.user.UserNotFoundException
1718
import org.springframework.stereotype.Component
1819
import org.springframework.transaction.annotation.Transactional
@@ -33,6 +34,7 @@ class PurchaseRouteUseCase(
3334
* @throws NoAvailableCouponException (쿠폰으로 구매 시) 이용 가능한 쿠폰이 없는 경우
3435
* @throws UserNotFoundException `buyerId`에 해당하는 유저가 없는 경우
3536
* @throws RouteNotFoundException `routeId`에 해당하는, 구매할 루트 정보가 없는 경우
37+
* @throws InsufficientPointException (포인트로 구매 시) 루트를 구매하기에 포인트가 부족한 경우
3638
*/
3739
@Transactional
3840
operator fun invoke(command: PurchaseRouteCommand) {

src/main/kotlin/com/routebox/routebox/controller/route/RouteController.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ class RouteController(
105105
)
106106
@ApiResponses(
107107
ApiResponse(responseCode = "200"),
108-
ApiResponse(responseCode = "400", description = "[3003] (쿠폰으로 구매 시) 이용 가능한 쿠폰이 없는 경우", content = [Content()]),
108+
ApiResponse(
109+
responseCode = "400",
110+
description = "<p>[3003] (쿠폰으로 구매 시) 이용 가능한 쿠폰이 없는 경우" +
111+
"<p>[3005] (포인트로 구매 시) 루트를 구매하기에 포인트가 부족한 경우",
112+
content = [Content()],
113+
),
109114
)
110115
@PostMapping("/v1/routes/{routeId}/purchase")
111116
fun purchaseRoute(

src/main/kotlin/com/routebox/routebox/domain/user/User.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,6 @@ class User(
124124
fun rejoin() {
125125
this.deletedAt = LocalDateTime.now()
126126
}
127+
128+
fun canPurchase(point: Int): Boolean = this.point >= point
127129
}

src/main/kotlin/com/routebox/routebox/domain/user/UserService.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.routebox.routebox.domain.user
33
import com.routebox.routebox.domain.common.FileManager
44
import com.routebox.routebox.domain.user.constant.Gender
55
import com.routebox.routebox.domain.user.constant.LoginType
6+
import com.routebox.routebox.exception.user.InsufficientPointException
67
import com.routebox.routebox.exception.user.UserNicknameDuplicationException
78
import com.routebox.routebox.exception.user.UserNotFoundException
89
import com.routebox.routebox.exception.user.UserSocialLoginUidDuplicationException
@@ -139,8 +140,8 @@ class UserService(
139140
@Transactional
140141
fun usePoint(userId: Long, point: Int): User {
141142
val user = getUserById(userId)
142-
if (user.point < point) {
143-
TODO("잔여 포인트가 부족할 경우 예외 발생")
143+
if (user.canPurchase(point)) {
144+
throw InsufficientPointException()
144145
}
145146
user.usePoint(point)
146147
return user

src/main/kotlin/com/routebox/routebox/exception/CustomExceptionType.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ enum class CustomExceptionType(
2727
USER_NICKNAME_DUPLICATION(3002, "이미 사용중인 닉네임입니다."),
2828
NO_AVAILABLE_COUPON(3003, "이용 가능한 쿠폰이 없습니다."),
2929
USER_WITHDRAWN(3004, "탈퇴한 유저입니다."),
30+
INSUFFICIENT_POINT(3005, "포인트가 부족합니다."),
3031

3132
/**
3233
* 루트 관련 예외
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.routebox.routebox.exception.user
2+
3+
import com.routebox.routebox.exception.CustomExceptionType
4+
import com.routebox.routebox.exception.common.BadRequestException
5+
6+
class InsufficientPointException : BadRequestException(exceptionType = CustomExceptionType.INSUFFICIENT_POINT)

0 commit comments

Comments
 (0)