Replaced the dedicated /admin/subscriptions page with a redirect to the central admin panel’s subscriptions tab. Removed session checks, imports, and the AdminSubscriptionDashboard since routing and access are now handled centrally. This streamlines admin UX and keeps access control unified.
79 lines
2.3 KiB
TypeScript
79 lines
2.3 KiB
TypeScript
import { NextRequest } from "next/server";
|
|
import { searchItems, type ItemSort } from "@/lib/prisma/queries/items";
|
|
import { buildPublicItemImageUrl } from "@/lib/item-images";
|
|
|
|
function parseArray(param: string | null): string[] {
|
|
if (!param) return [];
|
|
return param
|
|
.split(",")
|
|
.map((value) => value.trim())
|
|
.filter(Boolean);
|
|
}
|
|
|
|
const allowedSorts: ItemSort[] = [
|
|
"name",
|
|
"level-asc",
|
|
"level-desc",
|
|
"weight-asc",
|
|
"weight-desc",
|
|
"category",
|
|
"updated-desc",
|
|
];
|
|
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
const { searchParams } = new URL(req.url);
|
|
const q = searchParams.get("q") || undefined;
|
|
const categories = parseArray(searchParams.get("category"));
|
|
const rarities = parseArray(searchParams.get("rarity"));
|
|
const server = searchParams.get("server") || undefined;
|
|
const sortParamRaw = (searchParams.get("sort") || "name").toLowerCase();
|
|
const sortParam = (allowedSorts.find((sort) => sort === sortParamRaw) || "name") as ItemSort;
|
|
const page = Number(searchParams.get("page") || "1");
|
|
const pageSize = Number(searchParams.get("pageSize") || "24");
|
|
|
|
const { items, total } = await searchItems({
|
|
q,
|
|
category: categories[0],
|
|
rarity: rarities[0],
|
|
server,
|
|
sort: sortParam,
|
|
page,
|
|
pageSize,
|
|
});
|
|
|
|
const itemsWithImage = items.map((item) => {
|
|
const img = (item.image_url || item.image_local_path || "").toString();
|
|
if (/^https?:\/\//i.test(img)) {
|
|
return { ...item, image_url: img, image: img };
|
|
}
|
|
if (img.startsWith("data/")) {
|
|
return { ...item, image_url: img, image: `/api/images?path=${encodeURIComponent(img)}` };
|
|
}
|
|
const category = item.category || "other";
|
|
return {
|
|
...item,
|
|
image_url: item.image_url,
|
|
image: buildPublicItemImageUrl(category, img),
|
|
};
|
|
});
|
|
|
|
const totalPages = Math.max(1, Math.ceil(total / Math.max(1, pageSize)));
|
|
return Response.json(
|
|
{ items: itemsWithImage, page, pageSize, total, totalPages, sort: sortParam },
|
|
{ status: 200 }
|
|
);
|
|
} catch (error: any) {
|
|
console.error("[api/items] Erro:", error?.message, error?.stack);
|
|
return Response.json(
|
|
{
|
|
error: "Falha ao consultar itens",
|
|
message: error?.message || "Erro desconhecido",
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
|