add TriggersToPostsRepo

This commit is contained in:
InsanusMokrassar 2020-11-28 23:05:01 +06:00
parent c959da084a
commit 72c48794fc
5 changed files with 70 additions and 12 deletions

View File

@ -22,6 +22,9 @@ interface WritePublishingRegistrar {
key: TriggerControlKey,
postId: PostId
): Boolean
suspend fun unregisterTriggerForPost(
postId: PostId
): Boolean
}
interface PublishingKeysRegistrar : ReadPublishingRegistrar, WritePublishingRegistrar
@ -48,5 +51,17 @@ class BusinessPublishingKeysRegistrar(
}
}
}
override suspend fun unregisterTriggerForPost(
postId: PostId
): Boolean = repo.getTriggerControlKeyByPostId(
postId
).let { previousKey ->
repo.unsetPostTriggerControlKey(postId).also {
if (it && previousKey != null) {
unregisteredKeysChannel.send(previousKey)
}
}
}
}

View File

@ -8,19 +8,32 @@ class TriggerSetterServiceUnknownTriggerIdException(
"Unknown trigger id $triggerId"
)
interface TriggerSetterService {
interface ReadTriggersManagementService {
@Throws(TriggerSetterServiceUnknownTriggerIdException::class)
suspend fun getTrigger(postId: PostId): TriggerId?
}
interface WriteTriggersManagementService {
@Throws(TriggerSetterServiceUnknownTriggerIdException::class)
suspend fun setTrigger(postId: PostId, triggerId: TriggerId): Boolean
}
class BusinessTriggerSetterService(
interface TriggersManagementService : ReadTriggersManagementService, WriteTriggersManagementService
class BusinessTriggersManagementService(
private val postKeyGenerator: PostKeyGenerator,
private val publishingKeyReceiverGetter: PublishingKeyReceiverGetter
) : TriggerSetterService {
private val publishingKeyReceiverGetter: PublishingKeyReceiverGetter,
private val keysRegistrar: PublishingKeysRegistrar
) : TriggersManagementService {
override suspend fun getTrigger(postId: PostId): TriggerId? {
TODO("Not yet implemented")
}
override suspend fun setTrigger(postId: PostId, triggerId: TriggerId): Boolean {
val publishingKeyReceiver = publishingKeyReceiverGetter(triggerId) ?: throw TriggerSetterServiceUnknownTriggerIdException(triggerId)
val triggerControlKey = postKeyGenerator(postId, triggerId)
keysRegistrar.registerTriggerForPost(triggerControlKey, postId)
publishingKeyReceiver.acceptKey(postId, triggerControlKey)
return true
}

View File

@ -0,0 +1,31 @@
package dev.inmo.postssystem.core.publishing.repos
import dev.inmo.micro_utils.repos.*
import dev.inmo.postssystem.core.post.PostId
import dev.inmo.postssystem.core.publishing.*
interface ReadTriggersToPostsRepo : ReadKeyValueRepo<PostId, TriggerId>
interface WriteTriggersToPostsRepo : WriteKeyValueRepo<PostId, TriggerId>
interface TriggersToPostsRepo : KeyValueRepo<PostId, TriggerId>, WriteTriggersToPostsRepo, ReadTriggersToPostsRepo
fun TriggersToPostsRepo(
keyValueRepo: KeyValueRepo<PostId, TriggerId>,
generator: PostKeyGenerator,
writePublishingKeysRegistrar: WritePublishingRegistrar
): TriggersToPostsRepo = object : TriggersToPostsRepo, KeyValueRepo<PostId, TriggerId> by keyValueRepo {
override suspend fun set(toSet: Map<PostId, TriggerId>) {
keyValueRepo.set(
toSet.filter { (postId, triggerId) ->
val publishingKey = generator(postId, triggerId)
writePublishingKeysRegistrar.registerTriggerForPost(publishingKey, postId)
}
)
}
override suspend fun unset(toUnset: List<PostId>) {
toUnset.forEach {
writePublishingKeysRegistrar.unregisterTriggerForPost(it)
}
keyValueRepo.unset(toUnset)
}
}

View File

@ -6,14 +6,14 @@ 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 dev.inmo.postssystem.core.publishing.TriggersManagementService
import kotlinx.serialization.builtins.serializer
class TriggerSetterKtorClient(
class TriggersManagementKtorClient(
private val baseUrl: String,
private val subRoute: String? = triggersRootRoute,
private val unifiedRequester: UnifiedRequester
) : TriggerSetterService {
) : TriggersManagementService {
private val apiRootUrl = subRoute ?.let { "$baseUrl/$it" } ?: baseUrl
override suspend fun setTrigger(postId: PostId, triggerId: TriggerId): Boolean = unifiedRequester.unipost(
buildStandardUrl(

View File

@ -2,7 +2,6 @@ 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
@ -10,7 +9,7 @@ import io.ktor.response.respond
import io.ktor.routing.*
import kotlinx.serialization.builtins.serializer
private inline fun Route.configureTriggerSetter(triggerSetterService: TriggerSetterService, unifiedRouter: UnifiedRouter) {
private inline fun Route.configureTriggersManagement(triggerSetterService: TriggerSetterService, unifiedRouter: UnifiedRouter) {
post("$setTriggerSubRoute/{post_id}/{trigger_id}") {
unifiedRouter.apply {
val (postId, triggerId) = uniload(TriggerSettingData.serializer())
@ -30,14 +29,14 @@ private inline fun Route.configureTriggerSetter(triggerSetterService: TriggerSet
}
}
fun Route.configureTriggerSetter(
fun Route.configureTriggersManagement(
subRoute: String? = triggersRootRoute,
triggerSetterService: TriggerSetterService,
unifiedRouter: UnifiedRouter
) {
subRoute ?.also {
route(it) {
configureTriggerSetter(triggerSetterService, unifiedRouter)
configureTriggersManagement(triggerSetterService, unifiedRouter)
}
} ?: configureTriggerSetter(triggerSetterService, unifiedRouter)
} ?: configureTriggersManagement(triggerSetterService, unifiedRouter)
}