feat(home-following): 팔로잉 소식 inbox 저장 adapter를 추가한다
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package kr.co.vividnext.sodalive.v2.home.following.adapter.out.persistence
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.home.following.domain.FollowingNewsType
|
||||
import kr.co.vividnext.sodalive.v2.home.following.port.out.HomeFollowingNewsInboxPort
|
||||
import kr.co.vividnext.sodalive.v2.home.following.port.out.HomeFollowingNewsInboxRecord
|
||||
import org.springframework.dao.DataIntegrityViolationException
|
||||
import org.springframework.stereotype.Repository
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import javax.persistence.EntityManager
|
||||
|
||||
@Repository
|
||||
class HomeFollowingNewsInboxPersistenceAdapter(
|
||||
private val repository: HomeFollowingNewsInboxJpaRepository,
|
||||
private val entityManager: EntityManager
|
||||
) : HomeFollowingNewsInboxPort {
|
||||
@Transactional
|
||||
override fun insertIgnoreAll(records: List<HomeFollowingNewsInboxRecord>): Int {
|
||||
if (records.isEmpty()) {
|
||||
return 0
|
||||
}
|
||||
|
||||
return records
|
||||
.distinctBy { Triple(it.memberId, it.newsType, it.sourceKey) }
|
||||
.sumOf { record -> insertIgnore(record) }
|
||||
}
|
||||
|
||||
@Transactional
|
||||
override fun deactivateByMemberIdAndCreatorId(memberId: Long, creatorId: Long): Long {
|
||||
return repository.deactivateByMemberIdAndCreatorId(memberId, creatorId).toLong()
|
||||
}
|
||||
|
||||
override fun findActiveFollowerIds(creatorId: Long): List<Long> {
|
||||
return repository.findActiveFollowerIds(creatorId)
|
||||
}
|
||||
|
||||
private fun insertIgnore(record: HomeFollowingNewsInboxRecord): Int {
|
||||
val newsType = FollowingNewsType.valueOf(record.newsType)
|
||||
if (repository.existsByMemberIdAndNewsTypeAndSourceKey(record.memberId, newsType, record.sourceKey)) {
|
||||
return 0
|
||||
}
|
||||
|
||||
return try {
|
||||
repository.saveAndFlush(record.toEntity(newsType))
|
||||
1
|
||||
} catch (e: DataIntegrityViolationException) {
|
||||
entityManager.clear()
|
||||
if (repository.existsByMemberIdAndNewsTypeAndSourceKey(record.memberId, newsType, record.sourceKey)) {
|
||||
0
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun HomeFollowingNewsInboxRecord.toEntity(newsType: FollowingNewsType): HomeFollowingNewsInbox {
|
||||
return HomeFollowingNewsInbox(
|
||||
memberId = memberId,
|
||||
creatorId = creatorId,
|
||||
newsType = newsType,
|
||||
sourceKey = sourceKey,
|
||||
targetId = targetId,
|
||||
occurredAtUtc = occurredAtUtc,
|
||||
visibleFromAtUtc = visibleFromAtUtc,
|
||||
creatorNickname = creatorNickname,
|
||||
creatorProfileImagePath = creatorProfileImagePath,
|
||||
title = title,
|
||||
body = body,
|
||||
thumbnailImagePath = thumbnailImagePath,
|
||||
rank = rank,
|
||||
isAdult = isAdult
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user