refactor: Uri型のColumnTypeを作成して型安全性を向上

This commit is contained in:
usbharu 2024-09-11 08:18:29 +09:00
parent 34a36b5ed5
commit d9297b0992
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
2 changed files with 17 additions and 14 deletions

View File

@ -17,6 +17,7 @@
package dev.usbharu.hideout.core.infrastructure.exposedrepository package dev.usbharu.hideout.core.infrastructure.exposedrepository
import dev.usbharu.hideout.core.domain.model.instance.* import dev.usbharu.hideout.core.domain.model.instance.*
import dev.usbharu.hideout.core.infrastructure.exposed.uri
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.javatime.timestamp import org.jetbrains.exposed.sql.javatime.timestamp
@ -37,9 +38,9 @@ class InstanceRepositoryImpl : InstanceRepository,
it[id] = instance.id.instanceId it[id] = instance.id.instanceId
it[name] = instance.name.name it[name] = instance.name.name
it[description] = instance.description.description it[description] = instance.description.description
it[url] = instance.url.toString() it[url] = instance.url
it[iconUrl] = instance.iconUrl.toString() it[iconUrl] = instance.iconUrl
it[sharedInbox] = instance.sharedInbox?.toString() it[sharedInbox] = instance.sharedInbox
it[software] = instance.software.software it[software] = instance.software.software
it[version] = instance.version.version it[version] = instance.version.version
it[isBlocked] = instance.isBlocked it[isBlocked] = instance.isBlocked
@ -61,7 +62,7 @@ class InstanceRepositoryImpl : InstanceRepository,
} }
override suspend fun findByUrl(url: URI): dev.usbharu.hideout.core.domain.model.instance.Instance? = query { override suspend fun findByUrl(url: URI): dev.usbharu.hideout.core.domain.model.instance.Instance? = query {
return@query Instance.selectAll().where { Instance.url eq url.toString() }.limit(1).singleOrNull()?.toInstance() return@query Instance.selectAll().where { Instance.url eq url }.limit(1).singleOrNull()?.toInstance()
} }
companion object { companion object {
@ -74,9 +75,9 @@ fun ResultRow.toInstance(): InstanceEntity {
id = InstanceId(this[Instance.id]), id = InstanceId(this[Instance.id]),
name = InstanceName(this[Instance.name]), name = InstanceName(this[Instance.name]),
description = InstanceDescription(this[Instance.description]), description = InstanceDescription(this[Instance.description]),
url = URI.create(this[Instance.url]), url = this[Instance.url],
iconUrl = URI.create(this[Instance.iconUrl]), iconUrl = this[Instance.iconUrl],
sharedInbox = this[Instance.sharedInbox]?.let { URI.create(it) }, sharedInbox = this[Instance.sharedInbox],
software = InstanceSoftware(this[Instance.software]), software = InstanceSoftware(this[Instance.software]),
version = InstanceVersion(this[Instance.version]), version = InstanceVersion(this[Instance.version]),
isBlocked = this[Instance.isBlocked], isBlocked = this[Instance.isBlocked],
@ -90,9 +91,9 @@ object Instance : Table("instance") {
val id = long("id") val id = long("id")
val name = varchar("name", 1000) val name = varchar("name", 1000)
val description = varchar("description", 5000) val description = varchar("description", 5000)
val url = varchar("url", 255).uniqueIndex() val url = uri("url", 255).uniqueIndex()
val iconUrl = varchar("icon_url", 255) val iconUrl = uri("icon_url", 255)
val sharedInbox = varchar("shared_inbox", 255).nullable().uniqueIndex() val sharedInbox = uri("shared_inbox", 255).nullable().uniqueIndex()
val software = varchar("software", 255) val software = varchar("software", 255)
val version = varchar("version", 255) val version = varchar("version", 255)
val isBlocked = bool("is_blocked") val isBlocked = bool("is_blocked")

View File

@ -44,8 +44,9 @@ class InstanceRepositoryImplTest : AbstractRepositoryTest(InstanceTable) {
val table = assertTable val table = assertTable
assertThat(table).row(1).isEqualTo(InstanceTable.id, 1).isEqualTo(InstanceTable.name, "test") assertThat(table).row(1).isEqualTo(InstanceTable.id, 1).isEqualTo(InstanceTable.name, "test")
.isEqualTo(InstanceTable.url, "https://www.example.com") .value(InstanceTable.url).isEqualTo("https://www.example.com")
.isEqualTo(InstanceTable.iconUrl, "https://www.example.com").isEqualTo(InstanceTable.sharedInbox, null) .value(InstanceTable.iconUrl).isEqualTo("https://www.example.com")
.isEqualTo(InstanceTable.sharedInbox, null)
.isEqualTo(InstanceTable.software, "").isEqualTo(InstanceTable.version, "") .isEqualTo(InstanceTable.software, "").isEqualTo(InstanceTable.version, "")
.isEqualTo(InstanceTable.isBlocked, false).isEqualTo(InstanceTable.isMuted, false) .isEqualTo(InstanceTable.isBlocked, false).isEqualTo(InstanceTable.isMuted, false)
.isEqualTo(InstanceTable.moderationNote, "").value(InstanceTable.createdAt) .isEqualTo(InstanceTable.moderationNote, "").value(InstanceTable.createdAt)
@ -96,8 +97,9 @@ class InstanceRepositoryImplTest : AbstractRepositoryTest(InstanceTable) {
val table = assertTable val table = assertTable
assertThat(table).row(1).isEqualTo(InstanceTable.id, 1).isEqualTo(InstanceTable.name, "test") assertThat(table).row(1).isEqualTo(InstanceTable.id, 1).isEqualTo(InstanceTable.name, "test")
.isEqualTo(InstanceTable.url, "https://www.example.com") .value(InstanceTable.url).isEqualTo("https://www.example.com")
.isEqualTo(InstanceTable.iconUrl, "https://www.example.com").isEqualTo(InstanceTable.sharedInbox, null) .value(InstanceTable.iconUrl).isEqualTo("https://www.example.com")
.isEqualTo(InstanceTable.sharedInbox, null)
.isEqualTo(InstanceTable.software, "").isEqualTo(InstanceTable.version, "") .isEqualTo(InstanceTable.software, "").isEqualTo(InstanceTable.version, "")
.isEqualTo(InstanceTable.isBlocked, false).isEqualTo(InstanceTable.isMuted, false) .isEqualTo(InstanceTable.isBlocked, false).isEqualTo(InstanceTable.isMuted, false)
.isEqualTo(InstanceTable.moderationNote, "").value(InstanceTable.createdAt) .isEqualTo(InstanceTable.moderationNote, "").value(InstanceTable.createdAt)