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

아키텍처 점검 #24

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/main/kotlin/io/ticketaka/api/common/domain/EventBroker.kt

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import io.ticketaka.api.reservation.infrastructure.event.ReservationCreateEventQ
import org.springframework.stereotype.Component

@Component
class EventDispatcher(
class EventBroker(
private val pointRechargeEventQueue: PointRechargeEventQueue,
private val pointChargeEventQueue: PointChargeEventQueue,
private val reservationCreateEventQueue: ReservationCreateEventQueue,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.ticketaka.api.common.infrastructure.event

import io.ticketaka.api.common.domain.DomainEvent
import org.springframework.stereotype.Component

@Component
class EventProducer(
private val eventBroker: EventBroker,
) {
fun produce(domainEvent: DomainEvent) {
eventBroker.dispatch(domainEvent)
}
}
15 changes: 5 additions & 10 deletions src/main/kotlin/io/ticketaka/api/concert/domain/Concert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Concert(
private set

@Column(nullable = false)
var updatedAt: LocalDateTime? = null
var updatedAt: LocalDateTime? = LocalDateTime.now()
private set

@PreUpdate
Expand All @@ -36,13 +36,9 @@ class Concert(
@Transient
private var isNew = true

override fun isNew(): Boolean {
return isNew
}
override fun isNew(): Boolean = isNew

override fun getId(): Long {
return id
}
override fun getId(): Long = id

@PrePersist
@PostLoad
Expand All @@ -51,11 +47,10 @@ class Concert(
}

companion object {
fun newInstance(date: LocalDate): Concert {
return Concert(
fun newInstance(date: LocalDate): Concert =
Concert(
id = TsIdKeyGenerator.nextLong(),
date = date,
)
}
}
}
19 changes: 6 additions & 13 deletions src/main/kotlin/io/ticketaka/api/concert/domain/Seat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Seat(
private set

@Column(nullable = false)
var updatedAt: LocalDateTime? = null
var updatedAt: LocalDateTime? = LocalDateTime.now()
private set

@PreUpdate
Expand All @@ -45,23 +45,17 @@ class Seat(
@Transient
private var isNew = true

override fun isNew(): Boolean {
return isNew
}
override fun isNew(): Boolean = isNew

override fun getId(): Long {
return id
}
override fun getId(): Long = id

@PrePersist
@PostLoad
fun markNotNew() {
isNew = false
}

fun isAvailable(): Boolean {
return this.status == Status.AVAILABLE
}
fun isAvailable(): Boolean = this.status == Status.AVAILABLE

fun available() {
this.status = Status.AVAILABLE
Expand Down Expand Up @@ -96,15 +90,14 @@ class Seat(
number: String,
price: BigDecimal,
concertId: Long,
): Seat {
return Seat(
): Seat =
Seat(
id = TsIdKeyGenerator.nextLong(),
number = number,
status = Status.AVAILABLE,
price = price,
concertId = concertId,
concertDate = LocalDate.now(),
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.ticketaka.api.point.domain
package io.ticketaka.api.point.application

import java.math.BigDecimal

interface CachePointRecharger {
interface CacheWriteBehindPointService {
fun recharge(
pointId: Long,
amount: BigDecimal,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package io.ticketaka.api.point.application

import io.ticketaka.api.common.domain.EventBroker
import io.ticketaka.api.point.application.dto.BalanceQueryModel
import io.ticketaka.api.point.application.dto.RechargeCommand
import io.ticketaka.api.point.domain.CachePointRecharger
import io.ticketaka.api.point.domain.PointRechargeEvent
import io.ticketaka.api.user.application.QueueTokenUserCacheAsideQueryService
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Service

@Service
class PointService(
private val queueTokenUserCacheAsideQueryService: QueueTokenUserCacheAsideQueryService,
private val pointCacheAsideQueryService: PointCacheAsideQueryService,
private val cachePointRecharger: CachePointRecharger,
private val eventBroker: EventBroker,
private val cacheWriteBehindPointService: CacheWriteBehindPointService,
private val applicationEventPublisher: ApplicationEventPublisher,
) {
fun recharge(rechargeCommand: RechargeCommand) {
val user = queueTokenUserCacheAsideQueryService.getUser(rechargeCommand.userId)
val point = pointCacheAsideQueryService.getPoint(user.pointId)
cachePointRecharger.recharge(point.id, rechargeCommand.amount)
eventBroker.produce(PointRechargeEvent(user.id, point.id, rechargeCommand.amount))
cacheWriteBehindPointService.recharge(point.id, rechargeCommand.amount)
applicationEventPublisher.publishEvent(PointRechargeEvent(user.id, point.id, rechargeCommand.amount))
}

fun getBalance(userId: Long): BalanceQueryModel {
Expand Down
15 changes: 5 additions & 10 deletions src/main/kotlin/io/ticketaka/api/point/domain/Idempotent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Idempotent(
private set

@Column(nullable = false)
var updatedAt: LocalDateTime? = null
var updatedAt: LocalDateTime? = LocalDateTime.now()
private set

@PreUpdate
Expand All @@ -37,13 +37,9 @@ class Idempotent(
@Transient
private var isNew = true

override fun isNew(): Boolean {
return isNew
}
override fun isNew(): Boolean = isNew

override fun getId(): Long {
return id
}
override fun getId(): Long = id

@PrePersist
@PostLoad
Expand All @@ -57,8 +53,7 @@ class Idempotent(
tokenId: Long,
amount: BigDecimal,
transactionType: PointHistory.TransactionType,
): Idempotent {
return Idempotent(TsIdKeyGenerator.nextLong(), IdempotentKeyGenerator.generate(userId, tokenId, amount, transactionType.name))
}
): Idempotent =
Idempotent(TsIdKeyGenerator.nextLong(), IdempotentKeyGenerator.generate(userId, tokenId, amount, transactionType.name))
}
}
18 changes: 7 additions & 11 deletions src/main/kotlin/io/ticketaka/api/point/domain/payment/Payment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ class Payment(
val paymentTime: LocalDateTime,
val userId: Long,
val pointId: Long,
) : AbstractAggregateRoot(), Persistable<Long> {
) : AbstractAggregateRoot(),
Persistable<Long> {
@Column(nullable = false, updatable = false)
var createdAt: LocalDateTime? = LocalDateTime.now()
private set

@Column(nullable = false)
var updatedAt: LocalDateTime? = null
var updatedAt: LocalDateTime? = LocalDateTime.now()
private set

@PreUpdate
Expand All @@ -40,13 +41,9 @@ class Payment(
@Transient
private var isNew = true

override fun isNew(): Boolean {
return isNew
}
override fun isNew(): Boolean = isNew

override fun getId(): Long {
return id
}
override fun getId(): Long = id

@PrePersist
@PostLoad
Expand All @@ -63,14 +60,13 @@ class Payment(
amount: BigDecimal,
userId: Long,
pointId: Long,
): Payment {
return Payment(
): Payment =
Payment(
id = TsIdKeyGenerator.nextLong(),
amount = amount,
userId = userId,
paymentTime = LocalDateTime.now(),
pointId = pointId,
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package io.ticketaka.api.point.infrastructure

import io.ticketaka.api.common.exception.NotFoundException
import io.ticketaka.api.point.domain.CachePointRecharger
import io.ticketaka.api.point.application.CacheWriteBehindPointService
import io.ticketaka.api.point.domain.Point
import org.springframework.cache.caffeine.CaffeineCacheManager
import org.springframework.stereotype.Component
import java.math.BigDecimal

@Component
class InMemoryCacheCachePointRecharger(
class InMemoryCacheCacheWriteBehindPointService(
private val caffeineCacheManager: CaffeineCacheManager,
) : CachePointRecharger {
) : CacheWriteBehindPointService {
override fun recharge(
pointId: Long,
amount: BigDecimal,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package io.ticketaka.api.point.infrastructure.event

import io.ticketaka.api.common.domain.EventBroker
import io.ticketaka.api.common.infrastructure.event.EventProducer
import io.ticketaka.api.point.domain.PointChargeEvent
import io.ticketaka.api.point.domain.PointRechargeEvent
import org.springframework.context.event.EventListener
import org.springframework.stereotype.Component

@Component
class PointEventHandler(
private val eventBroker: EventBroker,
private val eventProducer: EventProducer,
) {
@EventListener
fun handle(event: PointRechargeEvent) {
eventBroker.produce(event)
eventProducer.produce(event)
}

@EventListener
fun handle(event: PointChargeEvent) {
eventBroker.produce(event)
eventProducer.produce(event)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.ticketaka.api.reservation.application

import io.ticketaka.api.common.domain.EventBroker
import io.ticketaka.api.common.exception.NotFoundException
import io.ticketaka.api.concert.application.ConcertCacheAsideQueryService
import io.ticketaka.api.concert.domain.ConcertSeatUpdater
import io.ticketaka.api.reservation.application.dto.CreateReservationCommand
import io.ticketaka.api.reservation.domain.reservation.ReservationCreateEvent
import io.ticketaka.api.reservation.domain.reservation.ReservationRepository
import io.ticketaka.api.user.application.QueueTokenUserCacheAsideQueryService
import org.springframework.context.ApplicationEventPublisher
import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -18,13 +18,13 @@ class ReservationService(
private val concertCacheAsideQueryService: ConcertCacheAsideQueryService,
private val reservationRepository: ReservationRepository,
private val concertSeatUpdater: ConcertSeatUpdater,
private val eventBroker: EventBroker,
private val applicationEventPublisher: ApplicationEventPublisher,
) {
fun createReservation(command: CreateReservationCommand) {
val user = queueTokenUserCacheAsideQueryService.getUser(command.userId)
val concert = concertCacheAsideQueryService.getConcert(command.date)
val seats = concertSeatUpdater.reserve(concert.id, command.date, command.seatNumbers)
eventBroker.produce(ReservationCreateEvent(user.id, concert.id, seats.map { it.id }))
applicationEventPublisher.publishEvent(ReservationCreateEvent(user.id, concert.id, seats.map { it.id }))
}

@Async
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class Reservation(
var createdAt: LocalDateTime? = LocalDateTime.now()
private set

@Column(nullable = false)
var updatedAt: LocalDateTime? = null
@Column
var updatedAt: LocalDateTime? = LocalDateTime.now()
private set

@PreUpdate
Expand All @@ -44,13 +44,9 @@ class Reservation(
@Transient
private var isNew = true

override fun isNew(): Boolean {
return isNew
}
override fun isNew(): Boolean = isNew

override fun getId(): Long {
return id
}
override fun getId(): Long = id

@PrePersist
@PostLoad
Expand Down Expand Up @@ -90,15 +86,14 @@ class Reservation(
fun createPendingReservation(
userId: Long,
concertId: Long,
): Reservation {
return Reservation(
TsIdKeyGenerator.nextLong(),
Status.PENDING,
LocalDateTime.now(),
LocalDateTime.now().plusMinutes(5L),
userId,
concertId,
): Reservation =
Reservation(
id = TsIdKeyGenerator.nextLong(),
status = Status.PENDING,
reservationTime = LocalDateTime.now(),
expirationTime = LocalDateTime.now().plusMinutes(5L),
userId = userId,
concertId = concertId,
)
}
}
}
Loading
Loading