diff --git a/core/api/src/commonMain/kotlin/dev/inmo/postssystem/core/post/BusinessPublishablePostsRepo.kt b/core/api/src/commonMain/kotlin/dev/inmo/postssystem/core/post/BusinessPublishablePostsRepo.kt deleted file mode 100644 index c8ff909b..00000000 --- a/core/api/src/commonMain/kotlin/dev/inmo/postssystem/core/post/BusinessPublishablePostsRepo.kt +++ /dev/null @@ -1,21 +0,0 @@ -package dev.inmo.postssystem.core.post - -import dev.inmo.postssystem.core.post.repo.PostsRepo - -class BusinessPublishablePostsRepo( - /** - * Will be used for storing of unpublished data - */ - private val repoPostsRepo: PostsRepo, - /** - * Will be used to send information ab - */ - private val publishedPostsRepo: PostsRepo -) : PostsRepo by repoPostsRepo { - override suspend fun deletePost(id: PostId): Boolean { - return getPostById(id) ?.let { post -> - publishedPostsRepo.createPost(post) - repoPostsRepo.deletePost(id) - } ?: return false - } -} \ No newline at end of file diff --git a/core/exposed/src/jvmMain/kotlin/dev/inmo/postssystem/core/exposed/ExposedPostsRepo.kt b/core/exposed/src/jvmMain/kotlin/dev/inmo/postssystem/core/exposed/ExposedPostsRepo.kt index b01d3020..8fa74ad0 100644 --- a/core/exposed/src/jvmMain/kotlin/dev/inmo/postssystem/core/exposed/ExposedPostsRepo.kt +++ b/core/exposed/src/jvmMain/kotlin/dev/inmo/postssystem/core/exposed/ExposedPostsRepo.kt @@ -6,6 +6,8 @@ import dev.inmo.postssystem.core.post.* import dev.inmo.postssystem.core.post.repo.PostsRepo import com.soywiz.klock.* import dev.inmo.micro_utils.pagination.* +import dev.inmo.micro_utils.repos.exposed.ExposedRepo +import dev.inmo.micro_utils.repos.exposed.initTable import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.Flow @@ -14,39 +16,29 @@ import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction private class PostsRepoContentRelations( - private val database: Database -) : Table() { + override val database: Database +) : Table(), ExposedRepo { private val postIdColumn = text("postId") private val contentIdColumn = text("contentId") init { - transaction ( - db = database - ) { - SchemaUtils.createMissingTablesAndColumns(this@PostsRepoContentRelations) - } + initTable() } fun getPostContents(postId: PostId): List { - return transaction( - db = database - ) { + return transaction(db = database) { select { postIdColumn.eq(postId) }.map { it[contentIdColumn] } } } fun getContentPosts(contentId: ContentId): List { - return transaction( - db = database - ) { + return transaction(db = database) { select { contentIdColumn.eq(contentId) }.map { it[postIdColumn] } } } fun linkPostAndContents(postId: PostId, vararg contentIds: ContentId) { - transaction( - db = database - ) { + transaction(db = database) { val leftToPut = contentIds.toSet() - getPostContents(postId) leftToPut.forEach { contentId -> insert { @@ -57,9 +49,7 @@ private class PostsRepoContentRelations( } } fun unlinkPostAndContents(postId: PostId, vararg contentIds: ContentId): Boolean { - return transaction( - db = database - ) { + return transaction(db = database) { deleteWhere { postIdColumn.eq(postId).and(contentIdColumn.inList(contentIds.toList())) } > 0 diff --git a/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/repos/PublishingKeysRepo.kt b/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/repos/PublishingKeysRepo.kt index 2e1d1f8c..da508a80 100644 --- a/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/repos/PublishingKeysRepo.kt +++ b/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/repos/PublishingKeysRepo.kt @@ -1,5 +1,7 @@ package dev.inmo.postssystem.core.publishing.repos +import dev.inmo.micro_utils.pagination.FirstPagePagination +import dev.inmo.micro_utils.repos.* import dev.inmo.postssystem.core.post.PostId import dev.inmo.postssystem.core.publishing.TriggerControlKey @@ -23,3 +25,30 @@ interface WritePublishingKeysRepo { } interface PublishingKeysRepo : ReadPublishingKeysRepo, WritePublishingKeysRepo + +fun PublishingKeysRepo( + keyValueRepo: KeyValueRepo +) = object : PublishingKeysRepo { + override suspend fun getPostIdByTriggerControlKey( + key: TriggerControlKey + ): PostId? = keyValueRepo.keys(key, FirstPagePagination(1)).results.firstOrNull() + + override suspend fun getTriggerControlKeyByPostId( + postId: PostId + ): TriggerControlKey? = keyValueRepo.get(postId) + + override suspend fun setPostTriggerControlKey( + postId: PostId, + key: TriggerControlKey + ): Boolean { + keyValueRepo.set(postId, key) + return true + } + + override suspend fun unsetPostTriggerControlKey( + postId: PostId + ): Boolean { + keyValueRepo.unset(postId) + return true + } +} 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 new file mode 100644 index 00000000..6dabaff1 --- /dev/null +++ b/publishing/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/TriggerSetterConfiguratorRoutes.kt @@ -0,0 +1,3 @@ +package com.insanusmokrassar.postssystem.publishing.ktor + +const val setTriggerRoute = "set_trigger" 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 new file mode 100644 index 00000000..90e6c8a9 --- /dev/null +++ b/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/TriggerSetterConfigurator.kt @@ -0,0 +1,33 @@ +package com.insanusmokrassar.postssystem.publishing.ktor.server + +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 kotlinx.serialization.builtins.serializer + +fun Route.configureTriggerSetter( + postKeyGenerator: PostKeyGenerator, + publishingKeyReceiverGetter: PublishingKeyReceiverGetter, + unifiedRouter: UnifiedRouter +) { + post("set_trigger/{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 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) + } + } +} diff --git a/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/PublishingKeysRepoRoutingConfigurator.kt b/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/publishing_keys_repo/PublishingKeysRepoRoutingConfigurator.kt similarity index 92% rename from publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/PublishingKeysRepoRoutingConfigurator.kt rename to publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/publishing_keys_repo/PublishingKeysRepoRoutingConfigurator.kt index cea7997d..3202c314 100644 --- a/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/PublishingKeysRepoRoutingConfigurator.kt +++ b/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/publishing_keys_repo/PublishingKeysRepoRoutingConfigurator.kt @@ -1,4 +1,4 @@ -package dev.inmo.postssystem.publishing.ktor.server +package com.insanusmokrassar.postssystem.publishing.ktor.server.publishing_keys_repo import dev.inmo.postssystem.core.publishing.repos.PublishingKeysRepo import dev.inmo.postssystem.publishing.ktor.publishingKeysRootRoute diff --git a/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/ReadPublishingKeysRepoRoutingConfigurator.kt b/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/publishing_keys_repo/ReadPublishingKeysRepoRoutingConfigurator.kt similarity index 95% rename from publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/ReadPublishingKeysRepoRoutingConfigurator.kt rename to publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/publishing_keys_repo/ReadPublishingKeysRepoRoutingConfigurator.kt index 1c322edd..5f90578a 100644 --- a/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/ReadPublishingKeysRepoRoutingConfigurator.kt +++ b/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/publishing_keys_repo/ReadPublishingKeysRepoRoutingConfigurator.kt @@ -1,4 +1,4 @@ -package dev.inmo.postssystem.publishing.ktor.server +package com.insanusmokrassar.postssystem.publishing.ktor.server.publishing_keys_repo import dev.inmo.postssystem.core.post.PostId import dev.inmo.postssystem.core.publishing.TriggerControlKey diff --git a/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/WritePublishingKeysRepoRoutingConfigurator.kt b/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/publishing_keys_repo/WritePublishingKeysRepoRoutingConfigurator.kt similarity index 92% rename from publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/WritePublishingKeysRepoRoutingConfigurator.kt rename to publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/publishing_keys_repo/WritePublishingKeysRepoRoutingConfigurator.kt index 95e60416..b154ee88 100644 --- a/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/WritePublishingKeysRepoRoutingConfigurator.kt +++ b/publishing/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/publishing/ktor/server/publishing_keys_repo/WritePublishingKeysRepoRoutingConfigurator.kt @@ -1,4 +1,4 @@ -package dev.inmo.postssystem.publishing.ktor.server +package com.insanusmokrassar.postssystem.publishing.ktor.server.publishing_keys_repo import dev.inmo.postssystem.core.post.PostId import dev.inmo.postssystem.core.publishing.repos.WritePublishingKeysRepo