refactor: 不要なコードを削除

This commit is contained in:
usbharu 2023-08-10 20:05:40 +09:00
parent b44d60b0d4
commit cff8640b86
4 changed files with 63 additions and 86 deletions

View File

@ -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<PostResponse>
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<PostResponse>
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<PostResponse>
}

View File

@ -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<PostResponse> {
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<PostResponse> {
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<PostResponse> {
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()) }
}
}

View File

@ -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<Post>
suspend fun findByUserNameAndDomain(
username: String,
s: String,
since: Instant?,
until: Instant?,
minId: Long?,
maxId: Long?,
limit: Int?,
userId: Long?
): List<Post>
suspend fun findByUserId(
idOrNull: Long,
since: Instant?,
until: Instant?,
minId: Long?,
maxId: Long?,
limit: Int?,
userId: Long?
): List<Post>
suspend fun findByApId(id: String): Post?
}

View File

@ -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 <T> 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<PostResponse> {
return query {
Posts.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }).selectAll()
.map { PostResponse.from(it.toPost(), it.toUser()) }
}
}
): List<PostResponse> =
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)
}
}
}