Skip to content

Commit

Permalink
refactor(api): Improve resource handling in addIsBookmarkedField
Browse files Browse the repository at this point in the history
  • Loading branch information
tobisupreme committed Aug 1, 2023
1 parent a60338c commit 7d3b5c0
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 35 deletions.
7 changes: 3 additions & 4 deletions src/answers/answersService.js
Original file line number Diff line number Diff line change
@@ -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 });
Expand All @@ -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;
Expand Down
24 changes: 0 additions & 24 deletions src/bookmarks/bookmarksService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -187,5 +164,4 @@ module.exports = {
deleteBookmark,
isBookmarkOwner,
addIsBookmarkedField,
addIsBookmarkedField2,
};
6 changes: 3 additions & 3 deletions src/comments/commentsService.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions src/posts/postsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions src/questions/questionsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 7d3b5c0

Please sign in to comment.