This commit is contained in:
Aya Morisawa 2017-02-27 16:50:36 +09:00
parent 3148be7792
commit 7bf27dc4ed
5 changed files with 261 additions and 266 deletions

View file

@ -15,40 +15,39 @@ import Post from '../../models/post';
* @return {Promise<object>} * @return {Promise<object>}
*/ */
module.exports = (params, user) => module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) => {
{ // Get 'post_id' parameter
// Get 'post_id' parameter let postId = params.post_id;
let postId = params.post_id; if (postId === undefined || postId === null) {
if (postId === undefined || postId === null) { return rej('post_id is required');
return rej('post_id is required'); }
}
// Get favoritee // Get favoritee
const post = await Post.findOne({ const post = await Post.findOne({
_id: new mongo.ObjectID(postId) _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();
});

View file

@ -15,38 +15,37 @@ import Post from '../../models/post';
* @return {Promise<object>} * @return {Promise<object>}
*/ */
module.exports = (params, user) => module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) => {
{ // Get 'post_id' parameter
// Get 'post_id' parameter let postId = params.post_id;
let postId = params.post_id; if (postId === undefined || postId === null) {
if (postId === undefined || postId === null) { return rej('post_id is required');
return rej('post_id is required'); }
}
// Get favoritee // Get favoritee
const post = await Post.findOne({ const post = await Post.findOne({
_id: new mongo.ObjectID(postId) _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();
});

View file

@ -17,77 +17,76 @@ import notify from '../../../common/notify';
* @return {Promise<object>} * @return {Promise<object>}
*/ */
module.exports = (params, user) => module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) => {
{ // Get 'post_id' parameter
// Get 'post_id' parameter let postId = params.post_id;
let postId = params.post_id; if (postId === undefined || postId === null) {
if (postId === undefined || postId === null) { return rej('post_id is required');
return rej('post_id is required');
}
// Validate id
if (!mongo.ObjectID.isValid(postId)) {
return rej('incorrect post_id');
}
// Get likee
const post = await Post.findOne({
_id: new mongo.ObjectID(postId)
});
if (post === null) {
return rej('post not found');
}
// 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 }
});
if (exist !== null) {
return rej('already liked');
}
// Create like
await Like.insert({
created_at: new Date(),
post_id: post._id,
user_id: user._id
});
// Send response
res();
// Increment likes count
Post.update({ _id: post._id }, {
$inc: {
likes_count: 1
} }
});
// Increment user likes count // Validate id
User.update({ _id: user._id }, { if (!mongo.ObjectID.isValid(postId)) {
$inc: { return rej('incorrect post_id');
likes_count: 1
} }
});
// Increment user liked count // Get likee
User.update({ _id: post.user_id }, { const post = await Post.findOne({
$inc: { _id: new mongo.ObjectID(postId)
liked_count: 1 });
if (post === null) {
return rej('post not found');
} }
});
// Notify // Myself
notify(post.user_id, user._id, 'like', { if (post.user_id.equals(user._id)) {
post_id: post._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 }
});
if (exist !== null) {
return rej('already liked');
}
// Create like
await Like.insert({
created_at: new Date(),
post_id: post._id,
user_id: user._id
});
// Send response
res();
// 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 liked count
User.update({ _id: post.user_id }, {
$inc: {
liked_count: 1
}
});
// Notify
notify(post.user_id, user._id, 'like', {
post_id: post._id
});
}); });
});

View file

@ -17,69 +17,68 @@ import User from '../../../models/user';
* @return {Promise<object>} * @return {Promise<object>}
*/ */
module.exports = (params, user) => module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) => {
{ // Get 'post_id' parameter
// Get 'post_id' parameter let postId = params.post_id;
let postId = params.post_id; if (postId === undefined || postId === null) {
if (postId === undefined || postId === null) { return rej('post_id is required');
return rej('post_id is required');
}
// Validate id
if (!mongo.ObjectID.isValid(postId)) {
return rej('incorrect post_id');
}
// Get likee
const post = await Post.findOne({
_id: new mongo.ObjectID(postId)
});
if (post === null) {
return rej('post not found');
}
// 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 not liked');
}
// Delete like
await Like.update({
_id: exist._id
}, {
$set: {
deleted_at: new Date()
} }
});
// Send response // Validate id
res(); if (!mongo.ObjectID.isValid(postId)) {
return rej('incorrect post_id');
// Decrement likes count
Post.update({ _id: post._id }, {
$inc: {
likes_count: -1
} }
});
// Decrement user likes count // Get likee
User.update({ _id: user._id }, { const post = await Post.findOne({
$inc: { _id: new mongo.ObjectID(postId)
likes_count: -1 });
}
});
// Decrement user liked count if (post === null) {
User.update({ _id: post.user_id }, { return rej('post not found');
$inc: {
liked_count: -1
} }
// 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 not liked');
}
// Delete like
await Like.update({
_id: exist._id
}, {
$set: {
deleted_at: new Date()
}
});
// Send response
res();
// Decrement likes count
Post.update({ _id: post._id }, {
$inc: {
likes_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
}
});
}); });
});

View file

@ -16,84 +16,83 @@ import notify from '../../../common/notify';
* @return {Promise<object>} * @return {Promise<object>}
*/ */
module.exports = (params, user) => module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) => {
{ // Get 'post_id' parameter
// Get 'post_id' parameter const postId = params.post_id;
const postId = params.post_id; if (postId === undefined || postId === null) {
if (postId === undefined || postId === null) { return rej('post_id is required');
return rej('post_id is required'); }
}
// Validate id // Validate id
if (!mongo.ObjectID.isValid(postId)) { if (!mongo.ObjectID.isValid(postId)) {
return rej('incorrect post_id'); return rej('incorrect post_id');
} }
// Get votee // Get votee
const post = await Post.findOne({ const post = await Post.findOne({
_id: new mongo.ObjectID(postId) _id: new mongo.ObjectID(postId)
});
if (post === null) {
return rej('post 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');
}
// 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
});
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
});
// Send response
res();
const inc = {};
inc[`poll.choices.${findWithAttr(post.poll.choices, 'id', choice)}.votes`] = 1;
console.log(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
});
}); });
if (post === null) {
return rej('post 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');
}
// 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
});
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
});
// Send response
res();
const inc = {};
inc[`poll.choices.${ findWithAttr(post.poll.choices, 'id', choice) }.votes`] = 1;
console.log(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
});
});
function findWithAttr(array, attr, value) { function findWithAttr(array, attr, value) {
for (let i = 0; i < array.length; i += 1) { for (let i = 0; i < array.length; i += 1) {
if(array[i][attr] === value) { if (array[i][attr] === value) {
return i; return i;
} }
} }