ai-bot/app/api/link/[id]/route.ts

53 lines
1.8 KiB
TypeScript

import { getCollection, getDb } from "@/app/_lib/mongodb";
import { ObjectId } from "mongodb";
import { NextRequest } from "next/server";
import { verifySession } from "@/app/_lib/dal";
import { Link } from "@/app/_lib/data/link";
export async function GET(req: NextRequest) {
try {
const collection = await getCollection('link');
// Check if the user is authenticated
const page = parseInt(req.nextUrl.searchParams.get('page') || '1') || 1;
const pageSize = parseInt(req.nextUrl.searchParams.get('pageSize') || '10') || 10;
const typeId = req.nextUrl.searchParams.get('typeId')
// 计算起始索引和结束索引
const startIndex = (page - 1) * pageSize;
// 查询数据
const cursor = collection.find<Link>({ type: typeId }).skip(startIndex).limit(pageSize);
const data = await cursor.toArray();
// 计算总数量
const total = (await collection.find<Link>({ type: typeId }).toArray()).length
return Response.json({
total,
list: data,
})
} catch (e) {
return Response.error()
}
}
export async function DELETE(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
try {
const session = await verifySession()
// Check if the user is authenticated
if (!session) {
// User is not authenticated
return new Response(null, { status: 401 })
}
// 获取路径参数
const slug = (await params).id
const collection = await getCollection('link')
collection.deleteOne({
_id: new ObjectId(slug)
})
return Response.json({ message: '删除成功' })
} catch (e) {
return Response.error()
}
}