feat: InstanceServiceを追加

This commit is contained in:
usbharu 2023-11-18 10:39:37 +09:00
parent 22d6ba40ed
commit 1db8f31b5f
5 changed files with 51 additions and 3 deletions

View File

@ -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,

View File

@ -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)

View File

@ -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")

View File

@ -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?,
)

View File

@ -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
}
}