feat: TimelineServiceを作成

This commit is contained in:
usbharu 2023-09-29 15:44:58 +09:00
parent 8c34019728
commit 5b89c681b0
7 changed files with 90 additions and 12 deletions

View File

@ -1,7 +1,6 @@
package dev.usbharu.hideout.domain.model.hideout.entity
import org.springframework.data.annotation.Id
import java.time.Instant
data class Timeline(
@Id
@ -10,9 +9,9 @@ data class Timeline(
val timelineId: Long,
val postId: Long,
val postUserId: Long,
val createdAt: Instant,
val replyId: Long,
val repostId: Long,
val createdAt: Long,
val replyId: Long?,
val repostId: Long?,
val visibility: Visibility,
val sensitive: Boolean
)

View File

@ -3,7 +3,9 @@ package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.hideout.entity.Timeline
import org.springframework.data.mongodb.repository.MongoRepository
interface MongoTimelineRepository : TimelineRepository, MongoRepository<Timeline, Long> {
override fun findByUserId(id: Long): List<Timeline>
override fun findByUserIdAndTimelineId(userId: Long, timelineId: Long): List<Timeline>
interface MongoTimelineRepository : MongoRepository<Timeline, Long> {
fun findByUserId(id: Long): List<Timeline>
fun findByUserIdAndTimelineId(userId: Long, timelineId: Long): List<Timeline>
}

View File

@ -0,0 +1,38 @@
package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.hideout.entity.Timeline
import dev.usbharu.hideout.service.core.IdGenerateService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.springframework.stereotype.Repository
@Repository
class MongoTimelineRepositoryWrapper(
private val mongoTimelineRepository: MongoTimelineRepository,
private val idGenerateService: IdGenerateService
) :
TimelineRepository {
override suspend fun generateId(): Long = idGenerateService.generateId()
override suspend fun save(timeline: Timeline): Timeline {
return withContext(Dispatchers.IO) {
mongoTimelineRepository.save(timeline)
}
}
override suspend fun saveAll(timelines: List<Timeline>): List<Timeline> {
return mongoTimelineRepository.saveAll(timelines)
}
override suspend fun findByUserId(id: Long): List<Timeline> {
return withContext(Dispatchers.IO) {
mongoTimelineRepository.findByUserId(id)
}
}
override suspend fun findByUserIdAndTimelineId(userId: Long, timelineId: Long): List<Timeline> {
return withContext(Dispatchers.IO) {
mongoTimelineRepository.findByUserIdAndTimelineId(userId, timelineId)
}
}
}

View File

@ -3,6 +3,9 @@ package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.hideout.entity.Timeline
interface TimelineRepository {
fun findByUserId(id: Long): List<Timeline>
fun findByUserIdAndTimelineId(userId: Long, timelineId: Long): List<Timeline>
suspend fun generateId(): Long
suspend fun save(timeline: Timeline): Timeline
suspend fun saveAll(timelines: List<Timeline>): List<Timeline>
suspend fun findByUserId(id: Long): List<Timeline>
suspend fun findByUserIdAndTimelineId(userId: Long, timelineId: Long): List<Timeline>
}

View File

@ -13,6 +13,7 @@ import java.util.*
class PostServiceImpl(
private val postRepository: PostRepository,
private val userRepository: UserRepository,
private val timelineService: TimelineService
) : PostService {
private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>())
@ -31,7 +32,7 @@ class PostServiceImpl(
}
private suspend fun internalCreate(post: Post): Post {
timelineService.publishTimeline(post)
return postRepository.save(post)
}
@ -49,6 +50,6 @@ class PostServiceImpl(
repostId = null,
replyId = null
)
return internalCreate(post)
return internalCreate(createPost)
}
}

View File

@ -0,0 +1,31 @@
package dev.usbharu.hideout.service.post
import dev.usbharu.hideout.domain.model.hideout.entity.Post
import dev.usbharu.hideout.domain.model.hideout.entity.Timeline
import dev.usbharu.hideout.query.FollowerQueryService
import dev.usbharu.hideout.repository.TimelineRepository
import org.springframework.stereotype.Service
@Service
class TimelineService(
private val followerQueryService: FollowerQueryService,
private val timelineRepository: TimelineRepository
) {
suspend fun publishTimeline(post: Post) {
val findFollowersById = followerQueryService.findFollowersById(post.userId)
timelineRepository.saveAll(findFollowersById.map {
Timeline(
id = timelineRepository.generateId(),
userId = it.id,
timelineId = 0,
postId = post.id,
postUserId = post.userId,
createdAt = post.createdAt,
replyId = post.replyId,
repostId = post.repostId,
visibility = post.visibility,
sensitive = post.sensitive
)
})
}
}

View File

@ -24,7 +24,11 @@ spring:
password: ""
data:
mongodb:
host: localhost
port: 27017
database: hideout
# username: hideoutuser
# password: hideoutpass
h2:
console: