From 1db8f31b5fd848bf22ad843e0e991935e1d93dd5 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 18 Nov 2023 10:39:37 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20InstanceService=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/domain/model/instance/Instance.kt | 2 +- .../model/instance/InstanceRepository.kt | 1 + .../InstanceRepositoryImpl.kt | 7 ++-- .../service/instance/InstanceCreateDto.kt | 11 +++++++ .../core/service/instance/InstanceService.kt | 33 +++++++++++++++++++ 5 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/dev/usbharu/hideout/core/service/instance/InstanceCreateDto.kt create mode 100644 src/main/kotlin/dev/usbharu/hideout/core/service/instance/InstanceService.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 f0599b7f..c777dcdc 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 @@ -8,7 +8,7 @@ data class Instance( val description: String, val url: String, val iconUrl: String, - val sharedInbox: String, + val sharedInbox: String?, val software: String, val version: String, val isBlocked: Boolean, diff --git a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/instance/InstanceRepository.kt b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/instance/InstanceRepository.kt index 41c35d3d..35b6026e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/domain/model/instance/InstanceRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/domain/model/instance/InstanceRepository.kt @@ -1,6 +1,7 @@ package dev.usbharu.hideout.core.domain.model.instance interface InstanceRepository { + suspend fun generateId(): Long suspend fun save(instance: Instance): Instance suspend fun findById(id: Long): Instance suspend fun delete(instance: Instance) 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 index 642fd189..a62ac486 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/InstanceRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/InstanceRepositoryImpl.kt @@ -1,5 +1,6 @@ package dev.usbharu.hideout.core.infrastructure.exposedrepository +import dev.usbharu.hideout.application.service.id.IdGenerateService import dev.usbharu.hideout.core.domain.exception.FailedToGetResourcesException import dev.usbharu.hideout.core.domain.model.instance.InstanceRepository import dev.usbharu.hideout.util.singleOr @@ -10,7 +11,9 @@ import org.springframework.stereotype.Repository import dev.usbharu.hideout.core.domain.model.instance.Instance as InstanceEntity @Repository -class InstanceRepositoryImpl : InstanceRepository { +class InstanceRepositoryImpl(private val idGenerateService: IdGenerateService) : InstanceRepository { + override suspend fun generateId(): Long = idGenerateService.generateId() + override suspend fun save(instance: InstanceEntity): InstanceEntity { if (Instance.select { Instance.id.eq(instance.id) }.firstOrNull() == null) { Instance.insert { @@ -78,7 +81,7 @@ object Instance : Table("instance") { val description = varchar("description", 5000) val url = varchar("url", 255) val iconUrl = varchar("icon_url", 255) - val sharedInbox = varchar("shared_inbox", 255) + val sharedInbox = varchar("shared_inbox", 255).nullable() val software = varchar("software", 255) val version = varchar("version", 255) val isBlocked = bool("is_blocked") diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/instance/InstanceCreateDto.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/instance/InstanceCreateDto.kt new file mode 100644 index 00000000..d5345cf0 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/instance/InstanceCreateDto.kt @@ -0,0 +1,11 @@ +package dev.usbharu.hideout.core.service.instance + +data class InstanceCreateDto( + val name: String?, + val description: String?, + val url: String, + val iconUrl: String, + val sharedInbox: String?, + val software: String?, + val version: String?, +) diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/instance/InstanceService.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/instance/InstanceService.kt new file mode 100644 index 00000000..afa182af --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/instance/InstanceService.kt @@ -0,0 +1,33 @@ +package dev.usbharu.hideout.core.service.instance + +import dev.usbharu.hideout.core.domain.model.instance.Instance +import dev.usbharu.hideout.core.domain.model.instance.InstanceRepository +import org.springframework.stereotype.Service +import java.time.Instant + +interface InstanceService { + suspend fun createNewInstance(instanceCreateDto: InstanceCreateDto): Instance +} + + +@Service +class InstanceServiceImpl(private val instanceRepository: InstanceRepository) : InstanceService { + override suspend fun createNewInstance(instanceCreateDto: InstanceCreateDto): Instance { + val instance = Instance( + instanceRepository.generateId(), + instanceCreateDto.name ?: instanceCreateDto.url, + instanceCreateDto.description ?: "", + instanceCreateDto.url, + instanceCreateDto.iconUrl, + instanceCreateDto.sharedInbox, + instanceCreateDto.software ?: "unknown", + instanceCreateDto.version ?: "unknown", + false, + false, + "", + Instant.now() + ) + instanceRepository.save(instance) + return instance + } +}