refactor(community): extract comment store and post/comment API adapters
This commit is contained in:
+50
-3
@@ -1,9 +1,56 @@
|
||||
import { mockComments } from "@/lib/mock"
|
||||
import { allow, deny } from "@/lib/policy/assert"
|
||||
import { addNotification } from "@/lib/api/notifications"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { useCommentStore } from "@/store/comments"
|
||||
import { usePostStore } from "@/store/posts"
|
||||
|
||||
export function listComments() {
|
||||
return mockComments
|
||||
return useCommentStore.getState().comments
|
||||
}
|
||||
|
||||
export function listCommentsByPost(postId: string) {
|
||||
return mockComments.filter((comment) => comment.postId === postId)
|
||||
return useCommentStore.getState().comments.filter((comment) => comment.postId === postId)
|
||||
}
|
||||
|
||||
export function addComment(postId: string, content: string) {
|
||||
const user = useAuthStore.getState().user
|
||||
if (!user) {
|
||||
return deny("AUTH_REQUIRED", "请先登录")
|
||||
}
|
||||
|
||||
const post = usePostStore.getState().posts.find((item) => item.id === postId)
|
||||
if (!post) {
|
||||
return deny("NOT_FOUND", "帖子不存在")
|
||||
}
|
||||
|
||||
const comment = useCommentStore.getState().addComment(postId, user, content)
|
||||
if (!comment) {
|
||||
return deny("VALIDATION_FAILED", "评论内容不能为空")
|
||||
}
|
||||
|
||||
usePostStore.getState().incrementCommentCount(postId)
|
||||
|
||||
addNotification({
|
||||
type: "community",
|
||||
title: "帖子收到新评论",
|
||||
content: `《${post.title}》有新的评论`,
|
||||
link: `/post/${post.id}`,
|
||||
})
|
||||
|
||||
return allow()
|
||||
}
|
||||
|
||||
export function toggleCommentLike(commentId: string) {
|
||||
const user = useAuthStore.getState().user
|
||||
if (!user) {
|
||||
return deny("AUTH_REQUIRED", "请先登录")
|
||||
}
|
||||
|
||||
const comment = useCommentStore.getState().comments.find((item) => item.id === commentId)
|
||||
if (!comment) {
|
||||
return deny("NOT_FOUND", "评论不存在")
|
||||
}
|
||||
|
||||
useCommentStore.getState().toggleCommentLike(commentId)
|
||||
return allow()
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,13 +1,13 @@
|
||||
export { getChatSessionById, listChatMessages, listChatSessions } from "./chat"
|
||||
export { requestWithAuth } from "./client"
|
||||
export { listComments, listCommentsByPost } from "./comments"
|
||||
export { addComment, listComments, listCommentsByPost, toggleCommentLike } from "./comments"
|
||||
export { getDisputeByOrderId, listDisputes } from "./disputes"
|
||||
export { isFavorited, listFavorites, listFavoritesByUser } from "./favorites"
|
||||
export { getGameById, listGames } from "./games"
|
||||
export { listNotifications } from "./notifications"
|
||||
export { addNotification, listNotifications, markNotificationAsRead } from "./notifications"
|
||||
export { getOrderById, listOrders, listOrdersByConsumer } from "./orders"
|
||||
export { getPlayerById, listPlayers, listPlayersByShop } from "./players"
|
||||
export { getPostById, listPosts, listPostsByAuthor } from "./posts"
|
||||
export { getPostById, listPosts, listPostsByAuthor, togglePostLike } from "./posts"
|
||||
export { listReviews, listReviewsByOrder, listReviewsByTargetUser } from "./reviews"
|
||||
export { getServiceById, listServices, listServicesByPlayer } from "./services"
|
||||
export { getShopById, getShopByOwnerId, listShops } from "./shops"
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import { allow, deny } from "@/lib/policy/assert"
|
||||
import { addNotification } from "@/lib/api/notifications"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { usePostStore } from "@/store/posts"
|
||||
|
||||
export function listPosts() {
|
||||
@@ -11,3 +14,30 @@ export function getPostById(postId: string) {
|
||||
export function listPostsByAuthor(userId: string) {
|
||||
return usePostStore.getState().posts.filter((post) => post.author.id === userId)
|
||||
}
|
||||
|
||||
export function togglePostLike(postId: string) {
|
||||
const user = useAuthStore.getState().user
|
||||
if (!user) {
|
||||
return deny("AUTH_REQUIRED", "请先登录")
|
||||
}
|
||||
|
||||
const post = usePostStore.getState().posts.find((item) => item.id === postId)
|
||||
if (!post) {
|
||||
return deny("NOT_FOUND", "帖子不存在")
|
||||
}
|
||||
|
||||
const shouldNotify = !post.liked
|
||||
|
||||
usePostStore.getState().togglePostLike(postId)
|
||||
|
||||
if (shouldNotify) {
|
||||
addNotification({
|
||||
type: "community",
|
||||
title: "帖子收到点赞",
|
||||
content: `《${post.title}》有新的点赞`,
|
||||
link: `/post/${post.id}`,
|
||||
})
|
||||
}
|
||||
|
||||
return allow()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user