diff --git a/src/main/kotlin/dev/usbharu/hideout/query/PostResponseQueryService.kt b/src/main/kotlin/dev/usbharu/hideout/query/PostResponseQueryService.kt index 9386a4ce..65441189 100644 --- a/src/main/kotlin/dev/usbharu/hideout/query/PostResponseQueryService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/query/PostResponseQueryService.kt @@ -4,23 +4,34 @@ import dev.usbharu.hideout.domain.model.hideout.dto.PostResponse @Suppress("LongParameterList") interface PostResponseQueryService { - suspend fun findById(id: Long, userId: Long): PostResponse + suspend fun findById(id: Long, userId: Long?): PostResponse suspend fun findAll( - since: Long, - until: Long, - minId: Long, - maxId: Long, - limit: Long, - userId: Long + since: Long? = null, + until: Long? = null, + minId: Long? = null, + maxId: Long? = null, + limit: Int? = null, + userId: Long? = null ): List suspend fun findByUserId( userId: Long, - since: Long, - until: Long, - minId: Long, - maxId: Long, - limit: Long, - userId2: Long + since: Long? = null, + until: Long? = null, + minId: Long? = null, + maxId: Long? = null, + limit: Int? = null, + userId2: Long? = null + ): List + + suspend fun findByUserNameAndUserDomain( + name: String, + domain: String, + since: Long? = null, + until: Long? = null, + minId: Long? = null, + maxId: Long? = null, + limit: Int? = null, + userId: Long? = null ): List } diff --git a/src/main/kotlin/dev/usbharu/hideout/query/PostResponseQueryServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/query/PostResponseQueryServiceImpl.kt index 858aa05f..51bd8ebe 100644 --- a/src/main/kotlin/dev/usbharu/hideout/query/PostResponseQueryServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/query/PostResponseQueryServiceImpl.kt @@ -5,6 +5,7 @@ import dev.usbharu.hideout.repository.Posts import dev.usbharu.hideout.repository.Users import dev.usbharu.hideout.repository.toPost import dev.usbharu.hideout.repository.toUser +import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.innerJoin import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.selectAll @@ -12,7 +13,7 @@ import org.koin.core.annotation.Single @Single class PostResponseQueryServiceImpl : PostResponseQueryService { - override suspend fun findById(id: Long, userId: Long): PostResponse { + override suspend fun findById(id: Long, userId: Long?): PostResponse { return Posts .innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { Users.id }) .select { Posts.id eq id } @@ -21,12 +22,12 @@ class PostResponseQueryServiceImpl : PostResponseQueryService { } override suspend fun findAll( - since: Long, - until: Long, - minId: Long, - maxId: Long, - limit: Long, - userId: Long + since: Long?, + until: Long?, + minId: Long?, + maxId: Long?, + limit: Int?, + userId: Long? ): List { return Posts .innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }) @@ -36,16 +37,32 @@ class PostResponseQueryServiceImpl : PostResponseQueryService { override suspend fun findByUserId( userId: Long, - since: Long, - until: Long, - minId: Long, - maxId: Long, - limit: Long, - userId2: Long + since: Long?, + until: Long?, + minId: Long?, + maxId: Long?, + limit: Int?, + userId2: Long? ): List { return Posts .innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }) .select { Posts.userId eq userId } .map { PostResponse.from(it.toPost(), it.toUser()) } } + + override suspend fun findByUserNameAndUserDomain( + name: String, + domain: String, + since: Long?, + until: Long?, + minId: Long?, + maxId: Long?, + limit: Int?, + userId: Long? + ): List { + return Posts + .innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }) + .select { Users.name eq name and (Users.domain eq domain) } + .map { PostResponse.from(it.toPost(), it.toUser()) } + } } diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt index 39f91ad1..9b331370 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt @@ -1,7 +1,6 @@ package dev.usbharu.hideout.repository import dev.usbharu.hideout.domain.model.hideout.entity.Post -import java.time.Instant @Suppress("LongParameterList") interface IPostRepository { @@ -10,35 +9,7 @@ interface IPostRepository { suspend fun findOneById(id: Long, userId: Long? = null): Post? suspend fun findByUrl(url: String): Post? suspend fun delete(id: Long) - suspend fun findAll( - since: Instant?, - until: Instant?, - minId: Long?, - maxId: Long?, - limit: Int?, - userId: Long? - ): List - suspend fun findByUserNameAndDomain( - username: String, - s: String, - since: Instant?, - until: Instant?, - minId: Long?, - maxId: Long?, - limit: Int?, - userId: Long? - ): List - - suspend fun findByUserId( - idOrNull: Long, - since: Instant?, - until: Instant?, - minId: Long?, - maxId: Long?, - limit: Int?, - userId: Long? - ): List suspend fun findByApId(id: String): Post? } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiServiceImpl.kt index b8957f87..e04bcd67 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiServiceImpl.kt @@ -3,14 +3,11 @@ package dev.usbharu.hideout.service.api import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto import dev.usbharu.hideout.domain.model.hideout.dto.PostResponse -import dev.usbharu.hideout.repository.* +import dev.usbharu.hideout.query.PostResponseQueryService +import dev.usbharu.hideout.repository.IUserRepository import dev.usbharu.hideout.service.post.IPostService import dev.usbharu.hideout.util.AcctUtil import kotlinx.coroutines.Dispatchers -import org.jetbrains.exposed.sql.and -import org.jetbrains.exposed.sql.innerJoin -import org.jetbrains.exposed.sql.select -import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.koin.core.annotation.Single import java.time.Instant @@ -19,8 +16,8 @@ import dev.usbharu.hideout.domain.model.hideout.form.Post as FormPost @Single class PostApiServiceImpl( private val postService: IPostService, - private val postRepository: IPostRepository, - private val userRepository: IUserRepository + private val userRepository: IUserRepository, + private val postResponseQueryService: PostResponseQueryService ) : IPostApiService { override suspend fun createPost(postForm: FormPost, userId: Long): PostResponse { val createdPost = postService.createLocal( @@ -41,13 +38,7 @@ class PostApiServiceImpl( suspend fun query(block: suspend () -> T): T = newSuspendedTransaction(Dispatchers.IO) { block() } - override suspend fun getById(id: Long, userId: Long?): PostResponse { - val query = query { - Posts.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { Users.id }).select { Posts.id eq id } - .single() - } - return PostResponse.from(query.toPost(), query.toUser()) - } + override suspend fun getById(id: Long, userId: Long?): PostResponse = postResponseQueryService.findById(id, userId) override suspend fun getAll( since: Instant?, @@ -56,12 +47,8 @@ class PostApiServiceImpl( maxId: Long?, limit: Int?, userId: Long? - ): List { - return query { - Posts.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }).selectAll() - .map { PostResponse.from(it.toPost(), it.toUser()) } - } - } + ): List = + postResponseQueryService.findAll(since?.toEpochMilli(), until?.toEpochMilli(), minId, maxId, limit, userId) override suspend fun getByUser( nameOrId: String, @@ -75,18 +62,9 @@ class PostApiServiceImpl( val idOrNull = nameOrId.toLongOrNull() return if (idOrNull == null) { val acct = AcctUtil.parse(nameOrId) - query { - Posts.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }).select { - Users.name.eq(acct.username) - .and(Users.domain eq (acct.domain ?: Config.configData.domain)) - }.map { PostResponse.from(it.toPost(), it.toUser()) } - } + postResponseQueryService.findByUserNameAndUserDomain(acct.username, acct.domain ?: Config.configData.domain) } else { - query { - Posts.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }).select { - Posts.userId eq idOrNull - }.map { PostResponse.from(it.toPost(), it.toUser()) } - } + postResponseQueryService.findByUserId(idOrNull) } } }