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..e60c975 100644 --- a/src/answers/answersService.js +++ b/src/answers/answersService.js @@ -1,4 +1,5 @@ const { Answer, Question } = require('../../model'); +const { addIsBookmarkedField } = require('../bookmarks/bookmarksService'); const getAnswer = async (id) => { const answers = await Answer.find({ id }); @@ -6,14 +7,21 @@ 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, }); + if (!userId) { + answers = answers.map((answer) => { + return { ...answer.toJSON(), isBookmarked: false }; + }); + return answers; + } + + 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 e14f4bf..d45610c 100644 --- a/src/bookmarks/bookmarksService.js +++ b/src/bookmarks/bookmarksService.js @@ -132,6 +132,30 @@ const isBookmarkOwner = async ({ userId, bookmarkId }) => { return userId.toString() === owner.toString(); }; +const addIsBookmarkedField = 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, @@ -139,4 +163,5 @@ module.exports = { updateBookmark, deleteBookmark, isBookmarkOwner, + addIsBookmarkedField, }; 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..3fbcbd7 100644 --- a/src/comments/commentsService.js +++ b/src/comments/commentsService.js @@ -1,4 +1,5 @@ const { Comment, Post } = require('../../model/'); +const { addIsBookmarkedField } = require('../bookmarks/bookmarksService'); const getComment = async (id) => { const comment = await Comment.findById(id); @@ -6,11 +7,19 @@ 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, }); + if (!userId) { + comments = comments.map((comment) => { + return { ...comment.toJSON(), isBookmarked: false }; + }); + return comments; + } + + comments = await addIsBookmarkedField(comments, userId); return comments; }; diff --git a/src/common/pagination/paginationSchema.js b/src/common/pagination/paginationSchema.js index 2d46251..5e9e217 100644 --- a/src/common/pagination/paginationSchema.js +++ b/src/common/pagination/paginationSchema.js @@ -6,6 +6,7 @@ const paginationSchema = searchSchema.keys({ page: Joi.number().integer().min(1).optional(), sort: Joi.string().optional(), isPaginated: Joi.boolean().optional(), + userId: Joi.string().optional(), }); module.exports = paginationSchema; diff --git a/src/posts/postsController.js b/src/posts/postsController.js index 3c0ade3..01d442d 100644 --- a/src/posts/postsController.js +++ b/src/posts/postsController.js @@ -27,7 +27,9 @@ const getAllPosts = async (req, res) => { req.query.tags = tagIds; } - const { data, meta } = await postsService.getPosts(req); + const { data, meta } = await postsService.getPosts(req, { + userId: req.user?._id, + }); new responseHandler(res, data, 200, RESPONSE_MESSAGE.SUCCESS, meta); }; diff --git a/src/posts/postsService.js b/src/posts/postsService.js index cc6896e..1d6eca1 100644 --- a/src/posts/postsService.js +++ b/src/posts/postsService.js @@ -1,12 +1,21 @@ const { Post } = require('../../model'); +const { addIsBookmarkedField } = require('../bookmarks/bookmarksService'); const { apiFeatures } = require('../common'); -const getPosts = async ({ query } = {}) => { +const getPosts = async ({ query } = {}, { userId }) => { const postsQuery = Post.find({}); const posts = await new apiFeatures(postsQuery, query) .filter() .sort() .paginate(); + + if (!userId) { + posts.data = posts.data.map((post) => { + return { ...post.toJSON(), isBookmarked: false }; + }); + return posts; + } + posts.data = await addIsBookmarkedField(posts.data, userId); return posts; }; 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..9264f94 100644 --- a/src/questions/questionsService.js +++ b/src/questions/questionsService.js @@ -1,12 +1,21 @@ 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(); + + if (!userId) { + questions.data = questions.data.map((post) => { + return { ...post.toJSON(), isBookmarked: false }; + }); + return questions; + } + questions.data = await addIsBookmarkedField(questions.data, userId); return questions; };