feat: Userのマッピングを追加

This commit is contained in:
usbharu 2023-10-23 13:28:28 +09:00
parent b11dc9f7b0
commit fe38cd93ef
7 changed files with 64 additions and 16 deletions

View File

@ -19,7 +19,7 @@ class PostQueryServiceImpl(
Posts.leftJoin(PostsMedia)
.select { Posts.id eq id }
.singleOr { FailedToGetResourcesException("id: $id is duplicate or does not exist.", it) }
.let(postResultRowMapper::map)!!
.let(postResultRowMapper::map)
override suspend fun findByUrl(url: String): Post =
Posts.leftJoin(PostsMedia)

View File

@ -2,8 +2,9 @@ package dev.usbharu.hideout.query
import dev.usbharu.hideout.domain.model.hideout.entity.User
import dev.usbharu.hideout.exception.FailedToGetResourcesException
import dev.usbharu.hideout.repository.QueryMapper
import dev.usbharu.hideout.repository.ResultRowMapper
import dev.usbharu.hideout.repository.Users
import dev.usbharu.hideout.repository.toUser
import dev.usbharu.hideout.util.singleOr
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.select
@ -12,17 +13,22 @@ import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
@Repository
class UserQueryServiceImpl : UserQueryService {
class UserQueryServiceImpl(
private val userResultRowMapper: ResultRowMapper<User>,
private val userQueryMapper: QueryMapper<User>
) : UserQueryService {
private val logger = LoggerFactory.getLogger(UserQueryServiceImpl::class.java)
override suspend fun findAll(limit: Int, offset: Long): List<User> =
Users.selectAll().limit(limit, offset).map { it.toUser() }
Users.selectAll().limit(limit, offset).let(userQueryMapper::map)
override suspend fun findById(id: Long): User = Users.select { Users.id eq id }
.singleOr { FailedToGetResourcesException("id: $id is duplicate or does not exist.", it) }.toUser()
.singleOr { FailedToGetResourcesException("id: $id is duplicate or does not exist.", it) }
.let(userResultRowMapper::map)
override suspend fun findByName(name: String): List<User> = Users.select { Users.name eq name }.map { it.toUser() }
override suspend fun findByName(name: String): List<User> =
Users.select { Users.name eq name }.let(userQueryMapper::map)
override suspend fun findByNameAndDomain(name: String, domain: String): User =
Users
@ -30,17 +36,17 @@ class UserQueryServiceImpl : UserQueryService {
.singleOr {
FailedToGetResourcesException("name: $name,domain: $domain is duplicate or does not exist.", it)
}
.toUser()
.let(userResultRowMapper::map)
override suspend fun findByUrl(url: String): User {
logger.trace("findByUrl url: $url")
return Users.select { Users.url eq url }
.singleOr { FailedToGetResourcesException("url: $url is duplicate or does not exist.", it) }
.toUser()
.let(userResultRowMapper::map)
}
override suspend fun findByIds(ids: List<Long>): List<User> =
Users.select { Users.id inList ids }.map { it.toUser() }
Users.select { Users.id inList ids }.let(userQueryMapper::map)
override suspend fun existByNameAndDomain(name: String, domain: String): Boolean =
Users.select { Users.name eq name and (Users.domain eq domain) }.empty().not()
@ -48,6 +54,6 @@ class UserQueryServiceImpl : UserQueryService {
override suspend fun findByKeyId(keyId: String): User {
return Users.select { Users.keyId eq keyId }
.singleOr { FailedToGetResourcesException("keyId: $keyId is duplicate or does not exist.", it) }
.toUser()
.let(userResultRowMapper::map)
}
}

View File

@ -10,7 +10,7 @@ class PostQueryMapper(private val postResultRowMapper: ResultRowMapper<Post>) :
return query.groupBy { it[Posts.id] }
.map { it.value }
.map {
it.first().let(postResultRowMapper::map)!!
it.first().let(postResultRowMapper::map)
.copy(mediaIds = it.mapNotNull { it.getOrNull(PostsMedia.mediaId) })
}
}

View File

@ -3,5 +3,5 @@ package dev.usbharu.hideout.repository
import org.jetbrains.exposed.sql.ResultRow
interface ResultRowMapper<T> {
fun map(resultRow: ResultRow): T?
fun map(resultRow: ResultRow): T
}

View File

@ -0,0 +1,12 @@
package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.hideout.entity.User
import org.jetbrains.exposed.sql.Query
import org.springframework.stereotype.Component
@Component
class UserQueryMapper(private val userResultRowMapper: ResultRowMapper<User>) : QueryMapper<User> {
override fun map(query: Query): List<User> {
return query.map(userResultRowMapper::map)
}
}

View File

@ -9,7 +9,10 @@ import org.springframework.stereotype.Repository
import java.time.Instant
@Repository
class UserRepositoryImpl(private val idGenerateService: IdGenerateService) :
class UserRepositoryImpl(
private val idGenerateService: IdGenerateService,
private val userResultRowMapper: ResultRowMapper<User>
) :
UserRepository {
override suspend fun save(user: User): User {
@ -54,9 +57,7 @@ class UserRepositoryImpl(private val idGenerateService: IdGenerateService) :
}
override suspend fun findById(id: Long): User? {
return Users.select { Users.id eq id }.map {
it.toUser()
}.singleOrNull()
return Users.select { Users.id eq id }.singleOrNull()?.let(userResultRowMapper::map)
}
override suspend fun deleteFollowRequest(id: Long, follower: Long) {

View File

@ -0,0 +1,29 @@
package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.hideout.entity.User
import org.jetbrains.exposed.sql.ResultRow
import org.springframework.stereotype.Component
import java.time.Instant
@Component
class UserResultRowMapper(private val userBuilder: User.UserBuilder) : ResultRowMapper<User> {
override fun map(resultRow: ResultRow): User {
return userBuilder.of(
id = resultRow[Users.id],
name = resultRow[Users.name],
domain = resultRow[Users.domain],
screenName = resultRow[Users.screenName],
description = resultRow[Users.description],
password = resultRow[Users.password],
inbox = resultRow[Users.inbox],
outbox = resultRow[Users.outbox],
url = resultRow[Users.url],
publicKey = resultRow[Users.publicKey],
privateKey = resultRow[Users.privateKey],
createdAt = Instant.ofEpochMilli((resultRow[Users.createdAt])),
keyId = resultRow[Users.keyId],
followers = resultRow[Users.followers],
following = resultRow[Users.following]
)
}
}