2025-01-27 02:58:43 +08:00
|
|
|
import { verifySession } from "@/app/_lib/dal";
|
|
|
|
import { Link } from "@/app/_lib/data/link";
|
|
|
|
import { getSearchWayList } from "@/app/_lib/data/search";
|
|
|
|
import { getCollection, getDb } from "@/app/_lib/mongodb";
|
|
|
|
import { ObjectId } from "mongodb";
|
|
|
|
import { NextRequest } from "next/server";
|
|
|
|
|
|
|
|
export async function GET(req: NextRequest) {
|
|
|
|
try {
|
|
|
|
// 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;
|
|
|
|
|
2025-02-07 19:02:22 +08:00
|
|
|
const res = await getSearchWayList({ page, pageSize })
|
2025-01-27 02:58:43 +08:00
|
|
|
return Response.json(res)
|
|
|
|
} catch (e) {
|
|
|
|
return Response.error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
|
|
try {
|
|
|
|
|
|
|
|
const session = await verifySession()
|
|
|
|
|
|
|
|
// Check if the user is authenticated
|
|
|
|
if (!session) {
|
|
|
|
// User is not authenticated
|
|
|
|
return new Response(null, { status: 401 })
|
|
|
|
}
|
|
|
|
// 获取待插入的对象
|
|
|
|
const link = await req.json()
|
|
|
|
const collection = await getCollection('search')
|
|
|
|
await collection.insertOne(link)
|
|
|
|
return Response.json({ message: '成功' })
|
|
|
|
} catch (e) {
|
|
|
|
return Response.error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function PUT(req: NextRequest) {
|
|
|
|
try {
|
|
|
|
const session = await verifySession()
|
|
|
|
|
|
|
|
// Check if the user is authenticated
|
|
|
|
if (!session) {
|
|
|
|
// User is not authenticated
|
|
|
|
return new Response(null, { status: 401 })
|
|
|
|
}
|
2025-02-07 19:02:22 +08:00
|
|
|
|
|
|
|
|
2025-01-27 02:58:43 +08:00
|
|
|
// 获取待更新的对象
|
|
|
|
const link = await req.json() as Link
|
2025-02-07 19:02:22 +08:00
|
|
|
const collection = await getCollection('search')
|
2025-01-27 02:58:43 +08:00
|
|
|
await collection.replaceOne({ _id: new ObjectId(link._id) }, { ...link, _id: new ObjectId(link._id) })
|
2025-02-07 19:02:22 +08:00
|
|
|
|
2025-01-27 02:58:43 +08:00
|
|
|
return Response.json({ message: '成功' })
|
2025-02-07 19:02:22 +08:00
|
|
|
|
2025-01-27 02:58:43 +08:00
|
|
|
} catch (e) {
|
|
|
|
return Response.error()
|
|
|
|
}
|
|
|
|
}
|