This commit is contained in:
syuilo
2017-03-03 02:42:17 +09:00
parent c94de44f27
commit d5e5419dfc
2 changed files with 90 additions and 20 deletions

View File

@@ -3,7 +3,7 @@
/**
* Module dependencies
*/
import validate from '../../validator';
import it from '../../it';
import Post from '../../models/post';
import serialize from '../../serializers/post';
@@ -18,37 +18,24 @@ module.exports = (params, user) =>
new Promise(async (res, rej) =>
{
// Get 'post_id' parameter
const [postId, postIdErr] = validate(params.post_id, 'id', true);
const [postId, postIdErr] = it(params.post_id, 'id', true);
if (postIdErr) return rej('invalid post_id');
// Get 'limit' parameter
let [limit, limitErr] = validate(params.limit, 'number');
const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
if (limitErr) return rej('invalid limit');
if (limit !== null) {
// From 1 to 100
if (!(1 <= limit && limit <= 100)) {
return rej('invalid limit range');
}
} else {
limit = 10;
}
// Get 'offset' parameter
let offset = params.offset;
if (offset !== undefined && offset !== null) {
offset = parseInt(offset, 10);
} else {
offset = 0;
}
const [offset, offsetErr] = it(params.limit).expect.number().min(0).default(0).qed();
if (offsetErr) return rej('invalid offset');
// Lookup post
const post = await Post.findOne({
_id: new mongo.ObjectID(postId)
_id: postId
});
if (post === null) {
return rej('post not found', 'POST_NOT_FOUND');
return rej('post not found');
}
const context = [];