45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
import { getCollection, getDb } from "@/app/_lib/mongodb";
|
|
import { ObjectId } from "mongodb";
|
|
import { NextRequest } from "next/server";
|
|
import { Link } from "../route";
|
|
|
|
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 slug = (await params).id
|
|
const collection = await getCollection('link')
|
|
collection.deleteOne({
|
|
_id: new ObjectId(slug)
|
|
})
|
|
return Response.json({ message: '删除成功' })
|
|
} catch (e) {
|
|
return Response.error()
|
|
}
|
|
}
|