From f909233533211f57849a8c176c7115b15c81052e Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Sun, 15 Aug 2021 22:19:35 -0700 Subject: [PATCH] post by user --- dbTools/post.js | 5 +++-- routes/post.js | 9 ++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/dbTools/post.js b/dbTools/post.js index 4176505..e142e25 100644 --- a/dbTools/post.js +++ b/dbTools/post.js @@ -77,8 +77,9 @@ postDB = (DB)=>{ }); } - DB.getPosts = (userObj) => { - return DB.postCols.find().sort({_id: -1}).toArray().catch((err)=>{ + DB.getPosts = (userid) => { + let query = userid ? {userid} : {}; + return DB.postCols.find(query).sort({_id: -1}).toArray().catch((err)=>{ console.log(err); return false; }); diff --git a/routes/post.js b/routes/post.js index f2d7140..ed5e13e 100644 --- a/routes/post.js +++ b/routes/post.js @@ -16,6 +16,12 @@ DB.getDB.then((DB)=>{ return res.json(posts) }); + router.get("/usr/:id", async (req, res) => { + const userid = req.params.id; + const posts = await DB.getPosts(userid); + return res.json(posts) + }); + router.get("/new", async (req, res) => { let post = { userid: getUserId(req), @@ -46,7 +52,8 @@ DB.getDB.then((DB)=>{ r = await DB.newComment(postid, comment); console.log(r) return res.json({ - status: "ok" + status: "ok", + ...comment }) });