refactor(errors): migrate decisions to {code,msg}
This commit is contained in:
+6
-6
@@ -1,5 +1,5 @@
|
||||
import { addNotification } from "@/lib/api/notifications"
|
||||
import { allow, deny } from "@/lib/policy/assert"
|
||||
import { allow, deny } from "@/lib/decision"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { useCommentStore } from "@/store/comments"
|
||||
import { usePostStore } from "@/store/posts"
|
||||
@@ -15,17 +15,17 @@ export function listCommentsByPost(postId: string) {
|
||||
export function addComment(postId: string, content: string) {
|
||||
const user = useAuthStore.getState().user
|
||||
if (!user) {
|
||||
return deny("AUTH_REQUIRED", "请先登录")
|
||||
return deny(401, "请先登录")
|
||||
}
|
||||
|
||||
const post = usePostStore.getState().posts.find((item) => item.id === postId)
|
||||
if (!post) {
|
||||
return deny("NOT_FOUND", "帖子不存在")
|
||||
return deny(404, "帖子不存在")
|
||||
}
|
||||
|
||||
const comment = useCommentStore.getState().addComment(postId, user, content)
|
||||
if (!comment) {
|
||||
return deny("VALIDATION_FAILED", "评论内容不能为空")
|
||||
return deny(400, "评论内容不能为空")
|
||||
}
|
||||
|
||||
usePostStore.getState().incrementCommentCount(postId)
|
||||
@@ -43,12 +43,12 @@ export function addComment(postId: string, content: string) {
|
||||
export function toggleCommentLike(commentId: string) {
|
||||
const user = useAuthStore.getState().user
|
||||
if (!user) {
|
||||
return deny("AUTH_REQUIRED", "请先登录")
|
||||
return deny(401, "请先登录")
|
||||
}
|
||||
|
||||
const comment = useCommentStore.getState().comments.find((item) => item.id === commentId)
|
||||
if (!comment) {
|
||||
return deny("NOT_FOUND", "评论不存在")
|
||||
return deny(404, "评论不存在")
|
||||
}
|
||||
|
||||
useCommentStore.getState().toggleCommentLike(commentId)
|
||||
|
||||
Reference in New Issue
Block a user