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") @Suppress("LongParameterList")
interface PostResponseQueryService { interface PostResponseQueryService {
suspend fun findById(id: Long, userId: Long): PostResponse suspend fun findById(id: Long, userId: Long?): PostResponse
suspend fun findAll( suspend fun findAll(
since: Long, since: Long? = null,
until: Long, until: Long? = null,
minId: Long, minId: Long? = null,
maxId: Long, maxId: Long? = null,
limit: Long, limit: Int? = null,
userId: Long userId: Long? = null
): List<PostResponse> ): List<PostResponse>
suspend fun findByUserId( suspend fun findByUserId(
userId: Long, userId: Long,
since: Long, since: Long? = null,
until: Long, until: Long? = null,
minId: Long, minId: Long? = null,
maxId: Long, maxId: Long? = null,
limit: Long, limit: Int? = null,
userId2: Long 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> ): 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.Users
import dev.usbharu.hideout.repository.toPost import dev.usbharu.hideout.repository.toPost
import dev.usbharu.hideout.repository.toUser import dev.usbharu.hideout.repository.toUser
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.innerJoin import org.jetbrains.exposed.sql.innerJoin
import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.selectAll
@ -12,7 +13,7 @@ import org.koin.core.annotation.Single
@Single @Single
class PostResponseQueryServiceImpl : PostResponseQueryService { class PostResponseQueryServiceImpl : PostResponseQueryService {
override suspend fun findById(id: Long, userId: Long): PostResponse { override suspend fun findById(id: Long, userId: Long?): PostResponse {
return Posts return Posts
.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { Users.id }) .innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { Users.id })
.select { Posts.id eq id } .select { Posts.id eq id }
@ -21,12 +22,12 @@ class PostResponseQueryServiceImpl : PostResponseQueryService {
} }
override suspend fun findAll( override suspend fun findAll(
since: Long, since: Long?,
until: Long, until: Long?,
minId: Long, minId: Long?,
maxId: Long, maxId: Long?,
limit: Long, limit: Int?,
userId: Long userId: Long?
): List<PostResponse> { ): List<PostResponse> {
return Posts return Posts
.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }) .innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id })
@ -36,16 +37,32 @@ class PostResponseQueryServiceImpl : PostResponseQueryService {
override suspend fun findByUserId( override suspend fun findByUserId(
userId: Long, userId: Long,
since: Long, since: Long?,
until: Long, until: Long?,
minId: Long, minId: Long?,
maxId: Long, maxId: Long?,
limit: Long, limit: Int?,
userId2: Long userId2: Long?
): List<PostResponse> { ): List<PostResponse> {
return Posts return Posts
.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }) .innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id })
.select { Posts.userId eq userId } .select { Posts.userId eq userId }
.map { PostResponse.from(it.toPost(), it.toUser()) } .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 package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.hideout.entity.Post import dev.usbharu.hideout.domain.model.hideout.entity.Post
import java.time.Instant
@Suppress("LongParameterList") @Suppress("LongParameterList")
interface IPostRepository { interface IPostRepository {
@ -10,35 +9,7 @@ interface IPostRepository {
suspend fun findOneById(id: Long, userId: Long? = null): Post? suspend fun findOneById(id: Long, userId: Long? = null): Post?
suspend fun findByUrl(url: String): Post? suspend fun findByUrl(url: String): Post?
suspend fun delete(id: Long) 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? 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.config.Config
import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto
import dev.usbharu.hideout.domain.model.hideout.dto.PostResponse 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.service.post.IPostService
import dev.usbharu.hideout.util.AcctUtil import dev.usbharu.hideout.util.AcctUtil
import kotlinx.coroutines.Dispatchers 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.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.koin.core.annotation.Single import org.koin.core.annotation.Single
import java.time.Instant import java.time.Instant
@ -19,8 +16,8 @@ import dev.usbharu.hideout.domain.model.hideout.form.Post as FormPost
@Single @Single
class PostApiServiceImpl( class PostApiServiceImpl(
private val postService: IPostService, private val postService: IPostService,
private val postRepository: IPostRepository, private val userRepository: IUserRepository,
private val userRepository: IUserRepository private val postResponseQueryService: PostResponseQueryService
) : IPostApiService { ) : IPostApiService {
override suspend fun createPost(postForm: FormPost, userId: Long): PostResponse { override suspend fun createPost(postForm: FormPost, userId: Long): PostResponse {
val createdPost = postService.createLocal( val createdPost = postService.createLocal(
@ -41,13 +38,7 @@ class PostApiServiceImpl(
suspend fun <T> query(block: suspend () -> T): T = suspend fun <T> query(block: suspend () -> T): T =
newSuspendedTransaction(Dispatchers.IO) { block() } newSuspendedTransaction(Dispatchers.IO) { block() }
override suspend fun getById(id: Long, userId: Long?): PostResponse { override suspend fun getById(id: Long, userId: Long?): PostResponse = postResponseQueryService.findById(id, userId)
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 getAll( override suspend fun getAll(
since: Instant?, since: Instant?,
@ -56,12 +47,8 @@ class PostApiServiceImpl(
maxId: Long?, maxId: Long?,
limit: Int?, limit: Int?,
userId: Long? userId: Long?
): List<PostResponse> { ): List<PostResponse> =
return query { postResponseQueryService.findAll(since?.toEpochMilli(), until?.toEpochMilli(), minId, maxId, limit, userId)
Posts.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }).selectAll()
.map { PostResponse.from(it.toPost(), it.toUser()) }
}
}
override suspend fun getByUser( override suspend fun getByUser(
nameOrId: String, nameOrId: String,
@ -75,18 +62,9 @@ class PostApiServiceImpl(
val idOrNull = nameOrId.toLongOrNull() val idOrNull = nameOrId.toLongOrNull()
return if (idOrNull == null) { return if (idOrNull == null) {
val acct = AcctUtil.parse(nameOrId) val acct = AcctUtil.parse(nameOrId)
query { postResponseQueryService.findByUserNameAndUserDomain(acct.username, acct.domain ?: Config.configData.domain)
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()) }
}
} else { } else {
query { postResponseQueryService.findByUserId(idOrNull)
Posts.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }).select {
Posts.userId eq idOrNull
}.map { PostResponse.from(it.toPost(), it.toUser()) }
}
} }
} }
} }