From df92d99a51763acd3895372d05b5f3b902e79943 Mon Sep 17 00:00:00 2001 From: expdsn <18111002318@163.com> Date: Mon, 27 Jan 2025 02:58:43 +0800 Subject: [PATCH] save --- app/_lib/actions/auth.ts | 2 +- app/_lib/data/search.ts | 12 +--- app/admin/(default)/dashboard/search/page.tsx | 58 ++++++++++++++++++ app/api/search/[id]/route.ts | 39 ++++++++++++ app/api/search/route.ts | 61 +++++++++++++++++++ app/api/searchType/[id]/route.ts | 25 ++++++++ app/api/searchType/route.ts | 56 +++++++++++++++++ app/api/test/route.ts | 18 ------ 8 files changed, 242 insertions(+), 29 deletions(-) create mode 100644 app/admin/(default)/dashboard/search/page.tsx create mode 100644 app/api/search/[id]/route.ts create mode 100644 app/api/search/route.ts create mode 100644 app/api/searchType/[id]/route.ts create mode 100644 app/api/searchType/route.ts delete mode 100644 app/api/test/route.ts diff --git a/app/_lib/actions/auth.ts b/app/_lib/actions/auth.ts index eff061e..d4dabed 100644 --- a/app/_lib/actions/auth.ts +++ b/app/_lib/actions/auth.ts @@ -40,7 +40,7 @@ export async function login(state: FormState, formData: FormData) { await createSession(user._id) // 5. Redirect user - redirect('/admin') + redirect('/admin')p } export async function logout() { diff --git a/app/_lib/data/search.ts b/app/_lib/data/search.ts index 22f5da1..2e81218 100644 --- a/app/_lib/data/search.ts +++ b/app/_lib/data/search.ts @@ -6,6 +6,7 @@ export type SearchTypeItem = { name: string; key: string; includes: string[]; + _id: string; priority: number; } @@ -13,7 +14,7 @@ export type SearchWayItemType = { label: string; value: string; fullName: string; - key: string; + _id: string; } export async function getSearchTypeList({ page = 1, pageSize = 9999 }: { @@ -68,12 +69,3 @@ export async function getSearchWayList({ page = 1, pageSize = 9999 }: { list } } -export async function addSearchType(searchTypeItem: SearchTypeItem) { - const collection = await getCollection('search-type') - collection.insertOne(searchTypeItem) -} -export async function addSearchWay(searchItem: SearchWayItemType) { - const collection = await getCollection('search') - collection.insertOne(searchItem) - -} \ No newline at end of file diff --git a/app/admin/(default)/dashboard/search/page.tsx b/app/admin/(default)/dashboard/search/page.tsx new file mode 100644 index 0000000..2853f9e --- /dev/null +++ b/app/admin/(default)/dashboard/search/page.tsx @@ -0,0 +1,58 @@ +import { SearchTypeItem, SearchWayItemType } from "@/app/_lib/data/search"; +import { mRequest } from "@/app/_lib/request"; +import { useAntdTable, useRequest } from "ahooks"; +import { Button, Card, Space, Table } from "antd"; + +export default function Page() { + const searchWayList = useRequest(async () => mRequest('GET', "/api/search")) + const { data } = useAntdTable(async () => mRequest<{ + total: number; + list: SearchTypeItem[] + }>('GET', "/api/")) + return + {/* + 添加}> + + columns={[ + { + title: '分类名称', + dataIndex: 'name' + }, + { + title: '标示符', + dataIndex: 'key', + + }, + { + title: + } + ]} + > + + + */} + + + columns={[ + { + title: '名称', + dataIndex: 'label', + + }, + { + title: '全称', + dataIndex: 'fullName', + }, + { + title: '搜索链接', + dataIndex: 'value' + } + ]} + > + + + + + + +} \ No newline at end of file diff --git a/app/api/search/[id]/route.ts b/app/api/search/[id]/route.ts new file mode 100644 index 0000000..9b7add4 --- /dev/null +++ b/app/api/search/[id]/route.ts @@ -0,0 +1,39 @@ +import { getCollection, getDb } from "@/app/_lib/mongodb"; +import { ObjectId } from "mongodb"; +import { NextRequest } from "next/server"; + +import { verifySession } from "@/app/_lib/dal"; +import { getLinkList, Link } from "@/app/_lib/data/link"; + +export async function GET(req: NextRequest) { + try { + 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 res = await getLinkList({ page, pageSize, typeId }) + return Response.json(res) + } 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() + } +} diff --git a/app/api/search/route.ts b/app/api/search/route.ts new file mode 100644 index 0000000..6180cc3 --- /dev/null +++ b/app/api/search/route.ts @@ -0,0 +1,61 @@ +import { verifySession } from "@/app/_lib/dal"; +import { Link } from "@/app/_lib/data/link"; +import { getSearchWayList } from "@/app/_lib/data/search"; +import { User } from "@/app/_lib/data/user"; +import { getCollection, getDb } from "@/app/_lib/mongodb"; +import { message } from "antd"; +import bcrypt from 'bcrypt'; +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; + + const res = getSearchWayList({ page, pageSize }) + 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 }) + } + // 获取待更新的对象 + const link = await req.json() as Link + const collection = await getCollection('link') + await collection.replaceOne({ _id: new ObjectId(link._id) }, { ...link, _id: new ObjectId(link._id) }) + return Response.json({ message: '成功' }) + } catch (e) { + return Response.error() + } +} \ No newline at end of file diff --git a/app/api/searchType/[id]/route.ts b/app/api/searchType/[id]/route.ts new file mode 100644 index 0000000..868350d --- /dev/null +++ b/app/api/searchType/[id]/route.ts @@ -0,0 +1,25 @@ +import { getCollection, getDb } from "@/app/_lib/mongodb"; +import { ObjectId } from "mongodb"; +import { NextRequest } from "next/server"; +import { verifySession } from "@/app/_lib/dal"; + +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('search-type') + collection.deleteOne({ + _id: new ObjectId(slug) + }) + return Response.json({ message: '删除成功' }) + } catch (e) { + return Response.error() + } +} diff --git a/app/api/searchType/route.ts b/app/api/searchType/route.ts new file mode 100644 index 0000000..87b84ee --- /dev/null +++ b/app/api/searchType/route.ts @@ -0,0 +1,56 @@ +import { verifySession } from "@/app/_lib/dal"; +import { getSearchTypeList, SearchTypeItem } from "@/app/_lib/data/search"; +import { getCollection } 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; + + const res = getSearchTypeList({ page, pageSize }) + 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 collection = await getCollection('search-type') + const item = req.json() + + const res = collection.insertOne(item) + return Response.json(res) + } 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 }) + } + // 获取待更新的对象 + const collection = await getCollection('search-type') + const item = await req.json() as SearchTypeItem + const res = await collection.replaceOne({ _id: new ObjectId(item._id) }, { ...item, _id: new ObjectId(item._id) }) + return Response.json(res) + } catch (e) { + return Response.error() + } +} \ No newline at end of file diff --git a/app/api/test/route.ts b/app/api/test/route.ts deleted file mode 100644 index 03ee541..0000000 --- a/app/api/test/route.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { getCollection, getDb } from "@/app/_lib/mongodb"; -import bcrypt from 'bcrypt'; -import { headers } from "next/headers"; -import { NextRequest } from "next/server"; - -export async function GET(request: NextRequest) { - try { - const referer = request.nextUrl.searchParams.get('page') - return Response.json({ - referer - }) - } catch (e) { - console.log(e); - return Response.json('遇到了一些问题') - } - - -} \ No newline at end of file