mirror of https://github.com/usbharu/Hideout.git
feat: TimelineServiceを作成
This commit is contained in:
parent
8c34019728
commit
5b89c681b0
|
@ -1,7 +1,6 @@
|
||||||
package dev.usbharu.hideout.domain.model.hideout.entity
|
package dev.usbharu.hideout.domain.model.hideout.entity
|
||||||
|
|
||||||
import org.springframework.data.annotation.Id
|
import org.springframework.data.annotation.Id
|
||||||
import java.time.Instant
|
|
||||||
|
|
||||||
data class Timeline(
|
data class Timeline(
|
||||||
@Id
|
@Id
|
||||||
|
@ -10,9 +9,9 @@ data class Timeline(
|
||||||
val timelineId: Long,
|
val timelineId: Long,
|
||||||
val postId: Long,
|
val postId: Long,
|
||||||
val postUserId: Long,
|
val postUserId: Long,
|
||||||
val createdAt: Instant,
|
val createdAt: Long,
|
||||||
val replyId: Long,
|
val replyId: Long?,
|
||||||
val repostId: Long,
|
val repostId: Long?,
|
||||||
val visibility: Visibility,
|
val visibility: Visibility,
|
||||||
val sensitive: Boolean
|
val sensitive: Boolean
|
||||||
)
|
)
|
||||||
|
|
|
@ -3,7 +3,9 @@ package dev.usbharu.hideout.repository
|
||||||
import dev.usbharu.hideout.domain.model.hideout.entity.Timeline
|
import dev.usbharu.hideout.domain.model.hideout.entity.Timeline
|
||||||
import org.springframework.data.mongodb.repository.MongoRepository
|
import org.springframework.data.mongodb.repository.MongoRepository
|
||||||
|
|
||||||
interface MongoTimelineRepository : TimelineRepository, MongoRepository<Timeline, Long> {
|
interface MongoTimelineRepository : MongoRepository<Timeline, Long> {
|
||||||
override fun findByUserId(id: Long): List<Timeline>
|
|
||||||
override fun findByUserIdAndTimelineId(userId: Long, timelineId: Long): List<Timeline>
|
|
||||||
|
fun findByUserId(id: Long): List<Timeline>
|
||||||
|
fun findByUserIdAndTimelineId(userId: Long, timelineId: Long): List<Timeline>
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,6 +3,9 @@ package dev.usbharu.hideout.repository
|
||||||
import dev.usbharu.hideout.domain.model.hideout.entity.Timeline
|
import dev.usbharu.hideout.domain.model.hideout.entity.Timeline
|
||||||
|
|
||||||
interface TimelineRepository {
|
interface TimelineRepository {
|
||||||
fun findByUserId(id: Long): List<Timeline>
|
suspend fun generateId(): Long
|
||||||
fun findByUserIdAndTimelineId(userId: Long, timelineId: Long): List<Timeline>
|
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>
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ import java.util.*
|
||||||
class PostServiceImpl(
|
class PostServiceImpl(
|
||||||
private val postRepository: PostRepository,
|
private val postRepository: PostRepository,
|
||||||
private val userRepository: UserRepository,
|
private val userRepository: UserRepository,
|
||||||
|
private val timelineService: TimelineService
|
||||||
) : PostService {
|
) : PostService {
|
||||||
private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>())
|
private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>())
|
||||||
|
|
||||||
|
@ -31,7 +32,7 @@ class PostServiceImpl(
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun internalCreate(post: Post): Post {
|
private suspend fun internalCreate(post: Post): Post {
|
||||||
|
timelineService.publishTimeline(post)
|
||||||
return postRepository.save(post)
|
return postRepository.save(post)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,6 +50,6 @@ class PostServiceImpl(
|
||||||
repostId = null,
|
repostId = null,
|
||||||
replyId = null
|
replyId = null
|
||||||
)
|
)
|
||||||
return internalCreate(post)
|
return internalCreate(createPost)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,7 +24,11 @@ spring:
|
||||||
password: ""
|
password: ""
|
||||||
data:
|
data:
|
||||||
mongodb:
|
mongodb:
|
||||||
|
host: localhost
|
||||||
|
port: 27017
|
||||||
|
database: hideout
|
||||||
|
# username: hideoutuser
|
||||||
|
# password: hideoutpass
|
||||||
|
|
||||||
h2:
|
h2:
|
||||||
console:
|
console:
|
||||||
|
|
Loading…
Reference in New Issue