-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from f-lab-edu/feature/46
[#46] 경매 상품 결제(포인트) 기능 추가
- Loading branch information
Showing
19 changed files
with
345 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
module-api/src/main/java/com/example/moduleapi/controller/point/PointController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.example.moduleapi.controller.point; | ||
|
||
import com.example.moduleapi.controller.request.point.PointAmount; | ||
import com.example.moduleapi.controller.response.point.PointResponse; | ||
import com.example.moduleapi.service.point.PointService; | ||
import com.example.moduledomain.domain.user.CustomUserDetails; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/point") | ||
public class PointController { | ||
|
||
private final PointService pointService; | ||
|
||
public PointController(PointService pointService) { | ||
this.pointService = pointService; | ||
} | ||
|
||
// 포인트 충전 | ||
@PostMapping("/charge") | ||
public PointResponse chargePoint( | ||
@AuthenticationPrincipal CustomUserDetails customUserDetails, | ||
@RequestBody PointAmount pointAmount) { | ||
int totalPoint = pointService.chargePoint(customUserDetails, pointAmount); | ||
PointResponse pointResponse = PointResponse.builder() | ||
.currentPoint(totalPoint) | ||
.message("포인트가 성공적으로 충전되었습니다.") | ||
.build(); | ||
return pointResponse; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
module-api/src/main/java/com/example/moduleapi/controller/request/point/PointAmount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.example.moduleapi.controller.request.point; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class PointAmount { | ||
private int amount; | ||
|
||
public PointAmount(int amount) { | ||
this.amount = amount; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
module-api/src/main/java/com/example/moduleapi/controller/response/point/PointResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.example.moduleapi.controller.response.point; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class PointResponse { | ||
private int currentPoint; | ||
private String message; | ||
|
||
@Builder | ||
public PointResponse(int currentPoint, String message) { | ||
this.currentPoint = currentPoint; | ||
this.message = message; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...pi/src/main/java/com/example/moduleapi/exception/point/PointDeductionFailedException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.example.moduleapi.exception.point; | ||
|
||
public class PointDeductionFailedException extends RuntimeException { | ||
public PointDeductionFailedException(Long userId) { | ||
super(userId + ": 포인트가 부족합니다."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
module-api/src/main/java/com/example/moduleapi/service/point/PointService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.example.moduleapi.service.point; | ||
|
||
import com.example.moduleapi.controller.request.point.PointAmount; | ||
import com.example.moduleapi.exception.point.PointDeductionFailedException; | ||
import com.example.moduledomain.domain.point.Point; | ||
import com.example.moduledomain.domain.user.CustomUserDetails; | ||
import com.example.moduledomain.domain.user.User; | ||
import com.example.moduledomain.repository.user.PointRepository; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
public class PointService { | ||
private final PointRepository pointRepository; | ||
|
||
public PointService(PointRepository pointRepository) { | ||
this.pointRepository = pointRepository; | ||
} | ||
|
||
// 포인트 충전 | ||
@Transactional | ||
public int chargePoint(CustomUserDetails userDetails, PointAmount pointAmount) { | ||
User user = userDetails.getUser(); | ||
Point point = pointRepository.findByUserId(user.getId()); | ||
point.plus(pointAmount.getAmount()); | ||
return point.getAmount(); | ||
} | ||
|
||
// 포인트 차감 | ||
@Transactional | ||
public int deductPoint(CustomUserDetails userDetails, PointAmount pointAmount) { | ||
User user = userDetails.getUser(); | ||
Point point = pointRepository.findByUserId(user.getId()); | ||
|
||
validateDeductPoint(pointAmount, user, point); | ||
|
||
point.minus(pointAmount.getAmount()); | ||
return point.getAmount(); | ||
} | ||
|
||
// 포인트 롤백 | ||
@Transactional | ||
public void rollbackPoint(Long userId, int plusAmount) { | ||
Point point = pointRepository.findByUserId(userId); | ||
point.plus(plusAmount); | ||
} | ||
|
||
private static void validateDeductPoint(PointAmount pointAmount, User user, Point point) { | ||
if (point.getAmount() < pointAmount.getAmount()) { | ||
throw new PointDeductionFailedException(user.getId()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
module-api/src/test/groovy/com/example/moduleapi/fixture/point/PointFixtures.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.example.moduleapi.fixture.point | ||
|
||
import com.example.moduledomain.domain.point.Point | ||
|
||
class PointFixtures { | ||
static Point createPoint(Map map = [:]) { | ||
return Point.builder() | ||
.userId(map.getOrDefault("userId", 1L) as Long) | ||
.amount(map.getOrDefault("amount", 0) as int) | ||
.build() | ||
} | ||
|
||
} | ||
|
2 changes: 1 addition & 1 deletion
2
.../fixture/UserFixtures/UserFixtures.groovy → ...oduleapi/fixture/user/UserFixtures.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
module-api/src/test/groovy/com/example/moduleapi/service/file/LocalFileServiceTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.