ZZY 73917d6001 dev feat(markcard, static): 实现书签卡片功能和静态资源获取
- 书签卡片相关的组件和页面
- 实现书签数据的获取、添加、编辑和删除功能
- 添加用户验证和权限控制
- 优化页面布局和样式
2024-12-22 15:21:07 +08:00

57 lines
1.6 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { MarkCardI, addCard, updateCard, getCards, getPath } from '@/lib/markcard/card'
import { notFound } from 'next/navigation'
async function getFilePath(params: Promise<{ file?: string[] }> ): Promise<string> {
const file = (await params).file
// console.log('file', file)
// const files = file ? file[0].split(',') : undefined
const filepath = await getPath(undefined, file)
if (filepath === null) {
return notFound()
}
return filepath
}
export async function GET(
req: NextRequest,
{ params }: { params: Promise<{ file: string[] }> }
) {
const filePath = await getFilePath(params)
const cards = await getCards(filePath)
if (cards === undefined) {
return notFound()
} else {
return NextResponse.json(cards, { status: 200 })
}
}
export async function POST(req: NextRequest,
{ params }: { params: Promise<{ file?: string[] }> }
) {
const card: MarkCardI = await req.json()
if (!card) {
return NextResponse.json({ error: 'Invalid card data' }, { status: 400 })
}
try {
const newcard = await addCard(card, await getFilePath(params))
return NextResponse.json(newcard, { status: 200 })
} catch (error) {
return NextResponse.json({ error }, { status: 400 })
}
}
export async function PATCH(req: NextRequest,
{ params }: { params: Promise<{ file?: string[] }> }
) {
const card: MarkCardI = await req.json()
if (!card) {
return NextResponse.json({ error: 'Invalid card data' }, { status: 400 })
}
const retcard = await updateCard(card, card.id, await getFilePath(params))
return NextResponse.json(retcard, { status: 200 })
}