import { prisma } from "@/lib/prisma"; import { getServerSession } from "next-auth"; import { authOptions } from "@/lib/auth"; import { NextResponse } from "next/server"; import { rewardPoints } from "@/lib/points-reward"; export async function POST(req: Request, { params }: { params: { id: string } }) { const session = await getServerSession(authOptions); if (!session?.user?.id) { return NextResponse.json({ error: "请先登录" }, { status: 401 }); } const userId = parseInt(session.user.id); const commentId = parseInt(params.id); const [existing] = await prisma.commentLike.findMany({ where: { commentId, userId }, }); if (existing) { await prisma.commentLike.delete({ where: { commentId_userId: { commentId, userId } }, }); await prisma.comment.update({ where: { id: commentId }, data: { likeCount: { decrement: 1 } }, }); return NextResponse.json({ liked: false }); } const result = await prisma.$transaction(async (tx) => { await tx.commentLike.create({ data: { commentId, userId }, }); await tx.comment.update({ where: { id: commentId }, data: { likeCount: { increment: 1 } }, }); const comment = await tx.comment.findUnique({ where: { id: commentId }, select: { userId: true }, }); if (comment && comment.userId !== userId) { await tx.notification.create({ data: { userId: comment.userId, type: "COMMENT_LIKE", title: `${session.user.name || "用户"} 点赞了你的评论`, link: "/notifications", }, }); } return comment; }); if (result) { await rewardPoints(result.userId, "comment_like", "评论被点赞", "comment", commentId); } return NextResponse.json({ liked: true }); }