|
1 | 1 | package io.ticketaka.api.point.application
|
2 | 2 |
|
| 3 | +import io.ticketaka.api.common.exception.NotFoundException |
3 | 4 | import io.ticketaka.api.point.application.dto.BalanceQueryModel
|
4 | 5 | import io.ticketaka.api.point.application.dto.RechargeCommand
|
5 |
| -import io.ticketaka.api.point.domain.PointBalanceUpdater |
| 6 | +import io.ticketaka.api.point.domain.PointBalanceCacheUpdater |
6 | 7 | import io.ticketaka.api.point.domain.PointRechargeEvent
|
| 8 | +import io.ticketaka.api.point.domain.PointRepository |
7 | 9 | import io.ticketaka.api.user.application.TokenUserQueryService
|
8 | 10 | import org.springframework.context.ApplicationEventPublisher
|
9 |
| -import org.springframework.retry.annotation.Backoff |
10 |
| -import org.springframework.retry.annotation.Retryable |
11 |
| -import org.springframework.scheduling.annotation.Async |
12 | 11 | import org.springframework.stereotype.Service
|
13 | 12 | import org.springframework.transaction.annotation.Transactional
|
14 | 13 |
|
15 | 14 | @Service
|
16 |
| -@Transactional(readOnly = true) |
17 | 15 | class PointService(
|
18 | 16 | private val tokenUserQueryService: TokenUserQueryService,
|
19 | 17 | private val pointQueryService: PointQueryService,
|
20 |
| - private val pointBalanceUpdater: PointBalanceUpdater, |
| 18 | + private val pointBalanceCacheUpdater: PointBalanceCacheUpdater, |
21 | 19 | private val applicationEventPublisher: ApplicationEventPublisher,
|
| 20 | + private val pointRepository: PointRepository, |
22 | 21 | ) {
|
23 |
| - @Async |
24 |
| - @Retryable(retryFor = [Exception::class], backoff = Backoff(delay = 1000, multiplier = 2.0, maxDelay = 10000)) |
25 | 22 | @Transactional
|
26 | 23 | fun recharge(rechargeCommand: RechargeCommand) {
|
27 | 24 | val user = tokenUserQueryService.getUser(rechargeCommand.userId)
|
28 |
| - val userPoint = pointQueryService.getPoint(user.pointId) |
29 |
| - pointBalanceUpdater.recharge(userPoint, rechargeCommand.amount) |
30 |
| - applicationEventPublisher.publishEvent(PointRechargeEvent(user.id, userPoint.id, rechargeCommand.amount)) |
| 25 | + val point = pointQueryService.getPoint(user.pointId) |
| 26 | + pointBalanceCacheUpdater.recharge(point.id, rechargeCommand.amount) |
| 27 | + applicationEventPublisher.publishEvent(PointRechargeEvent(user.id, point.id, rechargeCommand.amount)) |
31 | 28 | }
|
32 | 29 |
|
33 | 30 | fun getBalance(userId: Long): BalanceQueryModel {
|
34 | 31 | val user = tokenUserQueryService.getUser(userId)
|
35 | 32 | val point = pointQueryService.getPoint(user.pointId)
|
36 | 33 | return BalanceQueryModel(user.id, point.balance)
|
37 | 34 | }
|
| 35 | + |
| 36 | + @Transactional |
| 37 | + fun updateRecharge(event: PointRechargeEvent) { |
| 38 | + val point = pointRepository.findById(event.pointId) ?: throw NotFoundException("포인트를 찾을 수 없습니다.") |
| 39 | + point.recharge(event.amount) |
| 40 | + pointRepository.updateBalance(point.id, point.balance) |
| 41 | + } |
38 | 42 | }
|
0 commit comments