diff --git a/posts/src/jvmMain/kotlin/exposed/ExposedPostsRepo.kt b/posts/src/jvmMain/kotlin/exposed/ExposedPostsRepo.kt index 4ee12cf..05103a9 100644 --- a/posts/src/jvmMain/kotlin/exposed/ExposedPostsRepo.kt +++ b/posts/src/jvmMain/kotlin/exposed/ExposedPostsRepo.kt @@ -3,6 +3,7 @@ package dev.inmo.plaguposter.posts.exposed import com.benasher44.uuid.uuid4 import com.soywiz.klock.DateTime import dev.inmo.micro_utils.repos.KeyValuesRepo +import dev.inmo.micro_utils.repos.UpdatedValuePair import dev.inmo.micro_utils.repos.exposed.AbstractExposedCRUDRepo import dev.inmo.micro_utils.repos.exposed.initTable import dev.inmo.plaguposter.posts.models.* @@ -75,17 +76,20 @@ class ExposedPostsRepo( return id } - override fun update(id: PostId?, value: NewPost, it: UpdateBuilder) { - id ?: error("Unable to find post id in update") - with(contentRepo) { - deleteWhere { postIdColumn.eq(id.string) } - value.content.forEach { contentInfo -> - insert { - it[postIdColumn] = id.string - it[chatIdColumn] = contentInfo.chatId.chatId - it[messageIdColumn] = contentInfo.messageId - it[groupColumn] = contentInfo.group - it[orderColumn] = contentInfo.order + override fun update(id: PostId?, value: NewPost, it: UpdateBuilder) {} + + private fun updateContent(post: RegisteredPost) { + transaction(database) { + with(contentRepo) { + deleteWhere { postIdColumn.eq(post.id.string) } + post.content.forEach { contentInfo -> + insert { + it[postIdColumn] = post.id.string + it[chatIdColumn] = contentInfo.chatId.chatId + it[messageIdColumn] = contentInfo.messageId + it[groupColumn] = contentInfo.group + it[orderColumn] = contentInfo.order + } } } } @@ -96,6 +100,20 @@ class ExposedPostsRepo( it[createdColumn] = DateTime.now().unixMillis } + override suspend fun onAfterCreate(values: List>): List { + values.forEach { + updateContent(it.second) + } + return super.onAfterCreate(values) + } + + override suspend fun onAfterUpdate(value: List>): List { + value.forEach { + updateContent(it.second) + } + return super.onAfterUpdate(value) + } + override suspend fun deleteById(ids: List) { onBeforeDelete(ids) val posts = ids.mapNotNull {