This commit is contained in:
1 parent
8efd032cc6
commit
df38dccbf1
735 files changed
+128321
-120870
No files matched your search
+38
-30
@@ -8,37 +8,45 @@ import { prisma } from "@/lib/prisma";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const sp = new URL(req.url).searchParams;
|
||||
const { page, perPage, skip, take } = pagination(sp);
|
||||
const sp = new URL(req.url).searchParams;
|
||||
const { page, perPage, skip, take } = pagination(sp);
|
||||
|
||||
try {
|
||||
const [total, rows] = await Promise.all([
|
||||
prisma.guilds.count(),
|
||||
prisma.guilds.findMany({
|
||||
orderBy: { id: "desc" },
|
||||
skip,
|
||||
take,
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
userId: true,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
try {
|
||||
const [total, rows] = await Promise.all([
|
||||
prisma.guilds.count(),
|
||||
prisma.guilds.findMany({
|
||||
orderBy: { id: "desc" },
|
||||
skip,
|
||||
take,
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
userId: true,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
// Rename userId → ownerId for the public payload.
|
||||
const data = rows.map(({ userId, ...rest }) => ({
|
||||
...rest,
|
||||
ownerId: userId,
|
||||
}));
|
||||
// Rename userId → ownerId for the public payload.
|
||||
const data = rows.map(({ userId, ...rest }) => ({
|
||||
...rest,
|
||||
ownerId: userId,
|
||||
}));
|
||||
|
||||
return apiJson({
|
||||
data,
|
||||
meta: { page, perPage, total, lastPage: Math.max(1, Math.ceil(total / perPage)) },
|
||||
});
|
||||
} catch {
|
||||
// DB unreachable — never 500; return an empty, well-formed payload.
|
||||
return apiJson({ data: [], meta: { page, perPage, total: 0, lastPage: 1 } }, { status: 200 });
|
||||
}
|
||||
return apiJson({
|
||||
data,
|
||||
meta: {
|
||||
page,
|
||||
perPage,
|
||||
total,
|
||||
lastPage: Math.max(1, Math.ceil(total / perPage)),
|
||||
},
|
||||
});
|
||||
} catch {
|
||||
// DB unreachable — never 500; return an empty, well-formed payload.
|
||||
return apiJson(
|
||||
{ data: [], meta: { page, perPage, total: 0, lastPage: 1 } },
|
||||
{ status: 200 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user