feat(recommend): 조회 이력 성공 로그를 커밋 후 기록한다
This commit is contained in:
@@ -2,20 +2,36 @@ package kr.co.vividnext.sodalive.v2.recommend.application
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.recommend.port.out.CreatorContentViewHistoryPort
|
||||
import kr.co.vividnext.sodalive.v2.recommend.port.out.CreatorContentViewHistoryRecord
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Propagation
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.transaction.support.TransactionSynchronization
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager
|
||||
import java.time.LocalDateTime
|
||||
|
||||
@Service
|
||||
class CreatorContentViewHistoryService(
|
||||
private val port: CreatorContentViewHistoryPort
|
||||
) {
|
||||
private val log = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
fun recordView(memberId: Long?, contentId: Long, viewedAt: LocalDateTime = LocalDateTime.now()) {
|
||||
if (memberId == null) return
|
||||
if (memberId == null) {
|
||||
log.info("event=creator_content_view_history_record_skipped reason=anonymous contentId={}", contentId)
|
||||
return
|
||||
}
|
||||
|
||||
val genreId = port.findGenreIdByContentId(contentId) ?: return
|
||||
val genreId = port.findGenreIdByContentId(contentId)
|
||||
if (genreId == null) {
|
||||
log.info(
|
||||
"event=creator_content_view_history_record_skipped reason=genre_not_found memberId={} contentId={}",
|
||||
memberId,
|
||||
contentId
|
||||
)
|
||||
return
|
||||
}
|
||||
port.save(
|
||||
CreatorContentViewHistoryRecord(
|
||||
memberId = memberId,
|
||||
@@ -24,5 +40,25 @@ class CreatorContentViewHistoryService(
|
||||
viewedAt = viewedAt
|
||||
)
|
||||
)
|
||||
afterCommit {
|
||||
log.info(
|
||||
"event=creator_content_view_history_record_success memberId={} contentId={} genreId={}",
|
||||
memberId,
|
||||
contentId,
|
||||
genreId
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun afterCommit(action: () -> Unit) {
|
||||
if (!TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
action()
|
||||
return
|
||||
}
|
||||
TransactionSynchronizationManager.registerSynchronization(
|
||||
object : TransactionSynchronization {
|
||||
override fun afterCommit() = action()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user