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
|
|
|
|
|
|
|
|
// Host-Server mit Paginierung holen
|
|
|
|
|
const hosts = await prisma.server.findMany({
|
|
|
|
|
where: { hostServer: null },
|
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
|
|
|
// VMs für alle Hosts sammeln
|
|
|
|
|
const hostsWithVms = await Promise.all(
|
|
|
|
|
hosts.map(async (host) => ({
|
|
|
|
|
...host,
|
|
|
|
|
hostedVMs: await prisma.server.findMany({
|
|
|
|
|
where: { hostServer: host.id },
|
|
|
|
|
orderBy: { name: 'asc' }
|
|
|
|
|
})
|
|
|
|
|
}))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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 });
|
|
|
|
|
}
|
|
|
|
|
}
|