Skip to content

Commit

Permalink
fix(add/remove tag): 修复重复添加tag或删除不存在的tag依旧会返回200的错误
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
nullaqua committed Sep 25, 2024
1 parent a68bbd2 commit 073637d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/subit/database/Tags.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import subit.dataClasses.Slice
interface Tags
{
suspend fun getPostTags(pid: PostId): List<String>
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<String>
suspend fun getAllTags(begin: Long, count: Int): Slice<String>
}
8 changes: 4 additions & 4 deletions src/main/kotlin/subit/database/memoryImpl/TagsImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class TagsImpl: Tags

override suspend fun getPostTags(pid: PostId): List<String> = 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<String> =
Expand Down
15 changes: 10 additions & 5 deletions src/main/kotlin/subit/database/sqlImpl/TagsImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ class TagsImpl: Tags, DaoSqlImpl<TagsImpl.TagsTable>(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<String> = query()
Expand Down
7 changes: 5 additions & 2 deletions src/main/kotlin/subit/router/Tag.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,11 @@ private suspend fun Context.editPostTag(add: Boolean)
if (post.parent != null) finishCall(HttpStatus.NotAcceptable.subStatus("不能为评论添加标签"))
val tag = call.receive<Tag>().tag
if (tag.isBlank()) finishCall(HttpStatus.BadRequest)
if (add) get<Tags>().addPostTag(pid, tag)
else get<Tags>().removePostTag(pid, tag)
val success =
if (add) get<Tags>().addPostTag(pid, tag)
else get<Tags>().removePostTag(pid, tag)
if (!success && add) finishCall(HttpStatus.Conflict.subStatus("标签已存在"))
else if (!success) finishCall(HttpStatus.NotFound.subStatus("标签不存在"))
finishCall(HttpStatus.OK)
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/subit/utils/HttpStatus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 073637d

Please sign in to comment.