refactor: ID生成を全てIdGenerateServiceで行うように

This commit is contained in:
usbharu 2023-09-30 13:48:24 +09:00
parent c10d7e5204
commit 0df65ad9b7
10 changed files with 18 additions and 18 deletions

View File

@ -13,7 +13,6 @@ class MongoTimelineRepositoryWrapper(
private val idGenerateService: IdGenerateService
) :
TimelineRepository {
override suspend fun generateId(): Long = idGenerateService.generateId()
override suspend fun save(timeline: Timeline): Timeline {
return withContext(Dispatchers.IO) {

View File

@ -6,7 +6,6 @@ import org.springframework.stereotype.Repository
@Suppress("LongParameterList")
@Repository
interface PostRepository {
suspend fun generateId(): Long
suspend fun save(post: Post): Post
suspend fun delete(id: Long)
suspend fun findById(id: Long): Post

View File

@ -11,8 +11,6 @@ import org.springframework.stereotype.Repository
@Repository
class PostRepositoryImpl(private val idGenerateService: IdGenerateService) : PostRepository {
override suspend fun generateId(): Long = idGenerateService.generateId()
override suspend fun save(post: Post): Post {
val singleOrNull = Posts.select { Posts.id eq post.id }.singleOrNull()
if (singleOrNull == null) {

View File

@ -5,7 +5,6 @@ import org.springframework.stereotype.Repository
@Repository
interface ReactionRepository {
suspend fun generateId(): Long
suspend fun save(reaction: Reaction): Reaction
suspend fun delete(reaction: Reaction): Reaction
}

View File

@ -13,8 +13,6 @@ class ReactionRepositoryImpl(
) : ReactionRepository {
override suspend fun generateId(): Long = idGenerateService.generateId()
override suspend fun save(reaction: Reaction): Reaction {
if (Reactions.select { Reactions.id eq reaction.id }.empty()) {
Reactions.insert {

View File

@ -3,7 +3,6 @@ package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.hideout.entity.Timeline
interface TimelineRepository {
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>

View File

@ -16,6 +16,7 @@ import dev.usbharu.hideout.query.FollowerQueryService
import dev.usbharu.hideout.query.PostQueryService
import dev.usbharu.hideout.query.UserQueryService
import dev.usbharu.hideout.repository.PostRepository
import dev.usbharu.hideout.service.core.IdGenerateService
import dev.usbharu.hideout.service.job.JobQueueParentService
import dev.usbharu.hideout.service.post.PostCreateInterceptor
import dev.usbharu.hideout.service.post.PostService
@ -49,7 +50,8 @@ class APNoteServiceImpl(
private val postQueryService: PostQueryService,
@Qualifier("activitypub") private val objectMapper: ObjectMapper,
private val applicationConfig: ApplicationConfig,
private val postService: PostService
private val postService: PostService,
private val idGenerateService: IdGenerateService
) : APNoteService, PostCreateInterceptor {
@ -171,7 +173,7 @@ class APNoteServiceImpl(
postService.createRemote(
Post.of(
id = postRepository.generateId(),
id = idGenerateService.generateId(),
userId = person.second.id,
overview = null,
text = note.content.orEmpty(),

View File

@ -5,6 +5,7 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Post
import dev.usbharu.hideout.exception.UserNotFoundException
import dev.usbharu.hideout.repository.PostRepository
import dev.usbharu.hideout.repository.UserRepository
import dev.usbharu.hideout.service.core.IdGenerateService
import org.springframework.stereotype.Service
import java.time.Instant
import java.util.*
@ -13,7 +14,8 @@ import java.util.*
class PostServiceImpl(
private val postRepository: PostRepository,
private val userRepository: UserRepository,
private val timelineService: TimelineService
private val timelineService: TimelineService,
private val idGenerateService: IdGenerateService
) : PostService {
private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>())
@ -36,7 +38,7 @@ class PostServiceImpl(
private suspend fun internalCreate(post: PostCreateDto, isLocal: Boolean): Post {
val user = userRepository.findById(post.userId) ?: throw UserNotFoundException("${post.userId} was not found")
val id = postRepository.generateId()
val id = idGenerateService.generateId()
val createPost = Post.of(
id = id,
userId = post.userId,

View File

@ -6,13 +6,15 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Visibility
import dev.usbharu.hideout.query.FollowerQueryService
import dev.usbharu.hideout.query.UserQueryService
import dev.usbharu.hideout.repository.TimelineRepository
import dev.usbharu.hideout.service.core.IdGenerateService
import org.springframework.stereotype.Service
@Service
class TimelineService(
private val followerQueryService: FollowerQueryService,
private val userQueryService: UserQueryService,
private val timelineRepository: TimelineRepository
private val timelineRepository: TimelineRepository,
private val idGenerateService: IdGenerateService
) {
suspend fun publishTimeline(post: Post, isLocal: Boolean) {
val findFollowersById = followerQueryService.findFollowersById(post.userId).toMutableList()
@ -23,7 +25,7 @@ class TimelineService(
}
val timelines = findFollowersById.map {
Timeline(
id = timelineRepository.generateId(),
id = idGenerateService.generateId(),
userId = it.id,
timelineId = 0,
postId = post.id,
@ -39,7 +41,7 @@ class TimelineService(
if (post.visibility == Visibility.PUBLIC) {
timelines.add(
Timeline(
id = timelineRepository.generateId(),
id = idGenerateService.generateId(),
userId = 0,
timelineId = 0,
postId = post.id,

View File

@ -4,18 +4,20 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Reaction
import dev.usbharu.hideout.query.ReactionQueryService
import dev.usbharu.hideout.repository.ReactionRepository
import dev.usbharu.hideout.service.ap.APReactionService
import dev.usbharu.hideout.service.core.IdGenerateService
import org.springframework.stereotype.Service
@Service
class ReactionServiceImpl(
private val reactionRepository: ReactionRepository,
private val apReactionService: APReactionService,
private val reactionQueryService: ReactionQueryService
private val reactionQueryService: ReactionQueryService,
private val idGenerateService: IdGenerateService
) : ReactionService {
override suspend fun receiveReaction(name: String, domain: String, userId: Long, postId: Long) {
if (reactionQueryService.reactionAlreadyExist(postId, userId, 0).not()) {
reactionRepository.save(
Reaction(reactionRepository.generateId(), 0, postId, userId)
Reaction(idGenerateService.generateId(), 0, postId, userId)
)
}
}
@ -25,7 +27,7 @@ class ReactionServiceImpl(
// delete
reactionQueryService.deleteByPostIdAndUserId(postId, userId)
} else {
val reaction = Reaction(reactionRepository.generateId(), 0, postId, userId)
val reaction = Reaction(idGenerateService.generateId(), 0, postId, userId)
reactionRepository.save(reaction)
apReactionService.reaction(reaction)
}