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 { 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 }) }