0
0
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 = [];

View File

@ -1,3 +1,8 @@
/**
* it
*
*/
import * as mongo from 'mongodb';
import hasDuplicates from '../common/has-duplicates';
@ -11,6 +16,8 @@ interface Factory {
required: () => Factory;
default: (value: any) => Factory;
validate: (validator: Validator<any>) => Factory;
}
@ -33,6 +40,16 @@ class FactoryCore implements Factory {
return this;
}
/**
*
*/
default(value: any) {
if (this.value === null) {
this.value = value;
}
return this;
}
/**
*
*/
@ -79,6 +96,13 @@ class BooleanFactory extends FactoryCore {
return super.required();
}
/**
*
*/
default(value: boolean) {
return super.default(value);
}
/**
*
*/
@ -124,6 +148,30 @@ class NumberFactory extends FactoryCore {
return this;
}
/**
*
* @param value
*/
min(value: number) {
if (this.error || this.value === null) return this;
if (this.value < value) {
this.error = new Error('invalid-range');
}
return this;
}
/**
*
* @param value
*/
max(value: number) {
if (this.error || this.value === null) return this;
if (this.value > value) {
this.error = new Error('invalid-range');
}
return this;
}
/**
* undefined  null
*/
@ -131,6 +179,13 @@ class NumberFactory extends FactoryCore {
return super.required();
}
/**
*
*/
default(value: number) {
return super.default(value);
}
/**
*
*/
@ -189,6 +244,13 @@ class StringFactory extends FactoryCore {
return super.required();
}
/**
*
*/
default(value: string) {
return super.default(value);
}
/**
*
*/
@ -252,6 +314,13 @@ class ArrayFactory extends FactoryCore {
return super.required();
}
/**
*
*/
default(value: any[]) {
return super.default(value);
}
/**
*
*/
@ -291,6 +360,13 @@ class IdFactory extends FactoryCore {
return super.required();
}
/**
*
*/
default(value: mongo.ObjectID) {
return super.default(value);
}
/**
*
*/
@ -330,6 +406,13 @@ class ObjectFactory extends FactoryCore {
return super.required();
}
/**
*
*/
default(value: any) {
return super.default(value);
}
/**
*
*/