Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gogo.gogobetting.domain.batch.root.application.listener

import gogo.gogobetting.domain.batch.root.event.BatchCancelEvent
import gogo.gogobetting.domain.batch.root.event.MatchBatchEvent
import gogo.gogobetting.global.kafka.publisher.BatchPublisher
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
Expand All @@ -15,14 +14,6 @@ class BatchApplicationEventListener(

private val log = LoggerFactory.getLogger(this::class.java.simpleName)

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
fun bettingBatchEvent(event: MatchBatchEvent) {
with(event) {
log.info("published betting batch application event: {}", id)
batchPublisher.publishBettingBatchEvent(event)
}
}

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
fun batchCancelEvent(event: BatchCancelEvent) {
with(event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import gogo.gogobetting.domain.batch.root.persistence.BatchRepository
import gogo.gogobetting.domain.betting.result.persistence.BettingResult
import gogo.gogobetting.domain.betting.result.persistence.BettingResultRepository
import gogo.gogobetting.domain.betting.root.persistence.BettingRepository
import gogo.gogobetting.global.kafka.publisher.BatchPublisher
import org.slf4j.LoggerFactory
import org.springframework.batch.core.StepExecution
import org.springframework.batch.core.annotation.AfterStep
import org.springframework.batch.core.annotation.BeforeStep
import org.springframework.batch.core.configuration.annotation.StepScope
import org.springframework.batch.item.Chunk
Expand All @@ -27,6 +30,7 @@ class BettingWriter(
private val batchRepository: BatchRepository,
private val bettingRepository: BettingRepository,
private val applicationEventPublisher: ApplicationEventPublisher,
private val batchPublisher: BatchPublisher,
) : ItemWriter<BettingResult> {

@Value("#{jobParameters['matchId']}")
Expand All @@ -42,17 +46,21 @@ class BettingWriter(
private val bTeamScore: Int = 0

private var batchId: Long = 0
private val accumulatedItems = mutableListOf<BettingResult>()

private val log = LoggerFactory.getLogger(this::class.java.simpleName)

@BeforeStep
fun beforeStep(stepExecution: StepExecution) {
val jobExecution = stepExecution.jobExecution
accumulatedItems.clear()
batchId = jobExecution.executionContext["batchId"]!! as Long
}

override fun write(items: Chunk<out BettingResult>) {
bettingResultRepository.saveAll(items)

@AfterStep
fun afterStep(stepExecution: StepExecution) {
val batch = batchRepository.findByIdOrNull(batchId)!!

batchDetailRepository.save(
BatchDetail.of(
batchId = batch.id,
Expand All @@ -62,24 +70,28 @@ class BettingWriter(
)
)

val successList = items.filter { it.isPredicted }
.map { StudentBettingDto(
bettingRepository.findByIdOrNull(it.bettingId)!!.studentId,
it.earnedPoint
)
val successList = accumulatedItems.filter { it.isPredicted }
.map {
val studentId = bettingRepository.findByIdOrNull(it.bettingId)!!.studentId
StudentBettingDto(studentId, it.earnedPoint)
}

applicationEventPublisher.publishEvent(
MatchBatchEvent(
id = UUID.randomUUID().toString(),
batchId = batchId,
matchId = matchId,
victoryTeamId = winTeamId,
aTeamScore = aTeamScore,
bTeamScore = bTeamScore,
students = successList
)
val event = MatchBatchEvent(
id = UUID.randomUUID().toString(),
batchId = batchId,
matchId = matchId,
victoryTeamId = winTeamId,
aTeamScore = aTeamScore,
bTeamScore = bTeamScore,
students = successList
)

log.info("published betting batch application event: {}", event.id)
batchPublisher.publishBettingBatchEvent(event)
}

override fun write(items: Chunk<out BettingResult>) {
bettingResultRepository.saveAll(items)
accumulatedItems.addAll(items)
}
}