business setter configurator things

This commit is contained in:
InsanusMokrassar 2020-11-28 22:38:40 +06:00
parent 4b6544131f
commit c959da084a
6 changed files with 95 additions and 23 deletions

View File

@ -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<TriggerControlKey> = BroadcastChannel(Channel.BUFFERED)
override val unregisteredKeysFlow: Flow<TriggerControlKey> = unregisteredKeysChannel.asFlow()

View File

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

View File

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

View File

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

View File

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

View File

@ -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(
try {
unianswer(
Boolean.serializer(),
triggerSetterService.setTrigger(postId, triggerId)
)
} catch (e: TriggerSetterServiceUnknownTriggerIdException) {
call.respond(
HttpStatusCode.BadRequest,
"Unknown trigger id $triggerId"
).let { return@post }
val triggerControlKey = postKeyGenerator(postId, triggerId)
)
}
}
}
}
publishingKeyReceiver.acceptKey(postId, triggerControlKey)
call.respond(HttpStatusCode.OK)
}
fun Route.configureTriggerSetter(
subRoute: String? = triggersRootRoute,
triggerSetterService: TriggerSetterService,
unifiedRouter: UnifiedRouter
) {
subRoute ?.also {
route(it) {
configureTriggerSetter(triggerSetterService, unifiedRouter)
}
} ?: configureTriggerSetter(triggerSetterService, unifiedRouter)
}