upd
This commit is contained in:
79
bun/index.ts
79
bun/index.ts
@@ -126,23 +126,60 @@ const server = Bun.serve({
|
||||
return Response.json(data[0]);
|
||||
},
|
||||
"/api/posts/:slug": async req => {
|
||||
const res = await connection.run(`
|
||||
SELECT
|
||||
*
|
||||
FROM wp_posts
|
||||
WHERE post_name = $slug
|
||||
AND post_status = 'publish'
|
||||
AND post_type IN ('post', 'page')
|
||||
LIMIT 1;
|
||||
`, { slug: req.params.slug.toString() });
|
||||
// First query: Get the post
|
||||
const postRes = await connection.run(`
|
||||
SELECT
|
||||
*
|
||||
FROM wp_posts
|
||||
WHERE post_name = $slug
|
||||
AND post_status = 'publish'
|
||||
AND post_type IN ('post', 'page')
|
||||
LIMIT 1;
|
||||
`, { slug: req.params.slug.toString() });
|
||||
|
||||
const data = await res.getRowObjectsJson()
|
||||
const postData = await postRes.getRowObjectsJson();
|
||||
|
||||
if (data.length === 0) {
|
||||
if (postData.length === 0) {
|
||||
return Response.json(null, { status: 404 });
|
||||
}
|
||||
|
||||
return Response.json(data[0], { status: 200 });
|
||||
const post = postData[0];
|
||||
|
||||
if (post && post.ID) {
|
||||
// Second query: Get all postmeta for this post
|
||||
const metaRes = await connection.run(`
|
||||
SELECT
|
||||
meta_id,
|
||||
meta_key,
|
||||
meta_value
|
||||
FROM wp_postmeta
|
||||
WHERE post_id = $postId;
|
||||
`, { postId: post.ID.toString() });
|
||||
|
||||
const metaData = await metaRes.getRowObjectsJson();
|
||||
|
||||
// Process thumbnail synchronously
|
||||
for (const meta of metaData) {
|
||||
if (meta?.meta_key === '_thumbnail_id' && meta.meta_value) {
|
||||
const thumbRes = await connection.run(`
|
||||
SELECT
|
||||
*
|
||||
FROM wp_posts
|
||||
WHERE ID = $post_id AND post_type = 'attachment';
|
||||
`, { post_id: meta.meta_value.toString() });
|
||||
|
||||
const thumbData = await thumbRes.getRowObjectsJson();
|
||||
post.thumbnail = thumbData[0];
|
||||
break; // Once we find the thumbnail, we can stop looking
|
||||
}
|
||||
}
|
||||
|
||||
// Add the meta data to the post object as an array
|
||||
post.wp_postmeta = metaData;
|
||||
}
|
||||
|
||||
|
||||
return Response.json(post, { status: 200 });
|
||||
},
|
||||
"/api/pages/:slug": async req => {
|
||||
const res = await connection.run(`
|
||||
@@ -180,15 +217,15 @@ const server = Bun.serve({
|
||||
},
|
||||
"/api/home": async req => {
|
||||
const res = await connection.run(`
|
||||
SELECT p.*
|
||||
FROM wp_posts p
|
||||
WHERE p.ID = (
|
||||
SELECT option_value
|
||||
FROM wp_options
|
||||
WHERE option_name = 'page_on_front'
|
||||
LIMIT 1
|
||||
)
|
||||
LIMIT 1;`);
|
||||
SELECT p.*
|
||||
FROM wp_posts p
|
||||
WHERE p.ID = (
|
||||
SELECT option_value
|
||||
FROM wp_options
|
||||
WHERE option_name = 'page_on_front'
|
||||
LIMIT 1
|
||||
)
|
||||
LIMIT 1;`);
|
||||
|
||||
const data = await res.getRowObjectsJson()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user