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 private val idGenerateService: IdGenerateService
) : ) :
TimelineRepository { TimelineRepository {
override suspend fun generateId(): Long = idGenerateService.generateId()
override suspend fun save(timeline: Timeline): Timeline { override suspend fun save(timeline: Timeline): Timeline {
return withContext(Dispatchers.IO) { return withContext(Dispatchers.IO) {

View File

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

View File

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

View File

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

View File

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

View File

@ -3,7 +3,6 @@ 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 {
suspend fun generateId(): Long
suspend fun save(timeline: Timeline): Timeline suspend fun save(timeline: Timeline): Timeline
suspend fun saveAll(timelines: List<Timeline>): List<Timeline> suspend fun saveAll(timelines: List<Timeline>): List<Timeline>
suspend fun findByUserId(id: Long): 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.PostQueryService
import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.query.UserQueryService
import dev.usbharu.hideout.repository.PostRepository 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.job.JobQueueParentService
import dev.usbharu.hideout.service.post.PostCreateInterceptor import dev.usbharu.hideout.service.post.PostCreateInterceptor
import dev.usbharu.hideout.service.post.PostService import dev.usbharu.hideout.service.post.PostService
@ -49,7 +50,8 @@ class APNoteServiceImpl(
private val postQueryService: PostQueryService, private val postQueryService: PostQueryService,
@Qualifier("activitypub") private val objectMapper: ObjectMapper, @Qualifier("activitypub") private val objectMapper: ObjectMapper,
private val applicationConfig: ApplicationConfig, private val applicationConfig: ApplicationConfig,
private val postService: PostService private val postService: PostService,
private val idGenerateService: IdGenerateService
) : APNoteService, PostCreateInterceptor { ) : APNoteService, PostCreateInterceptor {
@ -171,7 +173,7 @@ class APNoteServiceImpl(
postService.createRemote( postService.createRemote(
Post.of( Post.of(
id = postRepository.generateId(), id = idGenerateService.generateId(),
userId = person.second.id, userId = person.second.id,
overview = null, overview = null,
text = note.content.orEmpty(), 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.exception.UserNotFoundException
import dev.usbharu.hideout.repository.PostRepository import dev.usbharu.hideout.repository.PostRepository
import dev.usbharu.hideout.repository.UserRepository import dev.usbharu.hideout.repository.UserRepository
import dev.usbharu.hideout.service.core.IdGenerateService
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.time.Instant import java.time.Instant
import java.util.* import java.util.*
@ -13,7 +14,8 @@ 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 private val timelineService: TimelineService,
private val idGenerateService: IdGenerateService
) : PostService { ) : PostService {
private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>()) private val interceptors = Collections.synchronizedList(mutableListOf<PostCreateInterceptor>())
@ -36,7 +38,7 @@ class PostServiceImpl(
private suspend fun internalCreate(post: PostCreateDto, isLocal: Boolean): Post { private suspend fun internalCreate(post: PostCreateDto, isLocal: Boolean): Post {
val user = userRepository.findById(post.userId) ?: throw UserNotFoundException("${post.userId} was not found") 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( val createPost = Post.of(
id = id, id = id,
userId = post.userId, 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.FollowerQueryService
import dev.usbharu.hideout.query.UserQueryService import dev.usbharu.hideout.query.UserQueryService
import dev.usbharu.hideout.repository.TimelineRepository import dev.usbharu.hideout.repository.TimelineRepository
import dev.usbharu.hideout.service.core.IdGenerateService
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@Service @Service
class TimelineService( class TimelineService(
private val followerQueryService: FollowerQueryService, private val followerQueryService: FollowerQueryService,
private val userQueryService: UserQueryService, private val userQueryService: UserQueryService,
private val timelineRepository: TimelineRepository private val timelineRepository: TimelineRepository,
private val idGenerateService: IdGenerateService
) { ) {
suspend fun publishTimeline(post: Post, isLocal: Boolean) { suspend fun publishTimeline(post: Post, isLocal: Boolean) {
val findFollowersById = followerQueryService.findFollowersById(post.userId).toMutableList() val findFollowersById = followerQueryService.findFollowersById(post.userId).toMutableList()
@ -23,7 +25,7 @@ class TimelineService(
} }
val timelines = findFollowersById.map { val timelines = findFollowersById.map {
Timeline( Timeline(
id = timelineRepository.generateId(), id = idGenerateService.generateId(),
userId = it.id, userId = it.id,
timelineId = 0, timelineId = 0,
postId = post.id, postId = post.id,
@ -39,7 +41,7 @@ class TimelineService(
if (post.visibility == Visibility.PUBLIC) { if (post.visibility == Visibility.PUBLIC) {
timelines.add( timelines.add(
Timeline( Timeline(
id = timelineRepository.generateId(), id = idGenerateService.generateId(),
userId = 0, userId = 0,
timelineId = 0, timelineId = 0,
postId = post.id, 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.query.ReactionQueryService
import dev.usbharu.hideout.repository.ReactionRepository import dev.usbharu.hideout.repository.ReactionRepository
import dev.usbharu.hideout.service.ap.APReactionService import dev.usbharu.hideout.service.ap.APReactionService
import dev.usbharu.hideout.service.core.IdGenerateService
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@Service @Service
class ReactionServiceImpl( class ReactionServiceImpl(
private val reactionRepository: ReactionRepository, private val reactionRepository: ReactionRepository,
private val apReactionService: APReactionService, private val apReactionService: APReactionService,
private val reactionQueryService: ReactionQueryService private val reactionQueryService: ReactionQueryService,
private val idGenerateService: IdGenerateService
) : ReactionService { ) : ReactionService {
override suspend fun receiveReaction(name: String, domain: String, userId: Long, postId: Long) { override suspend fun receiveReaction(name: String, domain: String, userId: Long, postId: Long) {
if (reactionQueryService.reactionAlreadyExist(postId, userId, 0).not()) { if (reactionQueryService.reactionAlreadyExist(postId, userId, 0).not()) {
reactionRepository.save( reactionRepository.save(
Reaction(reactionRepository.generateId(), 0, postId, userId) Reaction(idGenerateService.generateId(), 0, postId, userId)
) )
} }
} }
@ -25,7 +27,7 @@ class ReactionServiceImpl(
// delete // delete
reactionQueryService.deleteByPostIdAndUserId(postId, userId) reactionQueryService.deleteByPostIdAndUserId(postId, userId)
} else { } else {
val reaction = Reaction(reactionRepository.generateId(), 0, postId, userId) val reaction = Reaction(idGenerateService.generateId(), 0, postId, userId)
reactionRepository.save(reaction) reactionRepository.save(reaction)
apReactionService.reaction(reaction) apReactionService.reaction(reaction)
} }