This commit is contained in:
1 parent
8efd032cc6
commit
df38dccbf1
735 files changed
+128321
-120870
No files matched your search
@@ -6,62 +6,65 @@ import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(_req: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
const { id } = await params;
|
||||
export async function GET(
|
||||
_req: Request,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
const { id } = await params;
|
||||
|
||||
// The primary key is a BigInt; reject non-numeric ids up front.
|
||||
if (!/^\d+$/.test(id)) {
|
||||
return apiJson({ error: "Rare value not found" }, { status: 404 });
|
||||
}
|
||||
// The primary key is a BigInt; reject non-numeric ids up front.
|
||||
if (!/^\d+$/.test(id)) {
|
||||
return apiJson({ error: "Rare value not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
let valueId: bigint;
|
||||
try {
|
||||
valueId = BigInt(id);
|
||||
} catch {
|
||||
return apiJson({ error: "Rare value not found" }, { status: 404 });
|
||||
}
|
||||
let valueId: bigint;
|
||||
try {
|
||||
valueId = BigInt(id);
|
||||
} catch {
|
||||
return apiJson({ error: "Rare value not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
try {
|
||||
const value = await prisma.websiteRareValues.findUnique({
|
||||
where: { id: valueId },
|
||||
select: {
|
||||
id: true,
|
||||
categoryId: true,
|
||||
itemId: true,
|
||||
name: true,
|
||||
creditValue: true,
|
||||
currencyValue: true,
|
||||
currencyType: true,
|
||||
furnitureIcon: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
});
|
||||
try {
|
||||
const value = await prisma.websiteRareValues.findUnique({
|
||||
where: { id: valueId },
|
||||
select: {
|
||||
id: true,
|
||||
categoryId: true,
|
||||
itemId: true,
|
||||
name: true,
|
||||
creditValue: true,
|
||||
currencyValue: true,
|
||||
currencyType: true,
|
||||
furnitureIcon: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!value) {
|
||||
return apiJson({ error: "Rare value not found" }, { status: 404 });
|
||||
}
|
||||
if (!value) {
|
||||
return apiJson({ error: "Rare value not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
// No relation is modelled between the value and its category, so resolve the
|
||||
// category in a second guarded query.
|
||||
let category: {
|
||||
id: bigint;
|
||||
name: string;
|
||||
badge: string;
|
||||
priority: number;
|
||||
} | null = null;
|
||||
try {
|
||||
category = await prisma.websiteRareValueCategories.findUnique({
|
||||
where: { id: value.categoryId },
|
||||
select: { id: true, name: true, badge: true, priority: true },
|
||||
});
|
||||
} catch {
|
||||
category = null;
|
||||
}
|
||||
// No relation is modelled between the value and its category, so resolve the
|
||||
// category in a second guarded query.
|
||||
let category: {
|
||||
id: bigint;
|
||||
name: string;
|
||||
badge: string;
|
||||
priority: number;
|
||||
} | null = null;
|
||||
try {
|
||||
category = await prisma.websiteRareValueCategories.findUnique({
|
||||
where: { id: value.categoryId },
|
||||
select: { id: true, name: true, badge: true, priority: true },
|
||||
});
|
||||
} catch {
|
||||
category = null;
|
||||
}
|
||||
|
||||
return apiJson({ data: { ...value, category } });
|
||||
} catch {
|
||||
// DB unavailable — treat as not found rather than a 500.
|
||||
return apiJson({ error: "Rare value not found" }, { status: 200 });
|
||||
}
|
||||
return apiJson({ data: { ...value, category } });
|
||||
} catch {
|
||||
// DB unavailable — treat as not found rather than a 500.
|
||||
return apiJson({ error: "Rare value not found" }, { status: 200 });
|
||||
}
|
||||
}
|
||||
@@ -7,20 +7,20 @@ import { prisma } from "@/lib/prisma";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(_req: Request) {
|
||||
try {
|
||||
const data = await prisma.websiteRareValueCategories.findMany({
|
||||
orderBy: [{ priority: "asc" }, { name: "asc" }],
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
badge: true,
|
||||
priority: true,
|
||||
},
|
||||
});
|
||||
try {
|
||||
const data = await prisma.websiteRareValueCategories.findMany({
|
||||
orderBy: [{ priority: "asc" }, { name: "asc" }],
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
badge: true,
|
||||
priority: true,
|
||||
},
|
||||
});
|
||||
|
||||
return apiJson({ data });
|
||||
} catch {
|
||||
// DB unreachable — never 500; return empty data.
|
||||
return apiJson({ data: [] }, { status: 200 });
|
||||
}
|
||||
return apiJson({ data });
|
||||
} catch {
|
||||
// DB unreachable — never 500; return empty data.
|
||||
return apiJson({ data: [] }, { status: 200 });
|
||||
}
|
||||
}
|
||||
+46
-38
@@ -7,45 +7,53 @@ 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);
|
||||
|
||||
// Optional ?category=<id> filter (category_id is a BigInt).
|
||||
const categoryRaw = sp.get("category");
|
||||
let where: { categoryId?: bigint } = {};
|
||||
if (categoryRaw !== null) {
|
||||
const categoryId = positiveBigInt(categoryRaw);
|
||||
if (!categoryId) return apiError("A valid category id is required", 422);
|
||||
where = { categoryId };
|
||||
}
|
||||
// Optional ?category=<id> filter (category_id is a BigInt).
|
||||
const categoryRaw = sp.get("category");
|
||||
let where: { categoryId?: bigint } = {};
|
||||
if (categoryRaw !== null) {
|
||||
const categoryId = positiveBigInt(categoryRaw);
|
||||
if (!categoryId) return apiError("A valid category id is required", 422);
|
||||
where = { categoryId };
|
||||
}
|
||||
|
||||
try {
|
||||
const [total, data] = await Promise.all([
|
||||
prisma.websiteRareValues.count({ where }),
|
||||
prisma.websiteRareValues.findMany({
|
||||
where,
|
||||
orderBy: { name: "asc" },
|
||||
skip,
|
||||
take,
|
||||
select: {
|
||||
id: true,
|
||||
categoryId: true,
|
||||
itemId: true,
|
||||
name: true,
|
||||
creditValue: true,
|
||||
currencyValue: true,
|
||||
currencyType: true,
|
||||
furnitureIcon: true,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
try {
|
||||
const [total, data] = await Promise.all([
|
||||
prisma.websiteRareValues.count({ where }),
|
||||
prisma.websiteRareValues.findMany({
|
||||
where,
|
||||
orderBy: { name: "asc" },
|
||||
skip,
|
||||
take,
|
||||
select: {
|
||||
id: true,
|
||||
categoryId: true,
|
||||
itemId: true,
|
||||
name: true,
|
||||
creditValue: true,
|
||||
currencyValue: true,
|
||||
currencyType: true,
|
||||
furnitureIcon: true,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
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