Commit 2529f9ec authored by 神楽坂玲奈's avatar 神楽坂玲奈

merge

parents 07f2737c b73303a1
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
/.idea/ /.idea/
*.js *.js
*.js.map *.js.map
node-debug.log
!/node_modules/@types/ !/node_modules/@types/
!/node_modules/@types/mongorito/ !/node_modules/@types/mongorito/
!systemjs.config.js !systemjs.config.js
......
import * as Mongorito from 'mongorito';
import Model = Mongorito.Model;
/** /**
* Created by weijian on 2016/12/30. * Created by weijian on 2016/12/30.
*/ */
export function field(target: Model, propertyKey: string): any { export function field(target: any, propertyKey: string): any {
return { return {
get (): any { get (): any {
return this.get(propertyKey); return this.get(propertyKey);
......
...@@ -14,5 +14,10 @@ export class NotFound extends KoaError { ...@@ -14,5 +14,10 @@ export class NotFound extends KoaError {
super(msg, 404); super(msg, 404);
} }
} }
export class BadRequest extends KoaError {
constructor(msg = STATUS_CODES[400]) {
super(msg, 400);
}
}
export const InternalError = new KoaError(STATUS_CODES[500], 500); export const InternalError = new KoaError(STATUS_CODES[500], 500);
...@@ -2,13 +2,14 @@ import 'reflect-metadata'; ...@@ -2,13 +2,14 @@ import 'reflect-metadata';
import * as Mongorito from 'mongorito'; import * as Mongorito from 'mongorito';
import Model = Mongorito.Model; import Model = Mongorito.Model;
import {field} from '../db/decorators'; import {field} from '../db/decorators';
import {ModelExistsError} from './errors';
/** /**
* Created by weijian on 2016/12/28. * Created by weijian on 2016/12/28.
*/ */
interface I18n<T> { interface I18n<T> {
[locale: string]: T; [locale: string]: T;
} }
class App extends Model { export class App extends Model {
@field @field
id: string; id: string;
@field @field
...@@ -23,6 +24,17 @@ class App extends Model { ...@@ -23,6 +24,17 @@ class App extends Model {
conference?: string; conference?: string;
@field @field
data: any; data: any;
async checkExists() {
let app = await App.findOne({id: this.id});
if (app) {
throw new ModelExistsError(this.id);
}
}
configure() {
this.before('create', this.checkExists);
}
} }
export default App;
/**
* Created by weijian on 2017/1/4.
*/
class ModelError extends Error {
constructor(msg: string) {
super(msg);
}
}
export class ModelExistsError extends Error {
constructor(id: string) {
super(`App ${id} already exists`);
}
}
...@@ -7,30 +7,151 @@ import * as mongodb from "mongodb"; ...@@ -7,30 +7,151 @@ import * as mongodb from "mongodb";
declare module Mongorito { declare module Mongorito {
function connect(url: string): Promise<mongodb.Db> function connect(url: string): Promise<mongodb.Db>
function setDriver(driver: any);
function getDriver(): any;
interface Options {
[skip: string]: string|string[];
}
class Model { class Model {
constructor(o: any, options?: Object); constructor(o: any, options?: Object);
save(options?: any): Promise<any>; get(key: string): any;
create(options?: any): Promise<any>; set(key: string, value: Object);
update(options?: any): Promise<any>; unset(key: string);
remove(options?: any): Promise<any>; toJSON();
static remove(query: Object): Promise<any>; before(action: string, method: Function);
before(action: string, method: Function[]);
before(action: string, method: string);
before(action: string, method: string[]);
after(action: string, method: Function);
after(action: string, method: Function[]);
after(action: string, method: string);
after(action: string, method: string[]);
around(action: string, method: Function);
around(action: string, method: Function[]);
around(action: string, method: string);
around(action: string, method: string[]);
/**
* Configure model (usually, set hooks here)
* Supposed to be overriden
*
* @api public
*/
configure();
/**
* Save a model
*
* @param {Object} options - options for save operation
* @api public
*/
save(options?: Options): Promise<any>;
/**
* Create a model
*
* @api private
*/
create(options?: Options): Promise<any>;
update(options?: Options): Promise<any>;
remove(options?: Options): Promise<any>;
/**
* Atomically increment a model property
*
* @param {Object} props - set of properties and values
* @param {Object} options - options for update operation
* @api public
*/
inc(props: Object, options: Options);
/**
* Find documents
*
* @param {Object} query - find conditions, same as this.where()
* @api public
*/
static find(query: Object): Promise<any[]>; static find(query: Object): Promise<any[]>;
/**
* Count documents
*
* @param {Object} query - find conditions, same as this.where()
* @api public
*/
static count(query: Object): Promise<any>; static count(query: Object): Promise<any>;
/**
* Get distinct
*
* @param {String} field for distinct
* @param {Object} query - query to filter the results
* @see http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#distinct
* @api public
*/
static distinct(filed: string, query?: Object): Promise<any>;
/**
* Aggregation query
*
* @param {String} pipeline aggregation pipeline
* @param {Object} options - Options to be passed to aggregation pipeline
* @see http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#distinct
* @api public
*/
static aggregate(pipeline: Object[]): Promise<any>;
/**
* Find all documents in a collection
*
* @api public
*/
static all(): Promise<any[]> static all(): Promise<any[]>
/**
* Find one document
*
* @param {Object} query - find conditions, same as this.where()
* @api public
*/
static findOne(query: Object): Promise<any>; static findOne(query: Object): Promise<any>;
/**
* Find a document by ID
*
* @param {ObjectID} id - document id
* @api public
*/
static findById(id: string): Promise<any>; static findById(id: string): Promise<any>;
/**
* Remove documents
*
* @param {Object} query - remove conditions, same as this.where()
* @api public
*/
static remove(query: Object): Promise<any>;
/**
* Drop collection
*
* @api public
*/
static drop(): Promise<any>; static drop(): Promise<any>;
} }
......
...@@ -2,13 +2,14 @@ ...@@ -2,13 +2,14 @@
* Created by weijian on 2016/12/28. * Created by weijian on 2016/12/28.
*/ */
import Router = require('koa-router'); import Router = require('koa-router');
import App from '../models/app'; import {NotFound, BadRequest, InternalError} from '../koa/errors';
import {NotFound} from '../koa/errors';
import {Model} from '../db/mongo'; import {Model} from '../db/mongo';
import {App} from '../models/app';
import {ModelExistsError} from '../models/errors';
const router = new Router(); const router = new Router();
router.get('/apps', async(ctx, next) => { router.get('/apps', async(ctx, next) => {
ctx.body = await App.find({}); ctx.body = await App.all();
}); });
router.get('/apps/:id', async(ctx, next) => { router.get('/apps/:id', async(ctx, next) => {
...@@ -22,7 +23,15 @@ router.get('/apps/:id', async(ctx, next) => { ...@@ -22,7 +23,15 @@ router.get('/apps/:id', async(ctx, next) => {
router.post('/apps/:id', async(ctx, next) => { router.post('/apps/:id', async(ctx, next) => {
let app = new App(ctx.request.body); let app = new App(ctx.request.body);
ctx.body = await app.save(); try {
ctx.body = await app.save();
} catch (e) {
if (e instanceof ModelExistsError) {
throw new BadRequest();
} else {
throw InternalError;
}
}
}); });
router.patch('/apps/:id', async(ctx, next) => { router.patch('/apps/:id', async(ctx, next) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment