import { getArticleList } from "@/app/_lib/data/article";
import { getCollection } from "@/app/_lib/mongodb";
import { NextRequest } from "next/server";

export async function GET(request: NextRequest) {
    const page = parseInt(request.nextUrl.searchParams.get('page') || '1') || 1;
    const pageSize = parseInt(request.nextUrl.searchParams.get('pageSize') || '10') || 10;
    // 计算起始索引和结束索引
    const res = await getArticleList({ page, pageSize })
    return Response.json(res)
}
export async function POST(request: NextRequest) {
    try {
        const body = await request.json();
        const collection = await getCollection('article');
        const data = await collection.insertOne(body);
        return Response.json(data)
    } catch {
        return Response.error()
    }
}
export async function PUT(request: NextRequest) {
    try {
        const body = await request.json();
        const collection = await getCollection('article');
        const data = await collection.updateOne({ _id: body._id }, { $set: body });
        return Response.json(data)
    } catch {
        return Response.error()
    }
}