Skip to content

Commit

Permalink
Merge pull request #198 from AltCamp/isBookmarked-field
Browse files Browse the repository at this point in the history
feat: add is bookmarked field to posts, questions, answers and comments query
  • Loading branch information
tobisupreme committed Aug 1, 2023
2 parents 701a7b4 + 7d3b5c0 commit d0c4687
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/answers/answersController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
14 changes: 11 additions & 3 deletions src/answers/answersService.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
const { Answer, Question } = require('../../model');
const { addIsBookmarkedField } = require('../bookmarks/bookmarksService');

const getAnswer = async (id) => {
const answers = await Answer.find({ 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;
Expand Down
25 changes: 25 additions & 0 deletions src/bookmarks/bookmarksService.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,36 @@ 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,
createBookmark,
updateBookmark,
deleteBookmark,
isBookmarkOwner,
addIsBookmarkedField,
};
3 changes: 2 additions & 1 deletion src/comments/commentsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
13 changes: 11 additions & 2 deletions src/comments/commentsService.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
const { Comment, Post } = require('../../model/');
const { addIsBookmarkedField } = require('../bookmarks/bookmarksService');

const getComment = async (id) => {
const comment = await Comment.findById(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;
};

Expand Down
1 change: 1 addition & 0 deletions src/common/pagination/paginationSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 3 additions & 1 deletion src/posts/postsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
11 changes: 10 additions & 1 deletion src/posts/postsService.js
Original file line number Diff line number Diff line change
@@ -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;
};

Expand Down
4 changes: 3 additions & 1 deletion src/questions/questionsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
11 changes: 10 additions & 1 deletion src/questions/questionsService.js
Original file line number Diff line number Diff line change
@@ -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;
};

Expand Down

0 comments on commit d0c4687

Please sign in to comment.