feat: now support make any record starred

This commit is contained in:
2025-07-02 21:39:41 +08:00
parent 6e2976e234
commit acde78f4a0
8 changed files with 618 additions and 129 deletions
+46
View File
@@ -0,0 +1,46 @@
import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import { withCors } from '@/lib/middleware'
// 切换记录的星标状态
async function handleToggleStar(req: NextRequest) {
try {
// 从 URL 路径中提取 recordId
const pathSegments = req.nextUrl.pathname.split('/')
const recordIdIndex = pathSegments.indexOf('records') + 1
const recordId = pathSegments[recordIdIndex]
if (!recordId) {
return NextResponse.json({ error: '缺少记录ID' }, { status: 400 })
}
// 获取当前记录
const record = await prisma.record.findUnique({
where: { id: recordId },
select: { id: true, isStarred: true }
})
if (!record) {
return NextResponse.json({ error: '记录不存在' }, { status: 404 })
}
// 切换星标状态
const updatedRecord = await prisma.record.update({
where: { id: recordId },
data: { isStarred: !record.isStarred },
select: { id: true, isStarred: true }
})
return NextResponse.json({
success: true,
recordId: updatedRecord.id,
isStarred: updatedRecord.isStarred
})
} catch (error) {
console.error('切换星标状态失败:', error)
return NextResponse.json({ error: '操作失败' }, { status: 500 })
}
}
export const PATCH = withCors(handleToggleStar)