From 22d6ba40ed011a1a8db2fad7c2530844b78415a6 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 18 Nov 2023 00:51:57 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Instance=E3=81=AE=E3=83=AA=E3=83=9D?= =?UTF-8?q?=E3=82=B8=E3=83=88=E3=83=AA=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/domain/model/instance/Instance.kt | 7 +- .../InstanceRepositoryImpl.kt | 88 +++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/InstanceRepositoryImpl.kt diff --git a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/instance/Instance.kt b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/instance/Instance.kt index 1753ec00..f0599b7f 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/instance/Instance.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/instance/Instance.kt @@ -1,5 +1,7 @@ package dev.usbharu.hideout.core.domain.model.instance +import java.time.Instant + data class Instance( val id: Long, val name: String, @@ -10,6 +12,7 @@ data class Instance( val software: String, val version: String, val isBlocked: Boolean, - val isMuting: Boolean, - val moderationNote: String + val isMuted: Boolean, + val moderationNote: String, + val createdAt: Instant ) diff --git a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/InstanceRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/InstanceRepositoryImpl.kt new file mode 100644 index 00000000..642fd189 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/InstanceRepositoryImpl.kt @@ -0,0 +1,88 @@ +package dev.usbharu.hideout.core.infrastructure.exposedrepository + +import dev.usbharu.hideout.core.domain.exception.FailedToGetResourcesException +import dev.usbharu.hideout.core.domain.model.instance.InstanceRepository +import dev.usbharu.hideout.util.singleOr +import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq +import org.jetbrains.exposed.sql.javatime.timestamp +import org.springframework.stereotype.Repository +import dev.usbharu.hideout.core.domain.model.instance.Instance as InstanceEntity + +@Repository +class InstanceRepositoryImpl : InstanceRepository { + override suspend fun save(instance: InstanceEntity): InstanceEntity { + if (Instance.select { Instance.id.eq(instance.id) }.firstOrNull() == null) { + Instance.insert { + it[id] = instance.id + it[name] = instance.name + it[description] = instance.description + it[url] = instance.url + it[iconUrl] = instance.iconUrl + it[sharedInbox] = instance.sharedInbox + it[software] = instance.software + it[version] = instance.version + it[isBlocked] = instance.isBlocked + it[isMuted] = instance.isMuted + it[moderationNote] = instance.moderationNote + it[createdAt] = instance.createdAt + } + } else { + Instance.update({ Instance.id eq instance.id }) { + it[name] = instance.name + it[description] = instance.description + it[url] = instance.url + it[iconUrl] = instance.iconUrl + it[sharedInbox] = instance.sharedInbox + it[software] = instance.software + it[version] = instance.version + it[isBlocked] = instance.isBlocked + it[isMuted] = instance.isMuted + it[moderationNote] = instance.moderationNote + it[createdAt] = instance.createdAt + } + } + return instance + } + + override suspend fun findById(id: Long): InstanceEntity { + return Instance.select { Instance.id eq id } + .singleOr { FailedToGetResourcesException("id: $id doesn't exist.") }.toInstance() + } + + override suspend fun delete(instance: InstanceEntity) { + Instance.deleteWhere { Instance.id eq instance.id } + } +} + +fun ResultRow.toInstance(): InstanceEntity { + return InstanceEntity( + id = this[Instance.id], + name = this[Instance.name], + description = this[Instance.description], + url = this[Instance.url], + iconUrl = this[Instance.iconUrl], + sharedInbox = this[Instance.sharedInbox], + software = this[Instance.software], + version = this[Instance.version], + isBlocked = this[Instance.isBlocked], + isMuted = this[Instance.isMuted], + moderationNote = this[Instance.moderationNote], + createdAt = this[Instance.createdAt] + ) +} + +object Instance : Table("instance") { + val id = long("id") + val name = varchar("name", 1000) + val description = varchar("description", 5000) + val url = varchar("url", 255) + val iconUrl = varchar("icon_url", 255) + val sharedInbox = varchar("shared_inbox", 255) + val software = varchar("software", 255) + val version = varchar("version", 255) + val isBlocked = bool("is_blocked") + val isMuted = bool("is_muted") + val moderationNote = varchar("moderation_note", 10000) + val createdAt = timestamp("created_at") +}