updated widgets
This commit is contained in:
51
bun/index.ts
51
bun/index.ts
@@ -13,6 +13,9 @@ const server = Bun.serve({
|
||||
const url = new URL(req.url);
|
||||
const page = Math.max(1, Number(url.searchParams.get("page") ?? 1));
|
||||
|
||||
const UPLOADS_BASE_URL =
|
||||
process.env.WP_UPLOADS_URL ?? "https://new.jkhsakha.ru/wp-content/uploads";
|
||||
|
||||
// 1. Load grid config post
|
||||
const gridPost = await connection.run(`
|
||||
SELECT ID, post_title
|
||||
@@ -22,16 +25,15 @@ const server = Bun.serve({
|
||||
`, { id: gridId });
|
||||
|
||||
const [grid] = await gridPost.getRowObjectsJson();
|
||||
if (!grid) {
|
||||
return Response.json(null, { status: 404 });
|
||||
}
|
||||
if (!grid) return Response.json(null, { status: 404 });
|
||||
|
||||
|
||||
// 2. Load grid meta
|
||||
const metaRes = await connection.run(`
|
||||
SELECT meta_key, meta_value
|
||||
FROM wp_postmeta
|
||||
WHERE post_id = $id;
|
||||
`, { id: gridId });
|
||||
SELECT meta_key, meta_value
|
||||
FROM wp_postmeta
|
||||
WHERE post_id = $id;
|
||||
`, { id: gridId });
|
||||
|
||||
const meta = Object.fromEntries(
|
||||
(await metaRes.getRowObjectsJson())
|
||||
@@ -47,33 +49,32 @@ const server = Bun.serve({
|
||||
|
||||
// 3. Total count (for pagination)
|
||||
const totalRes = await connection.run(`
|
||||
SELECT COUNT(*)::int AS total
|
||||
FROM wp_posts
|
||||
WHERE post_status = 'publish'
|
||||
AND post_type = $postType;
|
||||
`, { postType });
|
||||
SELECT COUNT(*)::int AS total
|
||||
FROM wp_posts
|
||||
WHERE post_status = 'publish'
|
||||
AND post_type = $postType;
|
||||
`, { postType });
|
||||
|
||||
const [{ total }] = await totalRes.getRowObjectsJson();
|
||||
const totalPages = Math.max(1, Math.ceil(total / perPage));
|
||||
|
||||
// clamp page if out of range
|
||||
const safePage = Math.min(page, totalPages);
|
||||
const safeOffset = (safePage - 1) * perPage;
|
||||
|
||||
// 4. Fetch paginated posts
|
||||
const postsRes = await connection.run(`
|
||||
SELECT
|
||||
ID,
|
||||
post_title,
|
||||
post_name,
|
||||
post_excerpt,
|
||||
post_date
|
||||
FROM wp_posts
|
||||
WHERE post_status = 'publish'
|
||||
AND post_type = $postType
|
||||
ORDER BY ${orderBy} ${order}
|
||||
LIMIT $perPage OFFSET $offset;
|
||||
`, {
|
||||
SELECT
|
||||
ID,
|
||||
post_title,
|
||||
post_name,
|
||||
post_excerpt,
|
||||
post_date
|
||||
FROM wp_posts
|
||||
WHERE post_status = 'publish'
|
||||
AND post_type = $postType
|
||||
ORDER BY ${orderBy} ${order}
|
||||
LIMIT $perPage OFFSET $offset;
|
||||
`, {
|
||||
postType,
|
||||
perPage,
|
||||
offset: safeOffset
|
||||
|
||||
Reference in New Issue
Block a user