-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(toolsContexts): link tool and context
- Loading branch information
Showing
8 changed files
with
458 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
} | ||
|
||
} |
Oops, something went wrong.