feat: Postのidが同じ時、上書きするように

This commit is contained in:
usbharu 2023-06-03 23:54:13 +09:00
parent cec78bff00
commit d3d91fc2bc
1 changed files with 40 additions and 24 deletions

View File

@ -25,22 +25,38 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe
@Suppress("InjectDispatcher") @Suppress("InjectDispatcher")
suspend fun <T> query(block: suspend () -> T): T = suspend fun <T> query(block: suspend () -> T): T =
newSuspendedTransaction(Dispatchers.IO) { block() } newSuspendedTransaction(Dispatchers.IO) { block() }
override suspend fun save(post: Post): Post { override suspend fun save(post: Post): Post {
return query { return query {
Posts.insert { val singleOrNull = Posts.select { Posts.id eq post.id }.singleOrNull()
it[id] = post.id if (singleOrNull == null) {
it[userId] = post.userId Posts.insert {
it[overview] = post.overview it[id] = post.id
it[text] = post.text it[userId] = post.userId
it[createdAt] = post.createdAt it[overview] = post.overview
it[visibility] = post.visibility.ordinal it[text] = post.text
it[url] = post.url it[createdAt] = post.createdAt
it[repostId] = post.repostId it[visibility] = post.visibility.ordinal
it[replyId] = post.replyId it[url] = post.url
it[sensitive] = post.sensitive it[repostId] = post.repostId
it[apId] = post.apId 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 return@query post
} }
@ -92,16 +108,16 @@ object Posts : Table() {
fun ResultRow.toPost(): Post { fun ResultRow.toPost(): Post {
return Post( return Post(
id = this[Posts.id], id = this[Posts.id],
userId = this[Posts.userId], userId = this[Posts.userId],
overview = this[Posts.overview], overview = this[Posts.overview],
text = this[Posts.text], text = this[Posts.text],
createdAt = this[Posts.createdAt], createdAt = this[Posts.createdAt],
visibility = Visibility.values().first { visibility -> visibility.ordinal == this[Posts.visibility] }, visibility = Visibility.values().first { visibility -> visibility.ordinal == this[Posts.visibility] },
url = this[Posts.url], url = this[Posts.url],
repostId = this[Posts.repostId], repostId = this[Posts.repostId],
replyId = this[Posts.replyId], replyId = this[Posts.replyId],
sensitive = this[Posts.sensitive], sensitive = this[Posts.sensitive],
apId = this[Posts.apId] apId = this[Posts.apId]
) )
} }