From 2b0fccefdc3a55b376a0cc9dab199e9e49a126ba Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 4 Dec 2020 16:41:19 +0600 Subject: [PATCH] update update published repo --- .../publishing/repos/PublishedPostsRepo.kt | 3 +++ .../exposed/ExposedPublishedPostsRepo.kt | 21 ++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/repos/PublishedPostsRepo.kt b/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/repos/PublishedPostsRepo.kt index 36a80d60..3a6cc762 100644 --- a/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/repos/PublishedPostsRepo.kt +++ b/publishing/api/src/commonMain/kotlin/dev/inmo/postssystem/core/publishing/repos/PublishedPostsRepo.kt @@ -4,6 +4,8 @@ import com.soywiz.klock.DateTime import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.* import dev.inmo.postssystem.core.UnixMillis +import dev.inmo.postssystem.core.content.Content +import dev.inmo.postssystem.core.content.RegisteredContent import dev.inmo.postssystem.core.post.PostId import dev.inmo.postssystem.core.post.RegisteredPost import dev.inmo.postssystem.core.post.repo.PostsRepo @@ -16,6 +18,7 @@ typealias PublishedPostId = String data class PublishedPost( val id: PublishedPostId, val post: RegisteredPost, + val content: List, private val publicationTime: UnixMillis ) { @Transient diff --git a/publishing/exposed/src/jvmMain/kotlin/dev/inmo/postssystem/core/publishing/exposed/ExposedPublishedPostsRepo.kt b/publishing/exposed/src/jvmMain/kotlin/dev/inmo/postssystem/core/publishing/exposed/ExposedPublishedPostsRepo.kt index ee4bd794..96792d79 100644 --- a/publishing/exposed/src/jvmMain/kotlin/dev/inmo/postssystem/core/publishing/exposed/ExposedPublishedPostsRepo.kt +++ b/publishing/exposed/src/jvmMain/kotlin/dev/inmo/postssystem/core/publishing/exposed/ExposedPublishedPostsRepo.kt @@ -6,6 +6,7 @@ import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.utils.reverse import dev.inmo.micro_utils.repos.exposed.AbstractExposedCRUDRepo import dev.inmo.micro_utils.repos.exposed.initTable +import dev.inmo.postssystem.core.content.api.ReadContentRepo import dev.inmo.postssystem.core.generateId import dev.inmo.postssystem.core.post.PostId import dev.inmo.postssystem.core.post.RegisteredPost @@ -21,6 +22,7 @@ import org.jetbrains.exposed.sql.transactions.transaction class ExposedPublishedPostsRepo( override val database: Database, private val postsRepo: ReadPostsRepo, + private val contentRepo: ReadContentRepo, tableName: String = "PublishedPostsRepo", private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default) ) : PublishedPostsRepo, @@ -39,16 +41,25 @@ class ExposedPublishedPostsRepo( override fun InsertStatement.asObject(value: RegisteredPost): PublishedPost = PublishedPost( get(idColumn), value, + launchSynchronously(scope) { value.content.mapNotNull { contentRepo.getById(it) } }, get(dateTimeColumn) ) override val selectById: SqlExpressionBuilder.(PublishedPostId) -> Op = { idColumn.eq(it) } override val ResultRow.asObject: PublishedPost - get() = PublishedPost( - get(idColumn), - requireNotNull(launchSynchronously(scope) { postsRepo.getPostById(get(postIdColumn)) }) { "Post with id \"${get(postIdColumn)}\" not found" }, - get(dateTimeColumn) - ) + get() { + val (post, content) = launchSynchronously(scope) { + val post = requireNotNull(postsRepo.getPostById(get(postIdColumn))) {"Post with id \"${get(postIdColumn)}\" not found" } + post to post.content.mapNotNull { contentRepo.getById(it) } + } + return PublishedPost( + get(idColumn), + post, + content, + get(dateTimeColumn) + ) + } + init { initTable()