2025-01-21 18:41:27 +08:00
|
|
|
import { verifySession } from "@/app/_lib/dal";
|
|
|
|
import { User } from "@/app/_lib/data/user";
|
|
|
|
import { getCollection, getDb } from "@/app/_lib/mongodb";
|
|
|
|
import { message } from "antd";
|
|
|
|
import bcrypt from 'bcrypt';
|
2025-01-22 17:37:56 +08:00
|
|
|
import { ObjectId } from "mongodb";
|
2025-01-21 18:41:27 +08:00
|
|
|
import { NextRequest } from "next/server";
|
2025-01-21 20:34:06 +08:00
|
|
|
export type Link = {
|
2025-01-22 17:37:56 +08:00
|
|
|
name: string;
|
|
|
|
link?: string;
|
2025-01-21 20:34:06 +08:00
|
|
|
description: string;
|
2025-01-22 17:37:56 +08:00
|
|
|
_id: string;
|
2025-01-21 20:34:06 +08:00
|
|
|
type: string;
|
|
|
|
priority: number;
|
|
|
|
logoLink: string;
|
2025-01-21 18:41:27 +08:00
|
|
|
|
2025-01-21 20:34:06 +08:00
|
|
|
}
|
2025-01-21 18:41:27 +08:00
|
|
|
export async function GET(req: NextRequest) {
|
|
|
|
try {
|
|
|
|
const collection = await getCollection('link');
|
|
|
|
// Check if the user is authenticated
|
2025-01-22 17:37:56 +08:00
|
|
|
|
2025-01-21 18:41:27 +08:00
|
|
|
const page = parseInt(req.nextUrl.searchParams.get('page') || '1') || 1;
|
2025-01-22 17:37:56 +08:00
|
|
|
const pageSize = parseInt(req.nextUrl.searchParams.get('pageSize') || '10') || 10;
|
|
|
|
const typeId = req.nextUrl.searchParams.get('typeId')
|
2025-01-21 18:41:27 +08:00
|
|
|
// 计算起始索引和结束索引
|
|
|
|
const startIndex = (page - 1) * pageSize;
|
|
|
|
|
|
|
|
// 查询数据
|
2025-01-22 17:37:56 +08:00
|
|
|
const cursor = collection.find<Link>({ type: typeId }).skip(startIndex).limit(pageSize);
|
2025-01-21 18:41:27 +08:00
|
|
|
const data = await cursor.toArray();
|
|
|
|
|
|
|
|
// 计算总数量
|
2025-01-22 17:37:56 +08:00
|
|
|
const total = (await collection.find<Link>({ type: typeId }).toArray()).length
|
2025-01-21 18:41:27 +08:00
|
|
|
|
|
|
|
return Response.json({
|
|
|
|
total,
|
|
|
|
list: data,
|
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
return Response.error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
|
|
try {
|
|
|
|
// 获取待插入的对象
|
|
|
|
const link = await req.json()
|
|
|
|
const collection = await getCollection('link')
|
|
|
|
await collection.insertOne(link)
|
|
|
|
return Response.json({ message: '成功' })
|
|
|
|
} catch (e) {
|
2025-01-22 17:37:56 +08:00
|
|
|
return Response.error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export async function DELETE(req: NextRequest) {
|
|
|
|
try {
|
|
|
|
// 获取路径参数
|
|
|
|
const segments = req.nextUrl.pathname.split('/')
|
|
|
|
const dynamicParam = segments[segments.length - 1]
|
|
|
|
const collection = await getCollection('link')
|
|
|
|
collection.deleteOne({
|
|
|
|
_id: new ObjectId(dynamicParam)
|
|
|
|
})
|
|
|
|
return Response.json({ message: '删除成功' })
|
|
|
|
} catch (e) {
|
|
|
|
return Response.error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export async function PUT(req: NextRequest) {
|
|
|
|
try {
|
|
|
|
// 获取待更新的对象
|
|
|
|
const link = await req.json() as Link
|
|
|
|
const collection = await getCollection('link')
|
|
|
|
await collection.updateOne({ _id: new ObjectId(link.id) }, { $set: link })
|
|
|
|
return Response.json({ message: '成功' })
|
|
|
|
} catch (e) {
|
2025-01-21 18:41:27 +08:00
|
|
|
return Response.error()
|
|
|
|
}
|
|
|
|
}
|