diff --git a/functions/api/posts/[id].js b/functions/api/posts/[id].js index 4dd35b2..47da0d8 100644 --- a/functions/api/posts/[id].js +++ b/functions/api/posts/[id].js @@ -7,7 +7,7 @@ const auth=async(req,db)=>{const c=req.headers.get('Cookie')||'',u=cookie(c),h=h export async function onRequestGet({request,env,params}){ try{ const id=params.id,user=await auth(request,env.D1_SPCHCAP); - const post=await env.D1_SPCHCAP.prepare(`SELECT p.id,p.user_id,p.title,p.link,p.content,p.score,p.comment_count,p.created_at,p.post_type,u.username${user?',v.direction as voted':''} FROM posts p JOIN users u ON p.user_id=u.id ${user?'LEFT JOIN votes v ON v.post_id=p.id AND v.user_id=?':''} WHERE p.id=?`).bind(...(user?[user.id,id]:[id])).first(); + const post=await env.D1_SPCHCAP.prepare(`SELECT p.id,p.user_id,p.title,p.link,p.content,p.score,p.comment_count,p.created_at,u.username${user?',v.direction as voted':''} FROM posts p JOIN users u ON p.user_id=u.id ${user?'LEFT JOIN votes v ON v.post_id=p.id AND v.user_id=?':''} WHERE p.id=?`).bind(...(user?[user.id,id]:[id])).first(); if(!post)return json({error:'Not found'},{status:404},request); const{results}=await env.D1_SPCHCAP.prepare(`SELECT c.id,c.user_id,c.content,c.score,c.parent_id,c.created_at,u.username${user?',v.direction as voted':''} FROM comments c JOIN users u ON c.user_id=u.id ${user?'LEFT JOIN votes v ON v.comment_id=c.id AND v.user_id=?':''} WHERE c.post_id=? ORDER BY c.created_at ASC`).bind(...(user?[user.id,id]:[id])).all(); @@ -18,3 +18,4 @@ export async function onRequestGet({request,env,params}){ return json({post,comments:tree},{},request); }catch(e){return json({error:{message:e.message}},{status:500},request)} } +