57 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-04-12 12:33:37 +02:00
import { NextResponse, NextRequest } from "next/server";
2025-04-13 21:10:17 +02:00
import { prisma } from "@/lib/prisma";
2025-04-12 12:33:37 +02:00
interface GetRequest {
2025-04-14 21:26:12 +02:00
page?: number;
ITEMS_PER_PAGE?: number;
2025-04-12 12:33:37 +02:00
}
export async function POST(request: NextRequest) {
try {
const body: GetRequest = await request.json();
const page = Math.max(1, body.page || 1);
2025-04-14 21:26:12 +02:00
const ITEMS_PER_PAGE = body.ITEMS_PER_PAGE || 4;
2025-04-18 14:35:36 +02:00
const hosts = await prisma.server.findMany({
2025-04-18 16:52:14 +02:00
where: { hostServer: 0 },
2025-04-12 12:33:37 +02:00
skip: (page - 1) * ITEMS_PER_PAGE,
take: ITEMS_PER_PAGE,
orderBy: { name: 'asc' }
});
2025-04-18 14:35:36 +02:00
const hostsWithVms = await Promise.all(
hosts.map(async (host) => {
const vms = await prisma.server.findMany({
2025-04-18 14:35:36 +02:00
where: { hostServer: host.id },
orderBy: { name: 'asc' }
});
// Add isVM flag to VMs
const vmsWithFlag = vms.map(vm => ({
...vm,
isVM: true,
hostedVMs: [] // Initialize empty hostedVMs array for VMs
}));
return {
...host,
isVM: false, // Mark as physical server/not a VM
hostedVMs: vmsWithFlag
};
})
2025-04-18 14:35:36 +02:00
);
const totalHosts = await prisma.server.count({
where: { hostServer: null }
});
const maxPage = Math.ceil(totalHosts / ITEMS_PER_PAGE);
2025-04-12 12:33:37 +02:00
return NextResponse.json({
2025-04-18 14:35:36 +02:00
servers: hostsWithVms,
2025-04-12 12:33:37 +02:00
maxPage
});
} catch (error: any) {
return NextResponse.json({ error: error.message }, { status: 500 });
}
}