mirror of
https://github.com/spchcap/speech.capital.git
synced 2026-01-14 08:38:42 +00:00
Feat: Fetch and return user's posts for profile
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
const json=(d,o={})=>{const h=new Headers(o.headers);h.set('Content-Type','application/json');return new Response(JSON.stringify(d),{...o,headers:h})};
|
||||
export async function onRequestGet({env,params}){try{const user=await env.D1_SPCHCAP.prepare('SELECT id,username,created_at FROM users WHERE username=?').bind(params.username).first();if(!user)return json({error:'User not found'},{status:404});const{results:comments}=await env.D1_SPCHCAP.prepare('SELECT c.id,c.content,c.created_at,p.id post_id,p.title post_title,s.name sub_name FROM comments c JOIN posts p ON c.post_id=p.id JOIN subs s ON p.sub_id=s.id WHERE c.user_id=? ORDER BY c.created_at DESC LIMIT 100').bind(user.id).all();return json({user,comments})}catch(e){return json({error:{message:e.message}},{status:500})}}
|
||||
export async function onRequestGet({env,params}){try{const db=env.D1_SPCHCAP,user=await db.prepare('SELECT id,username,created_at FROM users WHERE username=?').bind(params.username).first();if(!user)return json({error:'User not found'},{status:404});const[c,p]=await Promise.all([db.prepare('SELECT c.id,c.content,c.created_at,p.id post_id,p.title post_title,s.name sub_name FROM comments c JOIN posts p ON c.post_id=p.id JOIN subs s ON p.sub_id=s.id WHERE c.user_id=? ORDER BY c.created_at DESC LIMIT 100').bind(user.id).all(),db.prepare('SELECT p.id,p.title,p.created_at,s.name sub_name FROM posts p JOIN subs s ON p.sub_id=s.id WHERE p.user_id=? ORDER BY p.created_at DESC LIMIT 100').bind(user.id).all()]);return json({user,comments:c.results,posts:p.results})}catch(e){return json({error:{message:e.message}},{status:500})}}
|
||||
|
||||
Reference in New Issue
Block a user