mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-20 08:56:29 +00:00
65 lines
2.2 KiB
TypeScript
65 lines
2.2 KiB
TypeScript
|
|
import { NextRequest, NextResponse } from "next/server";
|
||
|
|
import prisma from "@/app/prisma";
|
||
|
|
import Fuse from 'fuse.js';
|
||
|
|
import { z } from "zod/v4";
|
||
|
|
|
||
|
|
const schema = z.object({
|
||
|
|
currentPage: z.string().optional(),
|
||
|
|
itemPerPage: z.string().optional(),
|
||
|
|
search: z.string().optional(),
|
||
|
|
});
|
||
|
|
|
||
|
|
export async function GET(request: NextRequest) {
|
||
|
|
const { searchParams } = request.nextUrl;
|
||
|
|
const currentPage = Number(schema.parse({ currentPage: searchParams.get("currentPage") }).currentPage) || 1;
|
||
|
|
const itemPerPage = Number(schema.parse({ itemPerPage: searchParams.get("itemPerPage") }).itemPerPage) || 10;
|
||
|
|
const search = schema.parse({ search: searchParams.get("search") }).search || "";
|
||
|
|
|
||
|
|
try {
|
||
|
|
if (!search) {
|
||
|
|
const skip = (currentPage - 1) * itemPerPage;
|
||
|
|
const take = itemPerPage;
|
||
|
|
|
||
|
|
const servers = await prisma.server.findMany({
|
||
|
|
skip,
|
||
|
|
take,
|
||
|
|
orderBy: {
|
||
|
|
name: 'asc',
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
const total = await prisma.server.count();
|
||
|
|
return NextResponse.json({ servers, total, currentPage, itemPerPage }, { status: 200 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const allServers = await prisma.server.findMany({
|
||
|
|
orderBy: {
|
||
|
|
name: 'asc',
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
const fuseOptions = {
|
||
|
|
includeScore: true,
|
||
|
|
threshold: 0.4,
|
||
|
|
keys: ['name', 'description', 'networks.name']
|
||
|
|
};
|
||
|
|
|
||
|
|
const fuse = new Fuse(allServers, fuseOptions);
|
||
|
|
const searchResults = fuse.search(search);
|
||
|
|
|
||
|
|
const servers = searchResults.map((result) => result.item);
|
||
|
|
|
||
|
|
const total = searchResults.length;
|
||
|
|
const skip = (currentPage - 1) * itemPerPage;
|
||
|
|
const take = itemPerPage;
|
||
|
|
|
||
|
|
const paginatedServers = servers.slice(skip, skip + take);
|
||
|
|
|
||
|
|
return NextResponse.json({ servers: paginatedServers, total, currentPage, itemPerPage }, { status: 200 });
|
||
|
|
} catch (error: any) {
|
||
|
|
if(error instanceof z.ZodError) {
|
||
|
|
return NextResponse.json({ error: error.issues[0].message }, { status: 400 });
|
||
|
|
}
|
||
|
|
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
|
||
|
|
}
|
||
|
|
}
|