mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-17 15:36:50 +00:00
Applications Search API
This commit is contained in:
parent
a2d4202a24
commit
fe9fd02b9a
32
app/api/applications/search/route.ts
Normal file
32
app/api/applications/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 applications = await prisma.application.findMany({});
|
||||
|
||||
const fuseOptions = {
|
||||
keys: ['name', 'description'],
|
||||
threshold: 0.3,
|
||||
includeScore: true,
|
||||
};
|
||||
|
||||
const fuse = new Fuse(applications, 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