Skip to content

Commit

Permalink
feat(toolsContexts): link tool and context
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed Feb 24, 2017
1 parent 7a5127a commit aa67c23
Show file tree
Hide file tree
Showing 8 changed files with 458 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as Configs from './configurations';
import { ContextModel } from './contexts/context.model';
import { LayerModel } from './layers/layer.model';
import { ToolModel } from './tools/tool.model';
import { ToolContextModel } from './toolsContexts/toolContext.model';
import { LayerContextModel } from './layersContexts/layerContext.model';

export interface IDatabase {
Expand All @@ -14,6 +15,7 @@ export interface IDatabase {
layer: LayerModel;
tool: ToolModel;
layerContext: LayerContextModel;
toolContext: ToolContextModel;
}

const dbConfigs = Configs.getDatabaseConfig();
Expand Down
2 changes: 2 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as Contexts from './contexts';
import * as Layers from './layers';
import * as Tools from './tools';
import * as LayersContexts from './layersContexts';
import * as ToolsContexts from './toolsContexts';

export function init(configs: IServerConfigurations): Promise<Hapi.Server> {
return new Promise<Hapi.Server>(resolve => {
Expand Down Expand Up @@ -43,6 +44,7 @@ export function init(configs: IServerConfigurations): Promise<Hapi.Server> {
Contexts.init(server, configs, database);
Layers.init(server, configs, database);
Tools.init(server, configs, database);
ToolsContexts.init(server, configs, database);
LayersContexts.init(server, configs, database);
console.log('Routes loaded');

Expand Down
10 changes: 10 additions & 0 deletions src/toolsContexts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as Hapi from 'hapi';
import Routes from './routes';
import { IDatabase } from '../database';
import { IServerConfigurations } from '../configurations';

export function init(server: Hapi.Server,
configs: IServerConfigurations,
database: IDatabase) {
Routes(server, configs, database);
}
180 changes: 180 additions & 0 deletions src/toolsContexts/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import * as Hapi from 'hapi';
import * as Joi from 'joi';
import ToolContextController from './toolContext.controller';
import * as ToolContextValidator from './toolContext.validator';
// import { jwtValidator } from '../users/user-validator';
import { IDatabase } from '../database';
import { IServerConfigurations } from '../configurations';

export default function (server: Hapi.Server,
configs: IServerConfigurations,
database: IDatabase) {

const toolContextController = new ToolContextController(configs, database);
server.bind(toolContextController);

server.route({
method: 'GET',
path: '/contexts/{id}/tools',
config: {
handler: toolContextController.getToolsByContextId,
// auth: 'jwt',
auth: false,
tags: ['api', 'toolsContexts', 'tools', 'contexts'],
description: 'Get tools by context id.',
validate: {
params: {
id: Joi.string().required()
}
// headers: jwtValidator
},
plugins: {
'hapi-swagger': {
responses: {
'200': {
'description': 'Tools founded.'
},
'404': {
'description': 'Context does not exists.'
}
}
}
}
}
});

server.route({
method: 'GET',
path: '/toolsContexts/{id}',
config: {
handler: toolContextController.getToolContextById,
// auth: 'jwt',
auth: false,
tags: ['api', 'toolsContexts'],
description: 'Get toolsContexts by id.',
validate: {
params: {
id: Joi.string().required()
}
// headers: jwtValidator
},
plugins: {
'hapi-swagger': {
responses: {
'200': {
'description': 'ToolContext founded.'
},
'404': {
'description': 'ToolContext does not exists.'
}
}
}
}
}
});

server.route({
method: 'GET',
path: '/toolsContexts',
config: {
handler: toolContextController.gettoolsContexts,
// auth: 'jwt',
auth: false,
tags: ['api', 'toolsContexts'],
description: 'Get all toolsContexts.',
validate: {
query: {
// top: Joi.number().default(5),
// skip: Joi.number().default(0)
}
// headers: jwtValidator
}
}
});

server.route({
method: 'DELETE',
path: '/toolsContexts/{id}',
config: {
handler: toolContextController.deleteToolContext,
// auth: 'jwt',
auth: false,
tags: ['api', 'toolsContexts'],
description: 'Delete toolContext by id.',
validate: {
params: {
id: Joi.string().required()
}
// headers: jwtValidator
},
plugins: {
'hapi-swagger': {
responses: {
'200': {
'description': 'Deleted ToolContext.',
},
'404': {
'description': 'ToolContext does not exists.'
}
}
}
}
}
});

server.route({
method: 'PUT',
path: '/toolsContexts/{id}',
config: {
handler: toolContextController.updateToolContext,
// auth: 'jwt',
auth: false,
tags: ['api', 'toolsContexts'],
description: 'Update toolContext by id.',
validate: {
params: {
id: Joi.string().required()
},
payload: ToolContextValidator.updateToolContextModel
// headers: jwtValidator
},
plugins: {
'hapi-swagger': {
responses: {
'200': {
'description': 'Deleted ToolContext.',
},
'404': {
'description': 'ToolContext does not exists.'
}
}
}
}
}
});

server.route({
method: 'POST',
path: '/toolsContexts',
config: {
handler: toolContextController.createToolContext,
// auth: 'jwt',
auth: false,
tags: ['api', 'toolsContexts'],
description: 'Create a toolContext.',
validate: {
payload: ToolContextValidator.createToolContextModel
// headers: jwtValidator
},
plugins: {
'hapi-swagger': {
responses: {
'201': {
'description': 'Created ToolContext.'
}
}
}
}
}
});
}
107 changes: 107 additions & 0 deletions src/toolsContexts/toolContext.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import * as Hapi from 'hapi';
import * as Boom from 'boom';
import { IToolContext, ToolContextInstance } from './toolContext.model';
import { IDatabase } from '../database';
import { IServerConfigurations } from '../configurations';

export default class ToolContextController {

private database: IDatabase;
private configs: IServerConfigurations;

constructor(configs: IServerConfigurations, database: IDatabase) {
this.configs = configs;
this.database = database;
}

public createToolContext(request: Hapi.Request, reply: Hapi.IReply) {
const newToolContext: IToolContext = request.payload;
this.database.toolContext.create(newToolContext)
.then((toolContext) => {
reply(toolContext).code(201);
}).catch((error) => {
reply(Boom.badImplementation(error));
});
}

public updateToolContext(request: Hapi.Request, reply: Hapi.IReply) {
const id = request.params['id'];
const toolContext: IToolContext = request.payload;

this.database.toolContext.update(toolContext, {
where: {
id: id
}
}).then((count: [number, ToolContextInstance[]]) => {
if (count[0]) {
reply({});
} else {
reply(Boom.notFound());
}
}).catch((error) => {
reply(Boom.badImplementation(error));
});
}

public deleteToolContext(request: Hapi.Request, reply: Hapi.IReply) {
const id = request.params['id'];
this.database.toolContext.destroy({
where: {
id: id
}
}).then((count: number) => {
if (count) {
reply({});
} else {
reply(Boom.notFound());
}
}).catch((error) => {
reply(Boom.badImplementation(error));
});
}

public getToolContextById(request: Hapi.Request, reply: Hapi.IReply) {
const id = request.params['id'];
this.database.toolContext.findOne({
where: {
id: id
}
}).then((toolContext: ToolContextInstance) => {
if (toolContext) {
reply(toolContext);
} else {
reply(Boom.notFound());
}
}).catch((error) => {
reply(Boom.badImplementation(error));
});
}

public gettoolsContexts(request: Hapi.Request, reply: Hapi.IReply) {
this.database.toolContext.findAll()
.then((toolsContexts: Array<ToolContextInstance>) => {
reply(toolsContexts);
}).catch((error) => {
reply(Boom.badImplementation(error));
});
}


public getToolsByContextId(request: Hapi.Request, reply: Hapi.IReply) {
const id = request.params['id'];



this.database.context.findAll({
include: [ this.database.tool ],
where: {
id: id
}
}).then((toolsContexts: Array<any>) => {
reply(toolsContexts);
}).catch((error) => {
reply(Boom.badImplementation(error));
});
}

}
Loading

0 comments on commit aa67c23

Please sign in to comment.