This commit is contained in:
syuilo 2017-03-03 05:52:12 +09:00
parent 8985d55b1b
commit d6af0bb78b
2 changed files with 32 additions and 22 deletions

View File

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

View File

@ -266,6 +266,30 @@ class StringQuery extends QueryCore {
validate(validator: Validator<string>) { validate(validator: Validator<string>) {
return super.validate(validator); 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 { class ArrayQuery extends QueryCore {