diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt index e50a0f09..1c00fce0 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt @@ -25,22 +25,38 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe @Suppress("InjectDispatcher") suspend fun query(block: suspend () -> T): T = - newSuspendedTransaction(Dispatchers.IO) { block() } + newSuspendedTransaction(Dispatchers.IO) { block() } override suspend fun save(post: Post): Post { return query { - Posts.insert { - it[id] = post.id - it[userId] = post.userId - it[overview] = post.overview - it[text] = post.text - it[createdAt] = post.createdAt - it[visibility] = post.visibility.ordinal - it[url] = post.url - it[repostId] = post.repostId - it[replyId] = post.replyId - it[sensitive] = post.sensitive - it[apId] = post.apId + val singleOrNull = Posts.select { Posts.id eq post.id }.singleOrNull() + if (singleOrNull == null) { + Posts.insert { + it[id] = post.id + it[userId] = post.userId + it[overview] = post.overview + it[text] = post.text + it[createdAt] = post.createdAt + it[visibility] = post.visibility.ordinal + it[url] = post.url + it[repostId] = post.repostId + it[replyId] = post.replyId + it[sensitive] = post.sensitive + it[apId] = post.apId + } + } else { + Posts.update({ Posts.id eq post.id }) { + it[userId] = post.userId + it[overview] = post.overview + it[text] = post.text + it[createdAt] = post.createdAt + it[visibility] = post.visibility.ordinal + it[url] = post.url + it[repostId] = post.repostId + it[replyId] = post.replyId + it[sensitive] = post.sensitive + it[apId] = post.apId + } } return@query post } @@ -92,16 +108,16 @@ object Posts : Table() { fun ResultRow.toPost(): Post { return Post( - id = this[Posts.id], - userId = this[Posts.userId], - overview = this[Posts.overview], - text = this[Posts.text], - createdAt = this[Posts.createdAt], - visibility = Visibility.values().first { visibility -> visibility.ordinal == this[Posts.visibility] }, - url = this[Posts.url], - repostId = this[Posts.repostId], - replyId = this[Posts.replyId], - sensitive = this[Posts.sensitive], - apId = this[Posts.apId] + id = this[Posts.id], + userId = this[Posts.userId], + overview = this[Posts.overview], + text = this[Posts.text], + createdAt = this[Posts.createdAt], + visibility = Visibility.values().first { visibility -> visibility.ordinal == this[Posts.visibility] }, + url = this[Posts.url], + repostId = this[Posts.repostId], + replyId = this[Posts.replyId], + sensitive = this[Posts.sensitive], + apId = this[Posts.apId] ) }