small update
This commit is contained in:
parent
4d0aae2b91
commit
4b6544131f
@ -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
|
||||
}
|
||||
}
|
@ -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<ContentId> {
|
||||
return transaction(
|
||||
db = database
|
||||
) {
|
||||
return transaction(db = database) {
|
||||
select { postIdColumn.eq(postId) }.map { it[contentIdColumn] }
|
||||
}
|
||||
}
|
||||
|
||||
fun getContentPosts(contentId: ContentId): List<PostId> {
|
||||
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
|
||||
|
@ -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<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
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,3 @@
|
||||
package com.insanusmokrassar.postssystem.publishing.ktor
|
||||
|
||||
const val setTriggerRoute = "set_trigger"
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
@ -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
|
@ -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
|
Loading…
Reference in New Issue
Block a user