refactor: updateを非推奨にしてcreateをsaveに変更

This commit is contained in:
usbharu 2023-04-28 11:00:58 +09:00
parent cabecd4fce
commit 48f455d4bc
7 changed files with 16 additions and 18 deletions

View File

@ -3,7 +3,7 @@ package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.User
interface IUserRepository {
suspend fun create(user: User): User
suspend fun save(user: User): User
suspend fun findById(id: Long): User?
@ -17,7 +17,8 @@ interface IUserRepository {
suspend fun findByUrls(urls: List<String>): List<User>
suspend fun update(userEntity: User)
@Deprecated("", ReplaceWith("save(userEntity)"))
suspend fun update(userEntity: User) = save(userEntity)
suspend fun delete(id: Long)

View File

@ -1,15 +1,13 @@
package dev.usbharu.hideout.repository
import dev.usbharu.hideout.domain.model.*
import dev.usbharu.hideout.domain.model.User
import dev.usbharu.hideout.domain.model.UsersFollowers
import kotlinx.coroutines.Dispatchers
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZoneOffset
class UserRepository(private val database: Database) : IUserRepository {
init {
@ -21,12 +19,13 @@ class UserRepository(private val database: Database) : IUserRepository {
}
}
@Deprecated("", ReplaceWith("toUser()"))
private fun ResultRow.toUserEntity(): User = toUser()
suspend fun <T> query(block: suspend () -> T): T =
newSuspendedTransaction(Dispatchers.IO) { block() }
override suspend fun create(user: User): User {
override suspend fun save(user: User): User {
return query {
Users.insert {
it[id] = user.id

View File

@ -10,7 +10,6 @@ import java.security.*
import java.security.interfaces.RSAPrivateKey
import java.security.interfaces.RSAPublicKey
import java.time.Instant
import java.time.LocalDateTime
import java.util.*
class UserAuthService(
@ -43,7 +42,7 @@ class UserAuthService(
publicKey = "",
createdAt = Instant.now(),
)
val createdUser = userRepository.create(registerUser)
val createdUser = userRepository.save(registerUser)
val keyPair = generateKeyPair()
val privateKey = keyPair.private as RSAPrivateKey

View File

@ -42,7 +42,7 @@ class UserService(private val userRepository: IUserRepository) {
}
suspend fun create(user: User): User {
return userRepository.create(user)
return userRepository.save(user)
}
suspend fun findFollowersById(id: Long): List<User> {

View File

@ -18,7 +18,7 @@ class ActivityPubKtTest {
fun HttpSignTest(): Unit = runBlocking {
val ktorKeyMap = KtorKeyMap(object : IUserRepository {
override suspend fun create(user: User): User {
override suspend fun save(user: User): User {
TODO("Not yet implemented")
}

View File

@ -13,7 +13,7 @@ class KtorKeyMapTest {
@Test
fun getPrivateKey() {
val ktorKeyMap = KtorKeyMap(object : IUserRepository {
override suspend fun create(user: User): User {
override suspend fun save(user: User): User {
TODO("Not yet implemented")
}

View File

@ -16,7 +16,6 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.time.Clock
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
@ -45,7 +44,7 @@ class UserRepositoryTest {
@Test
fun `findFollowersById フォロワー一覧を取得`() = runTest {
val userRepository = UserRepository(db)
val user = userRepository.create(
val user = userRepository.save(
User(
id = 0L,
name = "test",
@ -60,7 +59,7 @@ class UserRepositoryTest {
createdAt = Instant.now(Clock.tickMillis(ZoneId.systemDefault()))
)
)
val follower = userRepository.create(
val follower = userRepository.save(
User(
id = 1L,
name = "follower",
@ -75,7 +74,7 @@ class UserRepositoryTest {
createdAt = Instant.now(Clock.tickMillis(ZoneId.systemDefault()))
)
)
val follower2 = userRepository.create(
val follower2 = userRepository.save(
User(
id = 3L,
name = "follower2",
@ -101,7 +100,7 @@ class UserRepositoryTest {
@Test
fun `createFollower フォロワー追加`() = runTest {
val userRepository = UserRepository(db)
val user = userRepository.create(
val user = userRepository.save(
User(0L,
"test",
"example.com",
@ -115,7 +114,7 @@ class UserRepositoryTest {
createdAt = Instant.now()
)
)
val follower = userRepository.create(
val follower = userRepository.save(
User(1L,
"follower",
"follower.example.com",