import { prisma } from "@/lib/prisma"; import { getServerSession } from "next-auth"; import { authOptions } from "@/lib/auth"; import { NextResponse } from "next/server"; export async function GET() { const session = await getServerSession(authOptions); if (!session?.user?.id) { return NextResponse.json({ error: "请先登录" }, { status: 401 }); } const userId = parseInt(session.user.id); const history = await prisma.browsingHistory.findMany({ where: { userId }, include: { tool: { select: { id: true, name: true, slug: true, description: true, logoUrl: true, pricingModel: true, category: { select: { id: true, name: true, slug: true } }, }, }, skill: { select: { id: true, name: true, slug: true, description: true, logoUrl: true, sourceType: true, category: { select: { id: true, name: true, slug: true } }, }, }, }, orderBy: { viewedAt: "desc" }, take: 20, }); return NextResponse.json({ history }); } export async function POST(request: Request) { const session = await getServerSession(authOptions); if (!session?.user?.id) { return NextResponse.json({ error: "请先登录" }, { status: 401 }); } const body = await request.json(); const { toolId, skillId } = body; const userId = parseInt(session.user.id); if (!toolId && !skillId) { return NextResponse.json({ error: "必须提供 toolId 或 skillId" }, { status: 400 }); } if (toolId) { await prisma.browsingHistory.upsert({ where: { userId_toolId: { userId, toolId } }, update: { viewedAt: new Date() }, create: { userId, toolId }, }); } if (skillId) { await prisma.browsingHistory.upsert({ where: { userId_skillId: { userId, skillId } }, update: { viewedAt: new Date() }, create: { userId, skillId }, }); } return NextResponse.json({ success: true }); }