From c959da084ac38eeb076b6aae91a5afdc414c0e30 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 28 Nov 2020 22:38:40 +0600 Subject: [PATCH] business setter configurator things --- ...egistrar.kt => PublishingKeysRegistrar.kt} | 6 +-- .../core/publishing/PublishingService.kt | 2 +- .../core/publishing/TriggerSetterService.kt | 27 +++++++++++ .../ktor/client/TriggerSetterClient.kt | 26 +++++++++++ .../ktor/TriggerSetterConfiguratorRoutes.kt | 11 ++++- .../ktor/server/TriggerSetterConfigurator.kt | 46 +++++++++++-------- 6 files changed, 95 insertions(+), 23 deletions(-) rename publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/{PublishingRegistrar.kt => PublishingKeysRegistrar.kt} (90%) create mode 100644 publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/TriggerSetterService.kt create mode 100644 publishing/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/client/TriggerSetterClient.kt diff --git a/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/PublishingRegistrar.kt b/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/PublishingKeysRegistrar.kt similarity index 90% rename from publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/PublishingRegistrar.kt rename to publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/PublishingKeysRegistrar.kt index f420ea65..43a76a66 100644 --- a/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/PublishingRegistrar.kt +++ b/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/PublishingKeysRegistrar.kt @@ -24,11 +24,11 @@ interface WritePublishingRegistrar { ): Boolean } -interface PublishingRegistrar : ReadPublishingRegistrar, WritePublishingRegistrar +interface PublishingKeysRegistrar : ReadPublishingRegistrar, WritePublishingRegistrar -class BusinessPublishingRegistrar( +class BusinessPublishingKeysRegistrar( private val repo: PublishingKeysRepo -) : PublishingRegistrar { +) : PublishingKeysRegistrar { private val unregisteredKeysChannel: BroadcastChannel = BroadcastChannel(Channel.BUFFERED) override val unregisteredKeysFlow: Flow = unregisteredKeysChannel.asFlow() diff --git a/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/PublishingService.kt b/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/PublishingService.kt index bbefd81a..ee05ae04 100644 --- a/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/PublishingService.kt +++ b/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/PublishingService.kt @@ -8,7 +8,7 @@ class PublishingService( private val postsRepo: PostsRepo, private val publishedPostsRepo: PublishedPostsWriteRepo, private val keysRepo: PublishingKeysRepo -) : PublishingRegistrar by BusinessPublishingRegistrar( +) : PublishingKeysRegistrar by BusinessPublishingKeysRegistrar( keysRepo ), PublishingTrigger by BusinessPublishingTrigger( postsRepo, diff --git a/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/TriggerSetterService.kt b/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/TriggerSetterService.kt new file mode 100644 index 00000000..ce700516 --- /dev/null +++ b/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/TriggerSetterService.kt @@ -0,0 +1,27 @@ +package dev.inmo.postssystem.core.publishing + +import dev.inmo.postssystem.core.post.PostId + +class TriggerSetterServiceUnknownTriggerIdException( + triggerId: TriggerId +) : IllegalArgumentException( + "Unknown trigger id $triggerId" +) + +interface TriggerSetterService { + @Throws(TriggerSetterServiceUnknownTriggerIdException::class) + suspend fun setTrigger(postId: PostId, triggerId: TriggerId): Boolean +} + +class BusinessTriggerSetterService( + private val postKeyGenerator: PostKeyGenerator, + private val publishingKeyReceiverGetter: PublishingKeyReceiverGetter +) : TriggerSetterService { + override suspend fun setTrigger(postId: PostId, triggerId: TriggerId): Boolean { + val publishingKeyReceiver = publishingKeyReceiverGetter(triggerId) ?: throw TriggerSetterServiceUnknownTriggerIdException(triggerId) + val triggerControlKey = postKeyGenerator(postId, triggerId) + + publishingKeyReceiver.acceptKey(postId, triggerControlKey) + return true + } +} diff --git a/publishing/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/client/TriggerSetterClient.kt b/publishing/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/client/TriggerSetterClient.kt new file mode 100644 index 00000000..0cfe3ee1 --- /dev/null +++ b/publishing/ktor/client/src/commonMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/client/TriggerSetterClient.kt @@ -0,0 +1,26 @@ +package com.insanusmokrassar.postssystem.publishing.ktor.client + +import com.insanusmokrassar.postssystem.publishing.ktor.* +import dev.inmo.micro_utils.ktor.client.BodyPair +import dev.inmo.micro_utils.ktor.client.UnifiedRequester +import dev.inmo.micro_utils.ktor.common.buildStandardUrl +import dev.inmo.postssystem.core.post.PostId +import dev.inmo.postssystem.core.publishing.TriggerId +import dev.inmo.postssystem.core.publishing.TriggerSetterService +import kotlinx.serialization.builtins.serializer + +class TriggerSetterKtorClient( + private val baseUrl: String, + private val subRoute: String? = triggersRootRoute, + private val unifiedRequester: UnifiedRequester +) : TriggerSetterService { + private val apiRootUrl = subRoute ?.let { "$baseUrl/$it" } ?: baseUrl + override suspend fun setTrigger(postId: PostId, triggerId: TriggerId): Boolean = unifiedRequester.unipost( + buildStandardUrl( + apiRootUrl, + "$setTriggerSubRoute/" + ), + BodyPair(TriggerSettingData.serializer(), TriggerSettingData(postId, triggerId)), + Boolean.serializer() + ) +} diff --git a/publishing/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/TriggerSetterConfiguratorRoutes.kt b/publishing/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/TriggerSetterConfiguratorRoutes.kt index 6dabaff1..377ed769 100644 --- a/publishing/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/TriggerSetterConfiguratorRoutes.kt +++ b/publishing/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/TriggerSetterConfiguratorRoutes.kt @@ -1,3 +1,12 @@ package com.insanusmokrassar.postssystem.publishing.ktor -const val setTriggerRoute = "set_trigger" +import dev.inmo.postssystem.core.content.ContentId +import dev.inmo.postssystem.core.post.PostId +import dev.inmo.postssystem.core.publishing.TriggerId +import kotlinx.serialization.Serializable + +const val triggersRootRoute = "triggers" +const val setTriggerSubRoute = "set" + +@Serializable +data class TriggerSettingData(val postId: PostId, val triggerId: TriggerId) diff --git a/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/TriggerSetterConfigurator.kt b/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/TriggerSetterConfigurator.kt index 90e6c8a9..98abf85e 100644 --- a/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/TriggerSetterConfigurator.kt +++ b/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/TriggerSetterConfigurator.kt @@ -1,33 +1,43 @@ package com.insanusmokrassar.postssystem.publishing.ktor.server +import com.insanusmokrassar.postssystem.publishing.ktor.* import dev.inmo.micro_utils.ktor.server.UnifiedRouter import dev.inmo.postssystem.core.post.PostId import dev.inmo.postssystem.core.publishing.* import io.ktor.application.call import io.ktor.http.HttpStatusCode import io.ktor.response.respond -import io.ktor.routing.Route -import io.ktor.routing.post +import io.ktor.routing.* import kotlinx.serialization.builtins.serializer -fun Route.configureTriggerSetter( - postKeyGenerator: PostKeyGenerator, - publishingKeyReceiverGetter: PublishingKeyReceiverGetter, - unifiedRouter: UnifiedRouter -) { - post("set_trigger/{post_id}/{trigger_id}") { +private inline fun Route.configureTriggerSetter(triggerSetterService: TriggerSetterService, unifiedRouter: UnifiedRouter) { + post("$setTriggerSubRoute/{post_id}/{trigger_id}") { unifiedRouter.apply { - val postId = decodeUrlQueryValueOrSendError("post_id", PostId.serializer()) ?: return@post - val triggerId = decodeUrlQueryValueOrSendError("trigger_id", TriggerId.serializer()) ?: return@post + val (postId, triggerId) = uniload(TriggerSettingData.serializer()) - val publishingKeyReceiver = publishingKeyReceiverGetter(triggerId) ?: call.respond( - HttpStatusCode.BadRequest, - "Unknown trigger id $triggerId" - ).let { return@post } - val triggerControlKey = postKeyGenerator(postId, triggerId) - - publishingKeyReceiver.acceptKey(postId, triggerControlKey) - call.respond(HttpStatusCode.OK) + try { + unianswer( + Boolean.serializer(), + triggerSetterService.setTrigger(postId, triggerId) + ) + } catch (e: TriggerSetterServiceUnknownTriggerIdException) { + call.respond( + HttpStatusCode.BadRequest, + "Unknown trigger id $triggerId" + ) + } } } } + +fun Route.configureTriggerSetter( + subRoute: String? = triggersRootRoute, + triggerSetterService: TriggerSetterService, + unifiedRouter: UnifiedRouter +) { + subRoute ?.also { + route(it) { + configureTriggerSetter(triggerSetterService, unifiedRouter) + } + } ?: configureTriggerSetter(triggerSetterService, unifiedRouter) +}