Skip to content

Commit 819aa8b

Browse files
authored
아키텍처 점검 (#24)
* Refactor: write-behind로 포인트 관리를 수행하는 코드는 캐시와 사용자 인터페이스 사이의 중재역할을 수행하고 있으므로 application 계층으로 이동 * Fix: 각 엔티티 nullable 하지않은 생성시간 필드에 현재시점의 시간으로 기본값 지정 * Refactor: 이벤트 드리븐 고도화시 발생하는 결합도를 줄이기위해, EventBroker와 EventProducer 분리
1 parent c2f3376 commit 819aa8b

File tree

18 files changed

+110
-117
lines changed

18 files changed

+110
-117
lines changed

src/main/kotlin/io/ticketaka/api/common/domain/EventBroker.kt

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/main/kotlin/io/ticketaka/api/common/infrastructure/event/DBEventBroker.kt

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/main/kotlin/io/ticketaka/api/common/infrastructure/event/EventDispatcher.kt renamed to src/main/kotlin/io/ticketaka/api/common/infrastructure/event/EventBroker.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import io.ticketaka.api.reservation.infrastructure.event.ReservationCreateEventQ
1010
import org.springframework.stereotype.Component
1111

1212
@Component
13-
class EventDispatcher(
13+
class EventBroker(
1414
private val pointRechargeEventQueue: PointRechargeEventQueue,
1515
private val pointChargeEventQueue: PointChargeEventQueue,
1616
private val reservationCreateEventQueue: ReservationCreateEventQueue,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.ticketaka.api.common.infrastructure.event
2+
3+
import io.ticketaka.api.common.domain.DomainEvent
4+
import org.springframework.stereotype.Component
5+
6+
@Component
7+
class EventProducer(
8+
private val eventBroker: EventBroker,
9+
) {
10+
fun produce(domainEvent: DomainEvent) {
11+
eventBroker.dispatch(domainEvent)
12+
}
13+
}

src/main/kotlin/io/ticketaka/api/concert/domain/Concert.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Concert(
2525
private set
2626

2727
@Column(nullable = false)
28-
var updatedAt: LocalDateTime? = null
28+
var updatedAt: LocalDateTime? = LocalDateTime.now()
2929
private set
3030

3131
@PreUpdate
@@ -36,13 +36,9 @@ class Concert(
3636
@Transient
3737
private var isNew = true
3838

39-
override fun isNew(): Boolean {
40-
return isNew
41-
}
39+
override fun isNew(): Boolean = isNew
4240

43-
override fun getId(): Long {
44-
return id
45-
}
41+
override fun getId(): Long = id
4642

4743
@PrePersist
4844
@PostLoad
@@ -51,11 +47,10 @@ class Concert(
5147
}
5248

5349
companion object {
54-
fun newInstance(date: LocalDate): Concert {
55-
return Concert(
50+
fun newInstance(date: LocalDate): Concert =
51+
Concert(
5652
id = TsIdKeyGenerator.nextLong(),
5753
date = date,
5854
)
59-
}
6055
}
6156
}

src/main/kotlin/io/ticketaka/api/concert/domain/Seat.kt

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Seat(
3434
private set
3535

3636
@Column(nullable = false)
37-
var updatedAt: LocalDateTime? = null
37+
var updatedAt: LocalDateTime? = LocalDateTime.now()
3838
private set
3939

4040
@PreUpdate
@@ -45,23 +45,17 @@ class Seat(
4545
@Transient
4646
private var isNew = true
4747

48-
override fun isNew(): Boolean {
49-
return isNew
50-
}
48+
override fun isNew(): Boolean = isNew
5149

52-
override fun getId(): Long {
53-
return id
54-
}
50+
override fun getId(): Long = id
5551

5652
@PrePersist
5753
@PostLoad
5854
fun markNotNew() {
5955
isNew = false
6056
}
6157

62-
fun isAvailable(): Boolean {
63-
return this.status == Status.AVAILABLE
64-
}
58+
fun isAvailable(): Boolean = this.status == Status.AVAILABLE
6559

6660
fun available() {
6761
this.status = Status.AVAILABLE
@@ -96,15 +90,14 @@ class Seat(
9690
number: String,
9791
price: BigDecimal,
9892
concertId: Long,
99-
): Seat {
100-
return Seat(
93+
): Seat =
94+
Seat(
10195
id = TsIdKeyGenerator.nextLong(),
10296
number = number,
10397
status = Status.AVAILABLE,
10498
price = price,
10599
concertId = concertId,
106100
concertDate = LocalDate.now(),
107101
)
108-
}
109102
}
110103
}

src/main/kotlin/io/ticketaka/api/point/domain/CachePointRecharger.kt renamed to src/main/kotlin/io/ticketaka/api/point/application/CacheWriteBehindPointService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package io.ticketaka.api.point.domain
1+
package io.ticketaka.api.point.application
22

33
import java.math.BigDecimal
44

5-
interface CachePointRecharger {
5+
interface CacheWriteBehindPointService {
66
fun recharge(
77
pointId: Long,
88
amount: BigDecimal,

src/main/kotlin/io/ticketaka/api/point/application/PointService.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
package io.ticketaka.api.point.application
22

3-
import io.ticketaka.api.common.domain.EventBroker
43
import io.ticketaka.api.point.application.dto.BalanceQueryModel
54
import io.ticketaka.api.point.application.dto.RechargeCommand
6-
import io.ticketaka.api.point.domain.CachePointRecharger
75
import io.ticketaka.api.point.domain.PointRechargeEvent
86
import io.ticketaka.api.user.application.QueueTokenUserCacheAsideQueryService
7+
import org.springframework.context.ApplicationEventPublisher
98
import org.springframework.stereotype.Service
109

1110
@Service
1211
class PointService(
1312
private val queueTokenUserCacheAsideQueryService: QueueTokenUserCacheAsideQueryService,
1413
private val pointCacheAsideQueryService: PointCacheAsideQueryService,
15-
private val cachePointRecharger: CachePointRecharger,
16-
private val eventBroker: EventBroker,
14+
private val cacheWriteBehindPointService: CacheWriteBehindPointService,
15+
private val applicationEventPublisher: ApplicationEventPublisher,
1716
) {
1817
fun recharge(rechargeCommand: RechargeCommand) {
1918
val user = queueTokenUserCacheAsideQueryService.getUser(rechargeCommand.userId)
2019
val point = pointCacheAsideQueryService.getPoint(user.pointId)
21-
cachePointRecharger.recharge(point.id, rechargeCommand.amount)
22-
eventBroker.produce(PointRechargeEvent(user.id, point.id, rechargeCommand.amount))
20+
cacheWriteBehindPointService.recharge(point.id, rechargeCommand.amount)
21+
applicationEventPublisher.publishEvent(PointRechargeEvent(user.id, point.id, rechargeCommand.amount))
2322
}
2423

2524
fun getBalance(userId: Long): BalanceQueryModel {

src/main/kotlin/io/ticketaka/api/point/domain/Idempotent.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Idempotent(
2626
private set
2727

2828
@Column(nullable = false)
29-
var updatedAt: LocalDateTime? = null
29+
var updatedAt: LocalDateTime? = LocalDateTime.now()
3030
private set
3131

3232
@PreUpdate
@@ -37,13 +37,9 @@ class Idempotent(
3737
@Transient
3838
private var isNew = true
3939

40-
override fun isNew(): Boolean {
41-
return isNew
42-
}
40+
override fun isNew(): Boolean = isNew
4341

44-
override fun getId(): Long {
45-
return id
46-
}
42+
override fun getId(): Long = id
4743

4844
@PrePersist
4945
@PostLoad
@@ -57,8 +53,7 @@ class Idempotent(
5753
tokenId: Long,
5854
amount: BigDecimal,
5955
transactionType: PointHistory.TransactionType,
60-
): Idempotent {
61-
return Idempotent(TsIdKeyGenerator.nextLong(), IdempotentKeyGenerator.generate(userId, tokenId, amount, transactionType.name))
62-
}
56+
): Idempotent =
57+
Idempotent(TsIdKeyGenerator.nextLong(), IdempotentKeyGenerator.generate(userId, tokenId, amount, transactionType.name))
6358
}
6459
}

src/main/kotlin/io/ticketaka/api/point/domain/payment/Payment.kt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ class Payment(
2323
val paymentTime: LocalDateTime,
2424
val userId: Long,
2525
val pointId: Long,
26-
) : AbstractAggregateRoot(), Persistable<Long> {
26+
) : AbstractAggregateRoot(),
27+
Persistable<Long> {
2728
@Column(nullable = false, updatable = false)
2829
var createdAt: LocalDateTime? = LocalDateTime.now()
2930
private set
3031

3132
@Column(nullable = false)
32-
var updatedAt: LocalDateTime? = null
33+
var updatedAt: LocalDateTime? = LocalDateTime.now()
3334
private set
3435

3536
@PreUpdate
@@ -40,13 +41,9 @@ class Payment(
4041
@Transient
4142
private var isNew = true
4243

43-
override fun isNew(): Boolean {
44-
return isNew
45-
}
44+
override fun isNew(): Boolean = isNew
4645

47-
override fun getId(): Long {
48-
return id
49-
}
46+
override fun getId(): Long = id
5047

5148
@PrePersist
5249
@PostLoad
@@ -63,14 +60,13 @@ class Payment(
6360
amount: BigDecimal,
6461
userId: Long,
6562
pointId: Long,
66-
): Payment {
67-
return Payment(
63+
): Payment =
64+
Payment(
6865
id = TsIdKeyGenerator.nextLong(),
6966
amount = amount,
7067
userId = userId,
7168
paymentTime = LocalDateTime.now(),
7269
pointId = pointId,
7370
)
74-
}
7571
}
7672
}

0 commit comments

Comments
 (0)