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, key: TriggerControlKey,
postId: PostId postId: PostId
): Boolean ): Boolean
suspend fun unregisterTriggerForPost(
postId: PostId
): Boolean
} }
interface PublishingKeysRegistrar : ReadPublishingRegistrar, WritePublishingRegistrar 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" "Unknown trigger id $triggerId"
) )
interface TriggerSetterService { interface ReadTriggersManagementService {
@Throws(TriggerSetterServiceUnknownTriggerIdException::class)
suspend fun getTrigger(postId: PostId): TriggerId?
}
interface WriteTriggersManagementService {
@Throws(TriggerSetterServiceUnknownTriggerIdException::class) @Throws(TriggerSetterServiceUnknownTriggerIdException::class)
suspend fun setTrigger(postId: PostId, triggerId: TriggerId): Boolean suspend fun setTrigger(postId: PostId, triggerId: TriggerId): Boolean
} }
class BusinessTriggerSetterService( interface TriggersManagementService : ReadTriggersManagementService, WriteTriggersManagementService
class BusinessTriggersManagementService(
private val postKeyGenerator: PostKeyGenerator, private val postKeyGenerator: PostKeyGenerator,
private val publishingKeyReceiverGetter: PublishingKeyReceiverGetter private val publishingKeyReceiverGetter: PublishingKeyReceiverGetter,
) : TriggerSetterService { 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 { override suspend fun setTrigger(postId: PostId, triggerId: TriggerId): Boolean {
val publishingKeyReceiver = publishingKeyReceiverGetter(triggerId) ?: throw TriggerSetterServiceUnknownTriggerIdException(triggerId) val publishingKeyReceiver = publishingKeyReceiverGetter(triggerId) ?: throw TriggerSetterServiceUnknownTriggerIdException(triggerId)
val triggerControlKey = postKeyGenerator(postId, triggerId) val triggerControlKey = postKeyGenerator(postId, triggerId)
keysRegistrar.registerTriggerForPost(triggerControlKey, postId)
publishingKeyReceiver.acceptKey(postId, triggerControlKey) publishingKeyReceiver.acceptKey(postId, triggerControlKey)
return true 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.micro_utils.ktor.common.buildStandardUrl
import dev.inmo.postssystem.core.post.PostId import dev.inmo.postssystem.core.post.PostId
import dev.inmo.postssystem.core.publishing.TriggerId 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 import kotlinx.serialization.builtins.serializer
class TriggerSetterKtorClient( class TriggersManagementKtorClient(
private val baseUrl: String, private val baseUrl: String,
private val subRoute: String? = triggersRootRoute, private val subRoute: String? = triggersRootRoute,
private val unifiedRequester: UnifiedRequester private val unifiedRequester: UnifiedRequester
) : TriggerSetterService { ) : TriggersManagementService {
private val apiRootUrl = subRoute ?.let { "$baseUrl/$it" } ?: baseUrl private val apiRootUrl = subRoute ?.let { "$baseUrl/$it" } ?: baseUrl
override suspend fun setTrigger(postId: PostId, triggerId: TriggerId): Boolean = unifiedRequester.unipost( override suspend fun setTrigger(postId: PostId, triggerId: TriggerId): Boolean = unifiedRequester.unipost(
buildStandardUrl( buildStandardUrl(

View File

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