diff --git a/src/main/kotlin/dev/usbharu/hideout/query/UserQueryService.kt b/src/main/kotlin/dev/usbharu/hideout/query/UserQueryService.kt index 2da0512d..eec057f4 100644 --- a/src/main/kotlin/dev/usbharu/hideout/query/UserQueryService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/query/UserQueryService.kt @@ -3,8 +3,10 @@ package dev.usbharu.hideout.query import dev.usbharu.hideout.domain.model.hideout.entity.User interface UserQueryService { + suspend fun findAll(limit: Int, offset: Long): List suspend fun findById(id: Long): User suspend fun findByName(name: String): List suspend fun findByNameAndDomain(name: String, domain: String): User suspend fun findByUrl(url: String): User + suspend fun findByIds(ids: List): List } diff --git a/src/main/kotlin/dev/usbharu/hideout/query/UserQueryServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/query/UserQueryServiceImpl.kt index d51938d6..e96dea20 100644 --- a/src/main/kotlin/dev/usbharu/hideout/query/UserQueryServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/query/UserQueryServiceImpl.kt @@ -5,19 +5,23 @@ import dev.usbharu.hideout.repository.Users import dev.usbharu.hideout.repository.toUser import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.select +import org.jetbrains.exposed.sql.selectAll +import org.koin.core.annotation.Single +@Single class UserQueryServiceImpl : UserQueryService { + override suspend fun findAll(limit: Int, offset: Long): List = + Users.selectAll().limit(limit, offset).map { it.toUser() } + override suspend fun findById(id: Long): User = Users.select { Users.id eq id }.single().toUser() - override suspend fun findByName(name: String): List { - return Users.select { Users.name eq name }.map { it.toUser() } - } + override suspend fun findByName(name: String): List = Users.select { Users.name eq name }.map { it.toUser() } - override suspend fun findByNameAndDomain(name: String, domain: String): User { - return Users.select { Users.name eq name and (Users.domain eq domain) }.single().toUser() - } + override suspend fun findByNameAndDomain(name: String, domain: String): User = + Users.select { Users.name eq name and (Users.domain eq domain) }.single().toUser() - override suspend fun findByUrl(url: String): User { - return Users.select { Users.url eq url }.single().toUser() - } + override suspend fun findByUrl(url: String): User = Users.select { Users.url eq url }.single().toUser() + + override suspend fun findByIds(ids: List): List = + Users.select { Users.id inList ids }.map { it.toUser() } } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/api/IUserApiService.kt b/src/main/kotlin/dev/usbharu/hideout/service/api/IUserApiService.kt index 17d548f1..580d4115 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/api/IUserApiService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/api/IUserApiService.kt @@ -12,8 +12,6 @@ interface IUserApiService { suspend fun findByAcct(acct: Acct): UserResponse - suspend fun findByAccts(accts: List): List - suspend fun findFollowers(userId: Long): List suspend fun findFollowings(userId: Long): List diff --git a/src/main/kotlin/dev/usbharu/hideout/service/api/UserApiServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/api/UserApiServiceImpl.kt index b5347983..70fc8bd3 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/api/UserApiServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/api/UserApiServiceImpl.kt @@ -3,36 +3,38 @@ package dev.usbharu.hideout.service.api import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.domain.model.Acct import dev.usbharu.hideout.domain.model.hideout.dto.UserResponse -import dev.usbharu.hideout.service.user.IUserService +import dev.usbharu.hideout.query.FollowerQueryService +import dev.usbharu.hideout.query.UserQueryService import org.koin.core.annotation.Single +import kotlin.math.min @Single -class UserApiServiceImpl(private val userService: IUserService) : IUserApiService { +class UserApiServiceImpl( + private val userQueryService: UserQueryService, + private val followerQueryService: FollowerQueryService +) : IUserApiService { override suspend fun findAll(limit: Int?, offset: Long): List = - userService.findAll(limit, offset).map { UserResponse.from(it) } + userQueryService.findAll(min(limit ?: 100, 100), offset).map { UserResponse.from(it) } - override suspend fun findById(id: Long): UserResponse = UserResponse.from(userService.findById(id)) + override suspend fun findById(id: Long): UserResponse = UserResponse.from(userQueryService.findById(id)) override suspend fun findByIds(ids: List): List = - userService.findByIds(ids).map { UserResponse.from(it) } + userQueryService.findByIds(ids).map { UserResponse.from(it) } override suspend fun findByAcct(acct: Acct): UserResponse = - UserResponse.from(userService.findByNameAndDomain(acct.username, acct.domain)) - - override suspend fun findByAccts(accts: List): List { - return userService.findByNameAndDomains(accts.map { it.username to (it.domain ?: Config.configData.domain) }) - .map { UserResponse.from(it) } - } + UserResponse.from(userQueryService.findByNameAndDomain(acct.username, acct.domain ?: Config.configData.domain)) override suspend fun findFollowers(userId: Long): List = - userService.findFollowersById(userId).map { UserResponse.from(it) } + followerQueryService.findFollowersById(userId).map { UserResponse.from(it) } override suspend fun findFollowings(userId: Long): List = - userService.findFollowingById(userId).map { UserResponse.from(it) } + followerQueryService.findFollowingById(userId).map { UserResponse.from(it) } override suspend fun findFollowersByAcct(acct: Acct): List = - userService.findFollowersByNameAndDomain(acct.username, acct.domain).map { UserResponse.from(it) } + followerQueryService.findFollowersByNameAndDomain(acct.username, acct.domain ?: Config.configData.domain) + .map { UserResponse.from(it) } override suspend fun findFollowingsByAcct(acct: Acct): List = - userService.findFollowingByNameAndDomain(acct.username, acct.domain).map { UserResponse.from(it) } + followerQueryService.findFollowingByNameAndDomain(acct.username, acct.domain ?: Config.configData.domain) + .map { UserResponse.from(it) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/user/IUserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/user/IUserService.kt index eb806eb1..f247b30a 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/user/IUserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/user/IUserService.kt @@ -10,16 +10,8 @@ interface IUserService { suspend fun findById(id: Long): User - suspend fun findByIds(ids: List): List - - suspend fun findByName(name: String): List - suspend fun findByNameLocalUser(name: String): User - suspend fun findByNameAndDomain(name: String, domain: String? = null): User - - suspend fun findByNameAndDomains(names: List>): List - suspend fun findByUrl(url: String): User suspend fun findByUrls(urls: List): List @@ -32,12 +24,6 @@ interface IUserService { suspend fun findFollowersById(id: Long): List - suspend fun findFollowersByNameAndDomain(name: String, domain: String?): List - - suspend fun findFollowingById(id: Long): List - - suspend fun findFollowingByNameAndDomain(name: String, domain: String?): List - /** * フォローリクエストを送信する * diff --git a/src/main/kotlin/dev/usbharu/hideout/service/user/UserService.kt b/src/main/kotlin/dev/usbharu/hideout/service/user/UserService.kt index bf531e74..024a3659 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/user/UserService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/user/UserService.kt @@ -31,23 +31,11 @@ class UserService( override suspend fun findById(id: Long): User = userRepository.findById(id) ?: throw UserNotFoundException("$id was not found.") - override suspend fun findByIds(ids: List): List = userRepository.findByIds(ids) - - override suspend fun findByName(name: String): List = userRepository.findByName(name) - override suspend fun findByNameLocalUser(name: String): User { return userRepository.findByNameAndDomain(name, Config.configData.domain) ?: throw UserNotFoundException("$name was not found.") } - override suspend fun findByNameAndDomain(name: String, domain: String?): User { - return userRepository.findByNameAndDomain(name, domain ?: Config.configData.domain) - ?: throw UserNotFoundException("$name was not found.") - } - - override suspend fun findByNameAndDomains(names: List>): List = - userRepository.findByNameAndDomains(names) - override suspend fun findByUrl(url: String): User = userRepository.findByUrl(url) ?: throw UserNotFoundException("$url was not found.") @@ -97,17 +85,6 @@ class UserService( } override suspend fun findFollowersById(id: Long): List = userRepository.findFollowersById(id) - override suspend fun findFollowersByNameAndDomain(name: String, domain: String?): List { - TODO("Not yet implemented") - } - - override suspend fun findFollowingById(id: Long): List { - TODO("Not yet implemented") - } - - override suspend fun findFollowingByNameAndDomain(name: String, domain: String?): List { - TODO("Not yet implemented") - } // TODO APのフォロー処理を作る override suspend fun followRequest(id: Long, followerId: Long): Boolean {