From 073637d17b1023892c5b6cf77e975ebee0e6670d Mon Sep 17 00:00:00 2001 From: nullaqua Date: Wed, 25 Sep 2024 22:17:35 +0800 Subject: [PATCH] =?UTF-8?q?fix(add/remove=20tag):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E6=B7=BB=E5=8A=A0tag=E6=88=96=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E4=B8=8D=E5=AD=98=E5=9C=A8=E7=9A=84tag=E4=BE=9D?= =?UTF-8?q?=E6=97=A7=E4=BC=9A=E8=BF=94=E5=9B=9E200=E7=9A=84=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #1 --- src/main/kotlin/subit/database/Tags.kt | 4 ++-- .../kotlin/subit/database/memoryImpl/TagsImpl.kt | 8 ++++---- .../kotlin/subit/database/sqlImpl/TagsImpl.kt | 15 ++++++++++----- src/main/kotlin/subit/router/Tag.kt | 7 +++++-- src/main/kotlin/subit/utils/HttpStatus.kt | 2 ++ 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/subit/database/Tags.kt b/src/main/kotlin/subit/database/Tags.kt index cd7de40..8bd4cda 100644 --- a/src/main/kotlin/subit/database/Tags.kt +++ b/src/main/kotlin/subit/database/Tags.kt @@ -6,8 +6,8 @@ import subit.dataClasses.Slice interface Tags { suspend fun getPostTags(pid: PostId): List - suspend fun removePostTag(pid: PostId, tag: String) - suspend fun addPostTag(pid: PostId, tag: String) + suspend fun removePostTag(pid: PostId, tag: String): Boolean + suspend fun addPostTag(pid: PostId, tag: String): Boolean suspend fun searchTags(key: String, begin: Long, count: Int): Slice suspend fun getAllTags(begin: Long, count: Int): Slice } \ No newline at end of file diff --git a/src/main/kotlin/subit/database/memoryImpl/TagsImpl.kt b/src/main/kotlin/subit/database/memoryImpl/TagsImpl.kt index 2ca2f14..71bf237 100644 --- a/src/main/kotlin/subit/database/memoryImpl/TagsImpl.kt +++ b/src/main/kotlin/subit/database/memoryImpl/TagsImpl.kt @@ -11,14 +11,14 @@ class TagsImpl: Tags override suspend fun getPostTags(pid: PostId): List = tags[pid]?.toList() ?: emptyList() - override suspend fun removePostTag(pid: PostId, tag: String) + override suspend fun removePostTag(pid: PostId, tag: String): Boolean { - tags[pid]?.remove(tag) + return tags[pid]?.remove(tag) ?: false } - override suspend fun addPostTag(pid: PostId, tag: String) + override suspend fun addPostTag(pid: PostId, tag: String): Boolean { - tags.getOrPut(pid) { mutableSetOf() }.add(tag) + return tags.getOrPut(pid) { mutableSetOf() }.add(tag) } override suspend fun searchTags(key: String, begin: Long, count: Int): Slice = diff --git a/src/main/kotlin/subit/database/sqlImpl/TagsImpl.kt b/src/main/kotlin/subit/database/sqlImpl/TagsImpl.kt index ca069da..57a4f6e 100644 --- a/src/main/kotlin/subit/database/sqlImpl/TagsImpl.kt +++ b/src/main/kotlin/subit/database/sqlImpl/TagsImpl.kt @@ -20,15 +20,20 @@ class TagsImpl: Tags, DaoSqlImpl(TagsTable) table.select(tag).where { post eq pid }.map { it[tag] } } - override suspend fun removePostTag(pid: PostId, tag: String): Unit = query() + override suspend fun removePostTag(pid: PostId, tag: String): Boolean = query() { - table.deleteWhere { (post eq pid) and (TagsTable.tag eq tag) } + table.deleteWhere { (post eq pid) and (TagsTable.tag eq tag) } > 0 } - override suspend fun addPostTag(pid: PostId, tag: String): Unit = query() + override suspend fun addPostTag(pid: PostId, tag: String): Boolean = query() { - table.deleteWhere { (post eq pid) and (TagsTable.tag eq tag) } - table.insert { it[post] = pid; it[TagsTable.tag] = tag } + if (selectAll().where { (post eq pid) and (TagsTable.tag eq tag) }.count() > 0) + return@query false + table.insert { + it[post] = pid + it[TagsTable.tag] = tag + } + true } override suspend fun searchTags(key: String, begin: Long, count: Int): Slice = query() diff --git a/src/main/kotlin/subit/router/Tag.kt b/src/main/kotlin/subit/router/Tag.kt index c041b3d..1d90134 100644 --- a/src/main/kotlin/subit/router/Tag.kt +++ b/src/main/kotlin/subit/router/Tag.kt @@ -119,8 +119,11 @@ private suspend fun Context.editPostTag(add: Boolean) if (post.parent != null) finishCall(HttpStatus.NotAcceptable.subStatus("不能为评论添加标签")) val tag = call.receive().tag if (tag.isBlank()) finishCall(HttpStatus.BadRequest) - if (add) get().addPostTag(pid, tag) - else get().removePostTag(pid, tag) + val success = + if (add) get().addPostTag(pid, tag) + else get().removePostTag(pid, tag) + if (!success && add) finishCall(HttpStatus.Conflict.subStatus("标签已存在")) + else if (!success) finishCall(HttpStatus.NotFound.subStatus("标签不存在")) finishCall(HttpStatus.OK) } diff --git a/src/main/kotlin/subit/utils/HttpStatus.kt b/src/main/kotlin/subit/utils/HttpStatus.kt index 8e8058c..1bbb73e 100644 --- a/src/main/kotlin/subit/utils/HttpStatus.kt +++ b/src/main/kotlin/subit/utils/HttpStatus.kt @@ -70,6 +70,8 @@ data class HttpStatus(val code: HttpStatusCode, val message: String) val NotRealName = HttpStatus(HttpStatusCode(451, "Unavailable For Legal Reasons"), "未绑定实名信息") // 不接受的请求 val NotAcceptable = HttpStatus(HttpStatusCode.NotAcceptable, "不接受的请求") + // 冲突 + val Conflict = HttpStatus(HttpStatusCode.Conflict, "冲突") } fun subStatus(message: String) = HttpStatus(code, "${this.message}: $message")