From 583b64331b4d3d36f642801c847109b8634df1d9 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 08:00:10 +0900 Subject: [PATCH] wip --- .../posts/favorites/{create.js => create.ts} | 12 +++---- .../posts/favorites/{delete.js => delete.ts} | 12 +++---- .../posts/likes/{create.js => create.ts} | 15 +++------ .../posts/likes/{delete.js => delete.ts} | 15 +++------ .../posts/polls/{vote.js => vote.ts} | 32 ++++++------------- .../username/{available.js => available.ts} | 12 ++----- 6 files changed, 31 insertions(+), 67 deletions(-) rename src/api/endpoints/posts/favorites/{create.js => create.ts} (75%) rename src/api/endpoints/posts/favorites/{delete.js => delete.ts} (74%) rename src/api/endpoints/posts/likes/{create.js => create.ts} (82%) rename src/api/endpoints/posts/likes/{delete.js => delete.ts} (80%) rename src/api/endpoints/posts/polls/{vote.js => vote.ts} (72%) rename src/api/endpoints/username/{available.js => available.ts} (69%) diff --git a/src/api/endpoints/posts/favorites/create.js b/src/api/endpoints/posts/favorites/create.ts similarity index 75% rename from src/api/endpoints/posts/favorites/create.js rename to src/api/endpoints/posts/favorites/create.ts index 7ee7c0d3f..5be338593 100644 --- a/src/api/endpoints/posts/favorites/create.js +++ b/src/api/endpoints/posts/favorites/create.ts @@ -3,9 +3,9 @@ /** * Module dependencies */ -import * as mongo from 'mongodb'; -import Favorite from '../../models/favorite'; -import Post from '../../models/post'; +import it from '../../../it'; +import Favorite from '../../../models/favorite'; +import Post from '../../../models/post'; /** * Favorite a post @@ -17,10 +17,8 @@ import Post from '../../models/post'; 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'); - } + const [postId, postIdErr] = it(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id param'); // Get favoritee const post = await Post.findOne({ diff --git a/src/api/endpoints/posts/favorites/delete.js b/src/api/endpoints/posts/favorites/delete.ts similarity index 74% rename from src/api/endpoints/posts/favorites/delete.js rename to src/api/endpoints/posts/favorites/delete.ts index 4b36b9bde..4dfd76158 100644 --- a/src/api/endpoints/posts/favorites/delete.js +++ b/src/api/endpoints/posts/favorites/delete.ts @@ -3,9 +3,9 @@ /** * Module dependencies */ -import * as mongo from 'mongodb'; -import Favorite from '../../models/favorite'; -import Post from '../../models/post'; +import it from '../../../it'; +import Favorite from '../../../models/favorite'; +import Post from '../../../models/post'; /** * Unfavorite a post @@ -17,10 +17,8 @@ import Post from '../../models/post'; 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'); - } + const [postId, postIdErr] = it(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id param'); // Get favoritee const post = await Post.findOne({ diff --git a/src/api/endpoints/posts/likes/create.js b/src/api/endpoints/posts/likes/create.ts similarity index 82% rename from src/api/endpoints/posts/likes/create.js rename to src/api/endpoints/posts/likes/create.ts index 3b2c778a0..0ae417e23 100644 --- a/src/api/endpoints/posts/likes/create.js +++ b/src/api/endpoints/posts/likes/create.ts @@ -3,7 +3,7 @@ /** * Module dependencies */ -import * as mongo from 'mongodb'; +import it from '../../../it'; import Like from '../../../models/like'; import Post from '../../../models/post'; import User from '../../../models/user'; @@ -19,19 +19,12 @@ import notify from '../../../common/notify'; 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'); - } + const [postId, postIdErr] = it(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id param'); // Get likee const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) + _id: postId }); if (post === null) { diff --git a/src/api/endpoints/posts/likes/delete.js b/src/api/endpoints/posts/likes/delete.ts similarity index 80% rename from src/api/endpoints/posts/likes/delete.js rename to src/api/endpoints/posts/likes/delete.ts index 1dd0f5b29..2b642c107 100644 --- a/src/api/endpoints/posts/likes/delete.js +++ b/src/api/endpoints/posts/likes/delete.ts @@ -3,7 +3,7 @@ /** * Module dependencies */ -import * as mongo from 'mongodb'; +import it from '../../../it'; import Like from '../../../models/like'; import Post from '../../../models/post'; import User from '../../../models/user'; @@ -19,19 +19,12 @@ import User from '../../../models/user'; 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'); - } + const [postId, postIdErr] = it(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id param'); // Get likee const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) + _id: postId }); if (post === null) { diff --git a/src/api/endpoints/posts/polls/vote.js b/src/api/endpoints/posts/polls/vote.ts similarity index 72% rename from src/api/endpoints/posts/polls/vote.js rename to src/api/endpoints/posts/polls/vote.ts index 9f9a5171a..d0caf7da9 100644 --- a/src/api/endpoints/posts/polls/vote.js +++ b/src/api/endpoints/posts/polls/vote.ts @@ -3,7 +3,7 @@ /** * Module dependencies */ -import * as mongo from 'mongodb'; +import it from '../../../it'; import Vote from '../../../models/poll-vote'; import Post from '../../../models/post'; import notify from '../../../common/notify'; @@ -18,19 +18,12 @@ import notify from '../../../common/notify'; 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'); - } - - // Validate id - if (!mongo.ObjectID.isValid(postId)) { - return rej('incorrect post_id'); - } + const [postId, postIdErr] = it(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id param'); // Get votee const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) + _id: postId }); if (post === null) { @@ -42,15 +35,12 @@ module.exports = (params, user) => } // 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'); - } + const [choice, choiceError] = + it(params.choice).expect.string() + .required() + .validate(c => post.poll.choices.some(x => x.id == c)) + .qed(); + if (choiceError) return rej('invalid choice param'); // if already voted const exist = await Vote.findOne({ @@ -76,8 +66,6 @@ module.exports = (params, user) => 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 diff --git a/src/api/endpoints/username/available.js b/src/api/endpoints/username/available.ts similarity index 69% rename from src/api/endpoints/username/available.js rename to src/api/endpoints/username/available.ts index 8f4d8cf28..9a85644b6 100644 --- a/src/api/endpoints/username/available.js +++ b/src/api/endpoints/username/available.ts @@ -3,6 +3,7 @@ /** * Module dependencies */ +import it from '../../it'; import User from '../../models/user'; import { validateUsername } from '../../models/user'; @@ -16,15 +17,8 @@ module.exports = async (params) => new Promise(async (res, rej) => { // Get 'username' parameter - const username = params.username; - if (username == null || username == '') { - return rej('username-is-required'); - } - - // Validate username - if (!validateUsername(username)) { - return rej('invalid-username'); - } + const [username, usernameError] = it(params.username).expect.string().required().trim().validate(validateUsername).qed(); + if (usernameError) return rej('invalid username param'); // Get exist const exist = await User