This commit is contained in:
syuilo 2017-03-03 05:52:12 +09:00
parent 954789dad5
commit 3f918d6f70
2 changed files with 32 additions and 22 deletions

View file

@ -3,7 +3,7 @@
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import it from '../../it';
import Post from '../../models/post';
import Like from '../../models/like';
import serialize from '../../serializers/user';
@ -19,33 +19,19 @@ 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');
}
const [postId, postIdErr] = it(params.post_id, 'id', true);
if (postIdErr) return rej('invalid post_id');
// Get 'limit' parameter
let limit = params.limit;
if (limit !== undefined && limit !== null) {
limit = parseInt(limit, 10);
// From 1 to 100
if (!(1 <= limit && limit <= 100)) {
return rej('invalid limit range');
}
} else {
limit = 10;
}
const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
if (limitErr) return rej('invalid limit');
// Get 'offset' parameter
let offset = params.offset;
if (offset !== undefined && offset !== null) {
offset = parseInt(offset, 10);
} else {
offset = 0;
}
const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed();
if (offsetErr) return rej('invalid offset');
// Get 'sort' parameter
const [sort] = it(params.sort).expect.string().or('desc asc').default('desc').qed();
let sort = params.sort || 'desc';
// Lookup post

View file

@ -266,6 +266,30 @@ class StringQuery extends QueryCore {
validate(validator: Validator<string>) {
return super.validate(validator);
}
/**
*
*
* @param pattern
*/
or(pattern: string | string[]) {
if (this.error || this.value === null) return this;
if (typeof pattern == 'string') pattern = pattern.split(' ');
const match = pattern.some(x => x === this.value);
if (!match) this.error = new Error('not-match-pattern');
return this;
}
/**
*
*
* @param pattern
*/
match(pattern: RegExp) {
if (this.error || this.value === null) return this;
if (!pattern.test(this.value)) this.error = new Error('not-match-pattern');
return this;
}
}
class ArrayQuery extends QueryCore {