From 0bcca86e7eec9fbfbe9b5fdd6449831cc6394e48 Mon Sep 17 00:00:00 2001 From: Skywonda Date: Tue, 4 Jul 2023 01:03:12 +0100 Subject: [PATCH 1/7] new: add userId to pagination schema --- src/common/pagination/paginationSchema.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/common/pagination/paginationSchema.js b/src/common/pagination/paginationSchema.js index 1069156..ec63ead 100644 --- a/src/common/pagination/paginationSchema.js +++ b/src/common/pagination/paginationSchema.js @@ -6,6 +6,7 @@ const paginationSchema = Joi.object().keys({ sort: Joi.string().optional(), searchTerm: Joi.string().optional(), isPaginated: Joi.boolean().optional(), + userId: Joi.string().optional(), }); module.exports = paginationSchema; From 6b318a3f1d883a99e8770d1d6f3b0b58eb37633d Mon Sep 17 00:00:00 2001 From: Skywonda Date: Tue, 4 Jul 2023 01:04:47 +0100 Subject: [PATCH 2/7] new: add isBooked marked functionality to getPosts query --- src/posts/postsController.js | 4 +++- src/posts/postsService.js | 27 ++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/posts/postsController.js b/src/posts/postsController.js index a229577..2e88cc7 100644 --- a/src/posts/postsController.js +++ b/src/posts/postsController.js @@ -18,7 +18,9 @@ const getPost = async (req, res) => { }; const getAllPosts = async (req, res) => { - const { data, meta } = await postsService.getPosts(req); + const { data, meta } = await postsService.getPosts(req, { + userId: req.query.userId, + }); new responseHandler(res, data, 200, RESPONSE_MESSAGE.SUCCESS, meta); }; diff --git a/src/posts/postsService.js b/src/posts/postsService.js index 5d61012..f4be4a8 100644 --- a/src/posts/postsService.js +++ b/src/posts/postsService.js @@ -1,16 +1,37 @@ const { AUTHOR_DETAILS } = require('../../constant'); -const { Post } = require('../../model'); +const { Post, Bookmark } = require('../../model'); const { apiFeatures } = require('../common'); -const getPosts = async ({ query } = {}) => { +const getPosts = async ({ query } = {}, { userId }) => { const postsQuery = Post.find({}).populate({ path: 'author', select: Object.values(AUTHOR_DETAILS), }); - const posts = await new apiFeatures(postsQuery, query) + let posts = await new apiFeatures(postsQuery, query) .filter() .sort() .paginate(); + + if (!userId) { + posts.data = posts.data.map((post) => { + return { ...post.toJSON(), isBookmarked: false }; + }); + return posts; + } + const postIds = posts.data.map((post) => post.id); + let bookmarks = await Bookmark.find({ + id: { $in: [postIds] }, + owner: userId, + }); + bookmarks = bookmarks.map((each) => each.post.toString()); + posts.data = posts.data.map((post) => { + if (bookmarks.includes(post.id)) { + return { ...post.toJSON(), isBookmarked: true }; + } else { + return { ...post.toJSON(), isBookmarked: false }; + } + }); + return posts; }; From 127c997b881a52fa826557e7f0ade0223a56b5cf Mon Sep 17 00:00:00 2001 From: Tobi Balogun <98078707+tobisupreme@users.noreply.github.com> Date: Tue, 1 Aug 2023 02:34:46 +0100 Subject: [PATCH 3/7] fix(api): Update userId handling in API endpoint --- src/posts/postsController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/posts/postsController.js b/src/posts/postsController.js index ec32dd3..01d442d 100644 --- a/src/posts/postsController.js +++ b/src/posts/postsController.js @@ -28,7 +28,7 @@ const getAllPosts = async (req, res) => { } const { data, meta } = await postsService.getPosts(req, { - userId: req.query.userId, + userId: req.user?._id, }); new responseHandler(res, data, 200, RESPONSE_MESSAGE.SUCCESS, meta); }; From d875c0626297580afa0a5f4609713790a57457fc Mon Sep 17 00:00:00 2001 From: Tobi Balogun <98078707+tobisupreme@users.noreply.github.com> Date: Tue, 1 Aug 2023 03:00:21 +0100 Subject: [PATCH 4/7] refactor(api): Separate bookmark checking into bookmark service --- src/bookmarks/bookmarksService.js | 24 ++++++++++++++++++++++++ src/posts/postsService.js | 20 ++++---------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/bookmarks/bookmarksService.js b/src/bookmarks/bookmarksService.js index e14f4bf..34e0f41 100644 --- a/src/bookmarks/bookmarksService.js +++ b/src/bookmarks/bookmarksService.js @@ -132,6 +132,29 @@ const isBookmarkOwner = async ({ userId, bookmarkId }) => { return userId.toString() === owner.toString(); }; +const addIsBookmarkedField = async (resources, userId) => { + const _resources = { ...resources }; + try { + const resourceIds = _resources.data.map((resource) => resource.id); + let bookmarks = await Bookmark.find({ + id: { $in: [resourceIds] }, + owner: userId, + }); + bookmarks = bookmarks.map((each) => each.post.toString()); + + _resources.data = _resources.data.map((resource) => { + if (bookmarks.includes(resource.id)) { + return { ...resource.toJSON(), isBookmarked: true }; + } else { + return { ...resource.toJSON(), isBookmarked: false }; + } + }); + return _resources; + } catch (error) { + return resources; + } +}; + module.exports = { getBookmarks, getBookmark, @@ -139,4 +162,5 @@ module.exports = { updateBookmark, deleteBookmark, isBookmarkOwner, + addIsBookmarkedField, }; diff --git a/src/posts/postsService.js b/src/posts/postsService.js index 58a2c36..024c11d 100644 --- a/src/posts/postsService.js +++ b/src/posts/postsService.js @@ -1,4 +1,5 @@ -const { Post, Bookmark } = require('../../model'); +const { Post } = require('../../model'); +const { addIsBookmarkedField } = require('../bookmarks/bookmarksService'); const { apiFeatures } = require('../common'); const getPosts = async ({ query } = {}, { userId }) => { @@ -14,21 +15,8 @@ const getPosts = async ({ query } = {}, { userId }) => { }); return posts; } - const postIds = posts.data.map((post) => post.id); - let bookmarks = await Bookmark.find({ - id: { $in: [postIds] }, - owner: userId, - }); - bookmarks = bookmarks.map((each) => each.post.toString()); - posts.data = posts.data.map((post) => { - if (bookmarks.includes(post.id)) { - return { ...post.toJSON(), isBookmarked: true }; - } else { - return { ...post.toJSON(), isBookmarked: false }; - } - }); - - return posts; + const postsWithBookmarks = await addIsBookmarkedField(posts, userId); + return postsWithBookmarks; }; const getPost = async (postId) => { From 6f56aaf0e12bef624e04457a69fccb3c886af8c3 Mon Sep 17 00:00:00 2001 From: Tobi Balogun <98078707+tobisupreme@users.noreply.github.com> Date: Tue, 1 Aug 2023 10:06:53 +0100 Subject: [PATCH 5/7] feat: add isBookmarked field to questions --- src/questions/questionsController.js | 4 +++- src/questions/questionsService.js | 13 +++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/questions/questionsController.js b/src/questions/questionsController.js index 6ec2228..e426cf2 100644 --- a/src/questions/questionsController.js +++ b/src/questions/questionsController.js @@ -27,7 +27,9 @@ const getAllQuestions = async (req, res) => { req.query.tags = tagIds; } - const { data, meta } = await questionsService.getQuestions(req); + const { data, meta } = await questionsService.getQuestions(req, { + userId: req.user?._id, + }); new responseHandler(res, data, 200, RESPONSE_MESSAGE.SUCCESS, meta); }; diff --git a/src/questions/questionsService.js b/src/questions/questionsService.js index 0d636c4..4a17c15 100644 --- a/src/questions/questionsService.js +++ b/src/questions/questionsService.js @@ -1,13 +1,22 @@ const { Question } = require('../../model'); +const { addIsBookmarkedField } = require('../bookmarks/bookmarksService'); const { apiFeatures } = require('../common'); -const getQuestions = async ({ query }) => { +const getQuestions = async ({ query } = {}, { userId }) => { const questionsQuery = Question.find({}); const questions = await new apiFeatures(questionsQuery, query) .filter() .sort() .paginate(); - return questions; + + if (!userId) { + questions.data = questions.data.map((post) => { + return { ...post.toJSON(), isBookmarked: false }; + }); + return questions; + } + const questionsWithBookmarks = await addIsBookmarkedField(questions, userId); + return questionsWithBookmarks; }; const getQuestion = async (questionId) => { From a60338c0c2c20809b94cc4e1b938294b59d6485a Mon Sep 17 00:00:00 2001 From: Tobi Balogun <98078707+tobisupreme@users.noreply.github.com> Date: Tue, 1 Aug 2023 10:19:44 +0100 Subject: [PATCH 6/7] feat: add isBookmarked field to answers and comments --- src/answers/answersController.js | 3 ++- src/answers/answersService.js | 15 ++++++++++++--- src/bookmarks/bookmarksService.js | 25 +++++++++++++++++++++++++ src/comments/commentsController.js | 3 ++- src/comments/commentsService.js | 15 ++++++++++++--- 5 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/answers/answersController.js b/src/answers/answersController.js index 8dafead..406dcb4 100644 --- a/src/answers/answersController.js +++ b/src/answers/answersController.js @@ -17,8 +17,9 @@ const getAnswer = async (req, res) => { const getAnswers = async (req, res) => { const { questionId } = req.query; + const userId = req.user?._id; - const answers = await answerService.getAnswers(questionId); + const answers = await answerService.getAnswers(questionId, userId); new responseHandler(res, answers, 200, RESPONSE_MESSAGE.SUCCESS); }; diff --git a/src/answers/answersService.js b/src/answers/answersService.js index 11561d2..aab7ab3 100644 --- a/src/answers/answersService.js +++ b/src/answers/answersService.js @@ -1,4 +1,5 @@ const { Answer, Question } = require('../../model'); +const { addIsBookmarkedField2 } = require('../bookmarks/bookmarksService'); const getAnswer = async (id) => { const answers = await Answer.find({ id }); @@ -6,12 +7,20 @@ const getAnswer = async (id) => { return answers; }; -const getAnswers = async (questionId) => { - const answers = await Answer.find({ +const getAnswers = async (questionId, userId) => { + let answers = await Answer.find({ question: questionId, }); - return answers; + if (!userId) { + answers = answers.map((answer) => { + return { ...answer.toJSON(), isBookmarked: false }; + }); + return answers; + } + + const answersWithBookmarks = await addIsBookmarkedField2(answers, userId); + return answersWithBookmarks; }; const createAnswer = async (answer) => { diff --git a/src/bookmarks/bookmarksService.js b/src/bookmarks/bookmarksService.js index 34e0f41..749c5b7 100644 --- a/src/bookmarks/bookmarksService.js +++ b/src/bookmarks/bookmarksService.js @@ -155,6 +155,30 @@ const addIsBookmarkedField = async (resources, userId) => { } }; +const addIsBookmarkedField2 = async (resources, userId) => { + let _resources = [...resources]; + try { + const resourceIds = _resources.map((resource) => resource._id.toString()); + + let bookmarks = await Bookmark.find({ + id: { $in: [resourceIds] }, + owner: userId, + }); + bookmarks = bookmarks.map((each) => each.post.toString()); + + _resources = _resources.map((resource) => { + if (bookmarks.includes(resource.id)) { + return { ...resource.toJSON(), isBookmarked: true }; + } else { + return { ...resource.toJSON(), isBookmarked: false }; + } + }); + return _resources; + } catch (error) { + return resources; + } +}; + module.exports = { getBookmarks, getBookmark, @@ -163,4 +187,5 @@ module.exports = { deleteBookmark, isBookmarkOwner, addIsBookmarkedField, + addIsBookmarkedField2, }; diff --git a/src/comments/commentsController.js b/src/comments/commentsController.js index 3d3faa9..b13ca0d 100644 --- a/src/comments/commentsController.js +++ b/src/comments/commentsController.js @@ -17,8 +17,9 @@ const getComment = async (req, res) => { const getComments = async (req, res) => { const { postId } = req.query; + const userId = req.user?._id; - const comments = await commentService.getComments(postId); + const comments = await commentService.getComments(postId, userId); new responseHandler(res, comments, 200, RESPONSE_MESSAGE.SUCCESS); }; diff --git a/src/comments/commentsService.js b/src/comments/commentsService.js index 89fe591..0c7efbb 100644 --- a/src/comments/commentsService.js +++ b/src/comments/commentsService.js @@ -1,4 +1,5 @@ const { Comment, Post } = require('../../model/'); +const { addIsBookmarkedField2 } = require('../bookmarks/bookmarksService'); const getComment = async (id) => { const comment = await Comment.findById(id); @@ -6,12 +7,20 @@ const getComment = async (id) => { return comment; }; -const getComments = async (postId) => { - const comments = await Comment.find({ +const getComments = async (postId, userId) => { + let comments = await Comment.find({ post: postId, }); - return comments; + if (!userId) { + comments = comments.map((comment) => { + return { ...comment.toJSON(), isBookmarked: false }; + }); + return comments; + } + + const commentsWithBookmarks = await addIsBookmarkedField2(comments, userId); + return commentsWithBookmarks; }; const createComment = async (comment) => { From 7d3b5c0a5be718a09f8edf161bf9dffa3f75a2f0 Mon Sep 17 00:00:00 2001 From: Tobi Balogun <98078707+tobisupreme@users.noreply.github.com> Date: Tue, 1 Aug 2023 13:09:31 +0100 Subject: [PATCH 7/7] refactor(api): Improve resource handling in addIsBookmarkedField --- src/answers/answersService.js | 7 +++---- src/bookmarks/bookmarksService.js | 24 ------------------------ src/comments/commentsService.js | 6 +++--- src/posts/postsService.js | 4 ++-- src/questions/questionsService.js | 4 ++-- 5 files changed, 10 insertions(+), 35 deletions(-) diff --git a/src/answers/answersService.js b/src/answers/answersService.js index aab7ab3..e60c975 100644 --- a/src/answers/answersService.js +++ b/src/answers/answersService.js @@ -1,5 +1,5 @@ const { Answer, Question } = require('../../model'); -const { addIsBookmarkedField2 } = require('../bookmarks/bookmarksService'); +const { addIsBookmarkedField } = require('../bookmarks/bookmarksService'); const getAnswer = async (id) => { const answers = await Answer.find({ id }); @@ -19,10 +19,9 @@ const getAnswers = async (questionId, userId) => { return answers; } - const answersWithBookmarks = await addIsBookmarkedField2(answers, userId); - return answersWithBookmarks; + answers = await addIsBookmarkedField(answers, userId); + return answers; }; - const createAnswer = async (answer) => { const newAnswer = await Answer.create(answer); newAnswer.question = answer.question; diff --git a/src/bookmarks/bookmarksService.js b/src/bookmarks/bookmarksService.js index 749c5b7..d45610c 100644 --- a/src/bookmarks/bookmarksService.js +++ b/src/bookmarks/bookmarksService.js @@ -133,29 +133,6 @@ const isBookmarkOwner = async ({ userId, bookmarkId }) => { }; const addIsBookmarkedField = async (resources, userId) => { - const _resources = { ...resources }; - try { - const resourceIds = _resources.data.map((resource) => resource.id); - let bookmarks = await Bookmark.find({ - id: { $in: [resourceIds] }, - owner: userId, - }); - bookmarks = bookmarks.map((each) => each.post.toString()); - - _resources.data = _resources.data.map((resource) => { - if (bookmarks.includes(resource.id)) { - return { ...resource.toJSON(), isBookmarked: true }; - } else { - return { ...resource.toJSON(), isBookmarked: false }; - } - }); - return _resources; - } catch (error) { - return resources; - } -}; - -const addIsBookmarkedField2 = async (resources, userId) => { let _resources = [...resources]; try { const resourceIds = _resources.map((resource) => resource._id.toString()); @@ -187,5 +164,4 @@ module.exports = { deleteBookmark, isBookmarkOwner, addIsBookmarkedField, - addIsBookmarkedField2, }; diff --git a/src/comments/commentsService.js b/src/comments/commentsService.js index 0c7efbb..3fbcbd7 100644 --- a/src/comments/commentsService.js +++ b/src/comments/commentsService.js @@ -1,5 +1,5 @@ const { Comment, Post } = require('../../model/'); -const { addIsBookmarkedField2 } = require('../bookmarks/bookmarksService'); +const { addIsBookmarkedField } = require('../bookmarks/bookmarksService'); const getComment = async (id) => { const comment = await Comment.findById(id); @@ -19,8 +19,8 @@ const getComments = async (postId, userId) => { return comments; } - const commentsWithBookmarks = await addIsBookmarkedField2(comments, userId); - return commentsWithBookmarks; + comments = await addIsBookmarkedField(comments, userId); + return comments; }; const createComment = async (comment) => { diff --git a/src/posts/postsService.js b/src/posts/postsService.js index 024c11d..1d6eca1 100644 --- a/src/posts/postsService.js +++ b/src/posts/postsService.js @@ -15,8 +15,8 @@ const getPosts = async ({ query } = {}, { userId }) => { }); return posts; } - const postsWithBookmarks = await addIsBookmarkedField(posts, userId); - return postsWithBookmarks; + posts.data = await addIsBookmarkedField(posts.data, userId); + return posts; }; const getPost = async (postId) => { diff --git a/src/questions/questionsService.js b/src/questions/questionsService.js index 4a17c15..9264f94 100644 --- a/src/questions/questionsService.js +++ b/src/questions/questionsService.js @@ -15,8 +15,8 @@ const getQuestions = async ({ query } = {}, { userId }) => { }); return questions; } - const questionsWithBookmarks = await addIsBookmarkedField(questions, userId); - return questionsWithBookmarks; + questions.data = await addIsBookmarkedField(questions.data, userId); + return questions; }; const getQuestion = async (questionId) => {