1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-12-11 21:28:14 +09:00
This commit is contained in:
syuilo 2017-03-03 08:00:10 +09:00
parent 0926d5b6da
commit 583b64331b
6 changed files with 31 additions and 67 deletions

View File

@ -3,9 +3,9 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../../it';
import Favorite from '../../models/favorite'; import Favorite from '../../../models/favorite';
import Post from '../../models/post'; import Post from '../../../models/post';
/** /**
* Favorite a post * Favorite a post
@ -17,10 +17,8 @@ import Post from '../../models/post';
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; const [postId, postIdErr] = it(params.post_id, 'id', true);
if (postId === undefined || postId === null) { if (postIdErr) return rej('invalid post_id param');
return rej('post_id is required');
}
// Get favoritee // Get favoritee
const post = await Post.findOne({ const post = await Post.findOne({

View File

@ -3,9 +3,9 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../../it';
import Favorite from '../../models/favorite'; import Favorite from '../../../models/favorite';
import Post from '../../models/post'; import Post from '../../../models/post';
/** /**
* Unfavorite a post * Unfavorite a post
@ -17,10 +17,8 @@ import Post from '../../models/post';
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; const [postId, postIdErr] = it(params.post_id, 'id', true);
if (postId === undefined || postId === null) { if (postIdErr) return rej('invalid post_id param');
return rej('post_id is required');
}
// Get favoritee // Get favoritee
const post = await Post.findOne({ const post = await Post.findOne({

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../../it';
import Like from '../../../models/like'; import Like from '../../../models/like';
import Post from '../../../models/post'; import Post from '../../../models/post';
import User from '../../../models/user'; import User from '../../../models/user';
@ -19,19 +19,12 @@ import notify from '../../../common/notify';
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; const [postId, postIdErr] = it(params.post_id, 'id', true);
if (postId === undefined || postId === null) { if (postIdErr) return rej('invalid post_id param');
return rej('post_id is required');
}
// Validate id
if (!mongo.ObjectID.isValid(postId)) {
return rej('incorrect post_id');
}
// Get likee // Get likee
const post = await Post.findOne({ const post = await Post.findOne({
_id: new mongo.ObjectID(postId) _id: postId
}); });
if (post === null) { if (post === null) {

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../../it';
import Like from '../../../models/like'; import Like from '../../../models/like';
import Post from '../../../models/post'; import Post from '../../../models/post';
import User from '../../../models/user'; import User from '../../../models/user';
@ -19,19 +19,12 @@ import User from '../../../models/user';
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; const [postId, postIdErr] = it(params.post_id, 'id', true);
if (postId === undefined || postId === null) { if (postIdErr) return rej('invalid post_id param');
return rej('post_id is required');
}
// Validate id
if (!mongo.ObjectID.isValid(postId)) {
return rej('incorrect post_id');
}
// Get likee // Get likee
const post = await Post.findOne({ const post = await Post.findOne({
_id: new mongo.ObjectID(postId) _id: postId
}); });
if (post === null) { if (post === null) {

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../../it';
import Vote from '../../../models/poll-vote'; import Vote from '../../../models/poll-vote';
import Post from '../../../models/post'; import Post from '../../../models/post';
import notify from '../../../common/notify'; import notify from '../../../common/notify';
@ -18,19 +18,12 @@ import notify from '../../../common/notify';
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, postIdErr] = it(params.post_id, 'id', true);
if (postId === undefined || postId === null) { if (postIdErr) return rej('invalid post_id param');
return rej('post_id is required');
}
// Validate id
if (!mongo.ObjectID.isValid(postId)) {
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: postId
}); });
if (post === null) { if (post === null) {
@ -42,15 +35,12 @@ module.exports = (params, user) =>
} }
// Get 'choice' parameter // Get 'choice' parameter
const choice = params.choice; const [choice, choiceError] =
if (choice == null) { it(params.choice).expect.string()
return rej('choice is required'); .required()
} .validate(c => post.poll.choices.some(x => x.id == c))
.qed();
// Validate choice if (choiceError) return rej('invalid choice param');
if (!post.poll.choices.some(x => x.id == choice)) {
return rej('invalid choice');
}
// if already voted // if already voted
const exist = await Vote.findOne({ const exist = await Vote.findOne({
@ -76,8 +66,6 @@ module.exports = (params, user) =>
const inc = {}; const inc = {};
inc[`poll.choices.${findWithAttr(post.poll.choices, 'id', choice)}.votes`] = 1; inc[`poll.choices.${findWithAttr(post.poll.choices, 'id', choice)}.votes`] = 1;
console.log(inc);
// Increment likes count // Increment likes count
Post.update({ _id: post._id }, { Post.update({ _id: post._id }, {
$inc: inc $inc: inc

View File

@ -3,6 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import it from '../../it';
import User from '../../models/user'; import User from '../../models/user';
import { validateUsername } from '../../models/user'; import { validateUsername } from '../../models/user';
@ -16,15 +17,8 @@ module.exports = async (params) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'username' parameter // Get 'username' parameter
const username = params.username; const [username, usernameError] = it(params.username).expect.string().required().trim().validate(validateUsername).qed();
if (username == null || username == '') { if (usernameError) return rej('invalid username param');
return rej('username-is-required');
}
// Validate username
if (!validateUsername(username)) {
return rej('invalid-username');
}
// Get exist // Get exist
const exist = await User const exist = await User