business setter configurator things
This commit is contained in:
parent
4b6544131f
commit
c959da084a
@ -24,11 +24,11 @@ interface WritePublishingRegistrar {
|
|||||||
): Boolean
|
): Boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PublishingRegistrar : ReadPublishingRegistrar, WritePublishingRegistrar
|
interface PublishingKeysRegistrar : ReadPublishingRegistrar, WritePublishingRegistrar
|
||||||
|
|
||||||
class BusinessPublishingRegistrar(
|
class BusinessPublishingKeysRegistrar(
|
||||||
private val repo: PublishingKeysRepo
|
private val repo: PublishingKeysRepo
|
||||||
) : PublishingRegistrar {
|
) : PublishingKeysRegistrar {
|
||||||
private val unregisteredKeysChannel: BroadcastChannel<TriggerControlKey> = BroadcastChannel(Channel.BUFFERED)
|
private val unregisteredKeysChannel: BroadcastChannel<TriggerControlKey> = BroadcastChannel(Channel.BUFFERED)
|
||||||
override val unregisteredKeysFlow: Flow<TriggerControlKey> = unregisteredKeysChannel.asFlow()
|
override val unregisteredKeysFlow: Flow<TriggerControlKey> = unregisteredKeysChannel.asFlow()
|
||||||
|
|
@ -8,7 +8,7 @@ class PublishingService(
|
|||||||
private val postsRepo: PostsRepo,
|
private val postsRepo: PostsRepo,
|
||||||
private val publishedPostsRepo: PublishedPostsWriteRepo,
|
private val publishedPostsRepo: PublishedPostsWriteRepo,
|
||||||
private val keysRepo: PublishingKeysRepo
|
private val keysRepo: PublishingKeysRepo
|
||||||
) : PublishingRegistrar by BusinessPublishingRegistrar(
|
) : PublishingKeysRegistrar by BusinessPublishingKeysRegistrar(
|
||||||
keysRepo
|
keysRepo
|
||||||
), PublishingTrigger by BusinessPublishingTrigger(
|
), PublishingTrigger by BusinessPublishingTrigger(
|
||||||
postsRepo,
|
postsRepo,
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -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()
|
||||||
|
)
|
||||||
|
}
|
@ -1,3 +1,12 @@
|
|||||||
package com.insanusmokrassar.postssystem.publishing.ktor
|
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)
|
||||||
|
@ -1,33 +1,43 @@
|
|||||||
package com.insanusmokrassar.postssystem.publishing.ktor.server
|
package com.insanusmokrassar.postssystem.publishing.ktor.server
|
||||||
|
|
||||||
|
import com.insanusmokrassar.postssystem.publishing.ktor.*
|
||||||
import dev.inmo.micro_utils.ktor.server.UnifiedRouter
|
import dev.inmo.micro_utils.ktor.server.UnifiedRouter
|
||||||
import dev.inmo.postssystem.core.post.PostId
|
import dev.inmo.postssystem.core.post.PostId
|
||||||
import dev.inmo.postssystem.core.publishing.*
|
import dev.inmo.postssystem.core.publishing.*
|
||||||
import io.ktor.application.call
|
import io.ktor.application.call
|
||||||
import io.ktor.http.HttpStatusCode
|
import io.ktor.http.HttpStatusCode
|
||||||
import io.ktor.response.respond
|
import io.ktor.response.respond
|
||||||
import io.ktor.routing.Route
|
import io.ktor.routing.*
|
||||||
import io.ktor.routing.post
|
|
||||||
import kotlinx.serialization.builtins.serializer
|
import kotlinx.serialization.builtins.serializer
|
||||||
|
|
||||||
fun Route.configureTriggerSetter(
|
private inline fun Route.configureTriggerSetter(triggerSetterService: TriggerSetterService, unifiedRouter: UnifiedRouter) {
|
||||||
postKeyGenerator: PostKeyGenerator,
|
post("$setTriggerSubRoute/{post_id}/{trigger_id}") {
|
||||||
publishingKeyReceiverGetter: PublishingKeyReceiverGetter,
|
|
||||||
unifiedRouter: UnifiedRouter
|
|
||||||
) {
|
|
||||||
post("set_trigger/{post_id}/{trigger_id}") {
|
|
||||||
unifiedRouter.apply {
|
unifiedRouter.apply {
|
||||||
val postId = decodeUrlQueryValueOrSendError("post_id", PostId.serializer()) ?: return@post
|
val (postId, triggerId) = uniload(TriggerSettingData.serializer())
|
||||||
val triggerId = decodeUrlQueryValueOrSendError("trigger_id", TriggerId.serializer()) ?: return@post
|
|
||||||
|
|
||||||
val publishingKeyReceiver = publishingKeyReceiverGetter(triggerId) ?: call.respond(
|
try {
|
||||||
HttpStatusCode.BadRequest,
|
unianswer(
|
||||||
"Unknown trigger id $triggerId"
|
Boolean.serializer(),
|
||||||
).let { return@post }
|
triggerSetterService.setTrigger(postId, triggerId)
|
||||||
val triggerControlKey = postKeyGenerator(postId, triggerId)
|
)
|
||||||
|
} catch (e: TriggerSetterServiceUnknownTriggerIdException) {
|
||||||
publishingKeyReceiver.acceptKey(postId, triggerControlKey)
|
call.respond(
|
||||||
call.respond(HttpStatusCode.OK)
|
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)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user