update update published repo

This commit is contained in:
InsanusMokrassar 2020-12-04 16:41:19 +06:00
parent b85d4cac47
commit 2b0fccefdc
2 changed files with 19 additions and 5 deletions

View File

@ -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<RegisteredContent>,
private val publicationTime: UnixMillis
) {
@Transient

View File

@ -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<Number>.asObject(value: RegisteredPost): PublishedPost = PublishedPost(
get(idColumn),
value,
launchSynchronously(scope) { value.content.mapNotNull { contentRepo.getById(it) } },
get(dateTimeColumn)
)
override val selectById: SqlExpressionBuilder.(PublishedPostId) -> Op<Boolean> = { 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()