diff --git a/src/api/endpoints/posts/favorites/create.js b/src/api/endpoints/posts/favorites/create.js index f2e29bbc39497921c1b59c3a5134d660a768af15..21ba9ecb2702f7de8c5ce6ab0f92750bb0402af2 100644 --- a/src/api/endpoints/posts/favorites/create.js +++ b/src/api/endpoints/posts/favorites/create.js @@ -15,40 +15,39 @@ import Post from '../../models/post'; * @return {Promise<object>} */ module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'post_id' parameter - let postId = params.post_id; - if (postId === undefined || postId === null) { - return rej('post_id is required'); - } - - // Get favoritee - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) + new Promise(async (res, rej) => { + // Get 'post_id' parameter + let postId = params.post_id; + if (postId === undefined || postId === null) { + return rej('post_id is required'); + } + + // Get favoritee + const post = await Post.findOne({ + _id: new mongo.ObjectID(postId) + }); + + if (post === null) { + return rej('post not found'); + } + + // Check already favorited + const exist = await Favorite.findOne({ + post_id: post._id, + user_id: user._id + }); + + if (exist !== null) { + return rej('already favorited'); + } + + // Create favorite + await Favorite.insert({ + created_at: new Date(), + post_id: post._id, + user_id: user._id + }); + + // Send response + res(); }); - - if (post === null) { - return rej('post not found'); - } - - // Check arleady favorited - const exist = await Favorite.findOne({ - post_id: post._id, - user_id: user._id - }); - - if (exist !== null) { - return rej('already favorited'); - } - - // Create favorite - await Favorite.insert({ - created_at: new Date(), - post_id: post._id, - user_id: user._id - }); - - // Send response - res(); -}); diff --git a/src/api/endpoints/posts/favorites/delete.js b/src/api/endpoints/posts/favorites/delete.js index e250d1772c2931a9eff9b2cb004f985cc53a2494..974852db213033c0def1c8da5a7ba0c11c1e8e99 100644 --- a/src/api/endpoints/posts/favorites/delete.js +++ b/src/api/endpoints/posts/favorites/delete.js @@ -15,38 +15,37 @@ import Post from '../../models/post'; * @return {Promise<object>} */ module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'post_id' parameter - let postId = params.post_id; - if (postId === undefined || postId === null) { - return rej('post_id is required'); - } - - // Get favoritee - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) + new Promise(async (res, rej) => { + // Get 'post_id' parameter + let postId = params.post_id; + if (postId === undefined || postId === null) { + return rej('post_id is required'); + } + + // Get favoritee + const post = await Post.findOne({ + _id: new mongo.ObjectID(postId) + }); + + if (post === null) { + return rej('post not found'); + } + + // Check arleady favorited + const exist = await Favorite.findOne({ + post_id: post._id, + user_id: user._id + }); + + if (exist === null) { + return rej('already not favorited'); + } + + // Delete favorite + await Favorite.deleteOne({ + _id: exist._id + }); + + // Send response + res(); }); - - if (post === null) { - return rej('post not found'); - } - - // Check arleady favorited - const exist = await Favorite.findOne({ - post_id: post._id, - user_id: user._id - }); - - if (exist === null) { - return rej('already not favorited'); - } - - // Delete favorite - await Favorite.deleteOne({ - _id: exist._id - }); - - // Send response - res(); -}); diff --git a/src/api/endpoints/posts/likes/create.js b/src/api/endpoints/posts/likes/create.js index 83debea960f52be5c4920c1df087df39947d9947..bae8aecc0c67d11bf57dad0b05cc14a7905bbc50 100644 --- a/src/api/endpoints/posts/likes/create.js +++ b/src/api/endpoints/posts/likes/create.js @@ -17,77 +17,76 @@ import notify from '../../../common/notify'; * @return {Promise<object>} */ module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'post_id' parameter - let postId = params.post_id; - if (postId === undefined || postId === null) { - return rej('post_id is required'); - } + new Promise(async (res, rej) => { + // Get 'post_id' parameter + let postId = params.post_id; + if (postId === undefined || postId === null) { + return rej('post_id is required'); + } - // Validate id - if (!mongo.ObjectID.isValid(postId)) { - return rej('incorrect post_id'); - } + // Validate id + if (!mongo.ObjectID.isValid(postId)) { + return rej('incorrect post_id'); + } - // Get likee - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) - }); + // Get likee + const post = await Post.findOne({ + _id: new mongo.ObjectID(postId) + }); - if (post === null) { - return rej('post not found'); - } + if (post === null) { + return rej('post not found'); + } - // Myself - if (post.user_id.equals(user._id)) { - return rej('-need-translate-'); - } + // Myself + if (post.user_id.equals(user._id)) { + return rej('-need-translate-'); + } - // Check arleady liked - const exist = await Like.findOne({ - post_id: post._id, - user_id: user._id, - deleted_at: { $exists: false } - }); + // Check arleady liked + const exist = await Like.findOne({ + post_id: post._id, + user_id: user._id, + deleted_at: { $exists: false } + }); - if (exist !== null) { - return rej('already liked'); - } + if (exist !== null) { + return rej('already liked'); + } - // Create like - await Like.insert({ - created_at: new Date(), - post_id: post._id, - user_id: user._id - }); + // Create like + await Like.insert({ + created_at: new Date(), + post_id: post._id, + user_id: user._id + }); - // Send response - res(); + // Send response + res(); - // Increment likes count - Post.update({ _id: post._id }, { - $inc: { - likes_count: 1 - } - }); + // Increment likes count + Post.update({ _id: post._id }, { + $inc: { + likes_count: 1 + } + }); - // Increment user likes count - User.update({ _id: user._id }, { - $inc: { - likes_count: 1 - } - }); + // Increment user likes count + User.update({ _id: user._id }, { + $inc: { + likes_count: 1 + } + }); - // Increment user liked count - User.update({ _id: post.user_id }, { - $inc: { - liked_count: 1 - } - }); + // Increment user liked count + User.update({ _id: post.user_id }, { + $inc: { + liked_count: 1 + } + }); - // Notify - notify(post.user_id, user._id, 'like', { - post_id: post._id + // Notify + notify(post.user_id, user._id, 'like', { + post_id: post._id + }); }); -}); diff --git a/src/api/endpoints/posts/likes/delete.js b/src/api/endpoints/posts/likes/delete.js index e3dee23bf2dd31a1b1bb2429bce664fe1a2a3957..c2b8f53375b088de625aa6f5e6d4d0d3cf1dd90a 100644 --- a/src/api/endpoints/posts/likes/delete.js +++ b/src/api/endpoints/posts/likes/delete.js @@ -17,69 +17,68 @@ import User from '../../../models/user'; * @return {Promise<object>} */ module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'post_id' parameter - let postId = params.post_id; - if (postId === undefined || postId === null) { - return rej('post_id is required'); - } - - // Validate id - if (!mongo.ObjectID.isValid(postId)) { - return rej('incorrect post_id'); - } + new Promise(async (res, rej) => { + // Get 'post_id' parameter + let postId = params.post_id; + if (postId === undefined || postId === null) { + return rej('post_id is required'); + } - // Get likee - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) - }); + // Validate id + if (!mongo.ObjectID.isValid(postId)) { + return rej('incorrect post_id'); + } - if (post === null) { - return rej('post not found'); - } + // Get likee + const post = await Post.findOne({ + _id: new mongo.ObjectID(postId) + }); - // Check arleady liked - const exist = await Like.findOne({ - post_id: post._id, - user_id: user._id, - deleted_at: { $exists: false } - }); + if (post === null) { + return rej('post not found'); + } - if (exist === null) { - return rej('already not liked'); - } + // Check arleady liked + const exist = await Like.findOne({ + post_id: post._id, + user_id: user._id, + deleted_at: { $exists: false } + }); - // Delete like - await Like.update({ - _id: exist._id - }, { - $set: { - deleted_at: new Date() + if (exist === null) { + return rej('already not liked'); } - }); - // Send response - res(); + // Delete like + await Like.update({ + _id: exist._id + }, { + $set: { + deleted_at: new Date() + } + }); - // Decrement likes count - Post.update({ _id: post._id }, { - $inc: { - likes_count: -1 - } - }); + // Send response + res(); - // Decrement user likes count - User.update({ _id: user._id }, { - $inc: { - likes_count: -1 - } - }); + // Decrement likes count + Post.update({ _id: post._id }, { + $inc: { + likes_count: -1 + } + }); - // Decrement user liked count - User.update({ _id: post.user_id }, { - $inc: { - liked_count: -1 - } + // Decrement user likes count + User.update({ _id: user._id }, { + $inc: { + likes_count: -1 + } + }); + + // Decrement user liked count + User.update({ _id: post.user_id }, { + $inc: { + liked_count: -1 + } + }); }); -}); diff --git a/src/api/endpoints/posts/polls/vote.js b/src/api/endpoints/posts/polls/vote.js index f1842069d4a723c86265207ed74c1f3dfcd3bf95..40cd8a7e2c369b9b88c8bafe63126a2165748b77 100644 --- a/src/api/endpoints/posts/polls/vote.js +++ b/src/api/endpoints/posts/polls/vote.js @@ -16,84 +16,83 @@ import notify from '../../../common/notify'; * @return {Promise<object>} */ module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'post_id' parameter - const postId = params.post_id; - if (postId === undefined || postId === null) { - return rej('post_id is required'); - } + new Promise(async (res, rej) => { + // Get 'post_id' parameter + const postId = params.post_id; + if (postId === undefined || postId === null) { + return rej('post_id is required'); + } - // Validate id - if (!mongo.ObjectID.isValid(postId)) { - return rej('incorrect post_id'); - } + // Validate id + if (!mongo.ObjectID.isValid(postId)) { + return rej('incorrect post_id'); + } - // Get votee - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) - }); + // Get votee + const post = await Post.findOne({ + _id: new mongo.ObjectID(postId) + }); - if (post === null) { - return rej('post not found'); - } + if (post === null) { + return rej('post not found'); + } - if (post.poll == null) { - return rej('poll not found'); - } + if (post.poll == null) { + return rej('poll not found'); + } - // Get 'choice' parameter - const choice = params.choice; - if (choice == null) { - return rej('choice is required'); - } + // Get 'choice' parameter + const choice = params.choice; + if (choice == null) { + return rej('choice is required'); + } - // Validate choice - if (!post.poll.choices.some(x => x.id == choice)) { - return rej('invalid choice'); - } + // Validate choice + if (!post.poll.choices.some(x => x.id == choice)) { + return rej('invalid choice'); + } - // Check arleady voted - const exist = await Vote.findOne({ - post_id: post._id, - user_id: user._id - }); + // Check arleady voted + const exist = await Vote.findOne({ + post_id: post._id, + user_id: user._id + }); - if (exist !== null) { - return rej('already voted'); - } + if (exist !== null) { + return rej('already voted'); + } - // Create vote - await Vote.insert({ - created_at: new Date(), - post_id: post._id, - user_id: user._id, - choice: choice - }); + // Create vote + await Vote.insert({ + created_at: new Date(), + post_id: post._id, + user_id: user._id, + choice: choice + }); - // Send response - res(); + // Send response + res(); - const inc = {}; - inc[`poll.choices.${ findWithAttr(post.poll.choices, 'id', choice) }.votes`] = 1; + const inc = {}; + inc[`poll.choices.${findWithAttr(post.poll.choices, 'id', choice)}.votes`] = 1; - console.log(inc); + console.log(inc); - // Increment likes count - Post.update({ _id: post._id }, { - $inc: inc - }); + // Increment likes count + Post.update({ _id: post._id }, { + $inc: inc + }); - // Notify - notify(post.user_id, user._id, 'poll_vote', { - post_id: post._id, - choice: choice + // Notify + notify(post.user_id, user._id, 'poll_vote', { + post_id: post._id, + choice: choice + }); }); -}); function findWithAttr(array, attr, value) { for (let i = 0; i < array.length; i += 1) { - if(array[i][attr] === value) { + if (array[i][attr] === value) { return i; } }