refactor(db): migrate app pages and APIs from Prisma facade to Drizzle (5)
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
9aa4f331bf
commit
30b54e99e7
40 files changed
+1183
-784
No files matched your search
@@ -1,22 +1,26 @@
|
||||
// Public REST: website store categories (website_shop_categories).
|
||||
// AtomCMS JSON API parity — read-only list ordered by `order` then name.
|
||||
import { asc } from "drizzle-orm";
|
||||
import { apiJson } from "@/lib/api";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { db, WebsiteShopCategories } from "@/lib/db";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(_req: Request) {
|
||||
try {
|
||||
const data = await prisma.websiteShopCategories.findMany({
|
||||
orderBy: [{ order: "asc" }, { name: "asc" }],
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
icon: true,
|
||||
order: true,
|
||||
},
|
||||
});
|
||||
const data = await db
|
||||
.select({
|
||||
id: WebsiteShopCategories.id,
|
||||
name: WebsiteShopCategories.name,
|
||||
description: WebsiteShopCategories.description,
|
||||
icon: WebsiteShopCategories.icon,
|
||||
order: WebsiteShopCategories.order,
|
||||
})
|
||||
.from(WebsiteShopCategories)
|
||||
.orderBy(
|
||||
asc(WebsiteShopCategories.order),
|
||||
asc(WebsiteShopCategories.name),
|
||||
);
|
||||
|
||||
return apiJson({ data });
|
||||
} catch {
|
||||
|
||||
+40
-30
@@ -1,8 +1,9 @@
|
||||
// Public REST: website store packages (website_shop_articles).
|
||||
// AtomCMS JSON API parity — read-only list of buyable packages, paginated and
|
||||
// ordered by `position` (then name), matching the admin /admin/shop query.
|
||||
import { asc, count, eq } from "drizzle-orm";
|
||||
import { apiError, apiJson, pagination, positiveBigInt } from "@/lib/api";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { db, WebsiteShopArticles } from "@/lib/db";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
@@ -12,40 +13,49 @@ export async function GET(req: Request) {
|
||||
|
||||
// Optional ?category=<id> filter (website_shop_category_id is a BigInt).
|
||||
const categoryRaw = sp.get("category");
|
||||
let where: { categoryId?: bigint } = {};
|
||||
let categoryId: bigint | undefined;
|
||||
if (categoryRaw !== null) {
|
||||
const categoryId = positiveBigInt(categoryRaw);
|
||||
if (!categoryId) return apiError("A valid category id is required", 422);
|
||||
where = { categoryId };
|
||||
const parsed = positiveBigInt(categoryRaw);
|
||||
if (!parsed) return apiError("A valid category id is required", 422);
|
||||
categoryId = parsed;
|
||||
}
|
||||
|
||||
try {
|
||||
const [total, data] = await Promise.all([
|
||||
prisma.websiteShopArticles.count({ where }),
|
||||
prisma.websiteShopArticles.findMany({
|
||||
where,
|
||||
orderBy: [{ position: "asc" }, { name: "asc" }],
|
||||
skip,
|
||||
take,
|
||||
select: {
|
||||
id: true,
|
||||
categoryId: true,
|
||||
name: true,
|
||||
info: true,
|
||||
iconUrl: true,
|
||||
color: true,
|
||||
costs: true,
|
||||
giveRank: true,
|
||||
isGiftable: true,
|
||||
credits: true,
|
||||
duckets: true,
|
||||
diamonds: true,
|
||||
badges: true,
|
||||
furniture: true,
|
||||
position: true,
|
||||
},
|
||||
}),
|
||||
const where =
|
||||
categoryId !== undefined
|
||||
? eq(WebsiteShopArticles.categoryId, categoryId)
|
||||
: undefined;
|
||||
|
||||
const [totalRows, data] = await Promise.all([
|
||||
db.select({ total: count() }).from(WebsiteShopArticles).where(where),
|
||||
db
|
||||
.select({
|
||||
id: WebsiteShopArticles.id,
|
||||
categoryId: WebsiteShopArticles.categoryId,
|
||||
name: WebsiteShopArticles.name,
|
||||
info: WebsiteShopArticles.info,
|
||||
iconUrl: WebsiteShopArticles.iconUrl,
|
||||
color: WebsiteShopArticles.color,
|
||||
costs: WebsiteShopArticles.costs,
|
||||
giveRank: WebsiteShopArticles.giveRank,
|
||||
isGiftable: WebsiteShopArticles.isGiftable,
|
||||
credits: WebsiteShopArticles.credits,
|
||||
duckets: WebsiteShopArticles.duckets,
|
||||
diamonds: WebsiteShopArticles.diamonds,
|
||||
badges: WebsiteShopArticles.badges,
|
||||
furniture: WebsiteShopArticles.furniture,
|
||||
position: WebsiteShopArticles.position,
|
||||
})
|
||||
.from(WebsiteShopArticles)
|
||||
.where(where)
|
||||
.orderBy(
|
||||
asc(WebsiteShopArticles.position),
|
||||
asc(WebsiteShopArticles.name),
|
||||
)
|
||||
.limit(take)
|
||||
.offset(skip),
|
||||
]);
|
||||
const total = totalRows[0]?.total ?? 0;
|
||||
|
||||
return apiJson({
|
||||
data,
|
||||
|
||||
Reference in new issue
Block a user