small update

This commit is contained in:
InsanusMokrassar 2020-11-28 12:53:50 +06:00
parent 4d0aae2b91
commit 4b6544131f
8 changed files with 77 additions and 43 deletions

View File

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

View File

@ -6,6 +6,8 @@ import dev.inmo.postssystem.core.post.*
import dev.inmo.postssystem.core.post.repo.PostsRepo import dev.inmo.postssystem.core.post.repo.PostsRepo
import com.soywiz.klock.* import com.soywiz.klock.*
import dev.inmo.micro_utils.pagination.* 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.BroadcastChannel
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
@ -14,39 +16,29 @@ import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
private class PostsRepoContentRelations( private class PostsRepoContentRelations(
private val database: Database override val database: Database
) : Table() { ) : Table(), ExposedRepo {
private val postIdColumn = text("postId") private val postIdColumn = text("postId")
private val contentIdColumn = text("contentId") private val contentIdColumn = text("contentId")
init { init {
transaction ( initTable()
db = database
) {
SchemaUtils.createMissingTablesAndColumns(this@PostsRepoContentRelations)
}
} }
fun getPostContents(postId: PostId): List<ContentId> { fun getPostContents(postId: PostId): List<ContentId> {
return transaction( return transaction(db = database) {
db = database
) {
select { postIdColumn.eq(postId) }.map { it[contentIdColumn] } select { postIdColumn.eq(postId) }.map { it[contentIdColumn] }
} }
} }
fun getContentPosts(contentId: ContentId): List<PostId> { fun getContentPosts(contentId: ContentId): List<PostId> {
return transaction( return transaction(db = database) {
db = database
) {
select { contentIdColumn.eq(contentId) }.map { it[postIdColumn] } select { contentIdColumn.eq(contentId) }.map { it[postIdColumn] }
} }
} }
fun linkPostAndContents(postId: PostId, vararg contentIds: ContentId) { fun linkPostAndContents(postId: PostId, vararg contentIds: ContentId) {
transaction( transaction(db = database) {
db = database
) {
val leftToPut = contentIds.toSet() - getPostContents(postId) val leftToPut = contentIds.toSet() - getPostContents(postId)
leftToPut.forEach { contentId -> leftToPut.forEach { contentId ->
insert { insert {
@ -57,9 +49,7 @@ private class PostsRepoContentRelations(
} }
} }
fun unlinkPostAndContents(postId: PostId, vararg contentIds: ContentId): Boolean { fun unlinkPostAndContents(postId: PostId, vararg contentIds: ContentId): Boolean {
return transaction( return transaction(db = database) {
db = database
) {
deleteWhere { deleteWhere {
postIdColumn.eq(postId).and(contentIdColumn.inList(contentIds.toList())) postIdColumn.eq(postId).and(contentIdColumn.inList(contentIds.toList()))
} > 0 } > 0

View File

@ -1,5 +1,7 @@
package dev.inmo.postssystem.core.publishing.repos 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.post.PostId
import dev.inmo.postssystem.core.publishing.TriggerControlKey import dev.inmo.postssystem.core.publishing.TriggerControlKey
@ -23,3 +25,30 @@ interface WritePublishingKeysRepo {
} }
interface PublishingKeysRepo : ReadPublishingKeysRepo, WritePublishingKeysRepo interface PublishingKeysRepo : ReadPublishingKeysRepo, WritePublishingKeysRepo
fun PublishingKeysRepo(
keyValueRepo: KeyValueRepo<PostId, TriggerControlKey>
) = 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
}
}

View File

@ -0,0 +1,3 @@
package com.insanusmokrassar.postssystem.publishing.ktor
const val setTriggerRoute = "set_trigger"

View File

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

View File

@ -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.core.publishing.repos.PublishingKeysRepo
import dev.inmo.postssystem.publishing.ktor.publishingKeysRootRoute import dev.inmo.postssystem.publishing.ktor.publishingKeysRootRoute

View File

@ -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.post.PostId
import dev.inmo.postssystem.core.publishing.TriggerControlKey import dev.inmo.postssystem.core.publishing.TriggerControlKey

View File

@ -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.post.PostId
import dev.inmo.postssystem.core.publishing.repos.WritePublishingKeysRepo import dev.inmo.postssystem.core.publishing.repos.WritePublishingKeysRepo