feat: PostResponseのQueryServiceを追加

This commit is contained in:
usbharu 2023-08-10 19:55:46 +09:00
parent ab73da01a7
commit b44d60b0d4
2 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,26 @@
package dev.usbharu.hideout.query
import dev.usbharu.hideout.domain.model.hideout.dto.PostResponse
@Suppress("LongParameterList")
interface PostResponseQueryService {
suspend fun findById(id: Long, userId: Long): PostResponse
suspend fun findAll(
since: Long,
until: Long,
minId: Long,
maxId: Long,
limit: Long,
userId: Long
): List<PostResponse>
suspend fun findByUserId(
userId: Long,
since: Long,
until: Long,
minId: Long,
maxId: Long,
limit: Long,
userId2: Long
): List<PostResponse>
}

View File

@ -0,0 +1,51 @@
package dev.usbharu.hideout.query
import dev.usbharu.hideout.domain.model.hideout.dto.PostResponse
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.innerJoin
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.koin.core.annotation.Single
@Single
class PostResponseQueryServiceImpl : PostResponseQueryService {
override suspend fun findById(id: Long, userId: Long): PostResponse {
return Posts
.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { Users.id })
.select { Posts.id eq id }
.single()
.let { PostResponse.from(it.toPost(), it.toUser()) }
}
override suspend fun findAll(
since: Long,
until: Long,
minId: Long,
maxId: Long,
limit: Long,
userId: Long
): List<PostResponse> {
return Posts
.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id })
.selectAll()
.map { PostResponse.from(it.toPost(), it.toUser()) }
}
override suspend fun findByUserId(
userId: Long,
since: Long,
until: Long,
minId: Long,
maxId: Long,
limit: Long,
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()) }
}
}