mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-18 07:56:57 +00:00
Servers Search API
This commit is contained in:
parent
06b422bfe9
commit
1373c5b92e
32
app/api/servers/search/route.ts
Normal file
32
app/api/servers/search/route.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { NextResponse, NextRequest } from "next/server";
|
||||||
|
import { prisma } from "@/lib/prisma";
|
||||||
|
import Fuse from "fuse.js";
|
||||||
|
|
||||||
|
interface SearchRequest {
|
||||||
|
searchterm: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function POST(request: NextRequest) {
|
||||||
|
try {
|
||||||
|
const body: SearchRequest = await request.json();
|
||||||
|
const { searchterm } = body;
|
||||||
|
|
||||||
|
const servers = await prisma.server.findMany({});
|
||||||
|
|
||||||
|
const fuseOptions = {
|
||||||
|
keys: ['name', 'description'],
|
||||||
|
threshold: 0.3,
|
||||||
|
includeScore: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
const fuse = new Fuse(servers, fuseOptions);
|
||||||
|
|
||||||
|
const searchResults = fuse.search(searchterm);
|
||||||
|
|
||||||
|
const results = searchResults.map(({ item }) => item);
|
||||||
|
|
||||||
|
return NextResponse.json({ results });
|
||||||
|
} catch (error: any) {
|
||||||
|
return NextResponse.json({ error: error.message }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user