diff --git a/src/main/kotlin/dev/usbharu/hideout/query/PostResponseQueryService.kt b/src/main/kotlin/dev/usbharu/hideout/query/PostResponseQueryService.kt new file mode 100644 index 00000000..9386a4ce --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/query/PostResponseQueryService.kt @@ -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 + + suspend fun findByUserId( + userId: Long, + since: Long, + until: Long, + minId: Long, + maxId: Long, + limit: Long, + userId2: Long + ): List +} diff --git a/src/main/kotlin/dev/usbharu/hideout/query/PostResponseQueryServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/query/PostResponseQueryServiceImpl.kt new file mode 100644 index 00000000..858aa05f --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/query/PostResponseQueryServiceImpl.kt @@ -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 { + 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 { + return Posts + .innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }) + .select { Posts.userId eq userId } + .map { PostResponse.from(it.toPost(), it.toUser()) } + } +}