rhamt-vscode-extension
Version:
RHAMT VSCode extension
103 lines (94 loc) • 4.07 kB
text/typescript
import { Request, Response, NextFunction } from 'express';
import { RhamtModelService } from 'raas-core';
export class ServerController {
private modelService: RhamtModelService;
constructor(modelService: RhamtModelService) {
this.modelService = modelService;
}
get(req: Request, res: Response, next: NextFunction): void {
const config = this.modelService.getConfiguration(req.params.id);
if (config) {
res.render('index', {configId: JSON.stringify(config.id)});
}
else {
console.log('unable to find configuration associated with request: ' + req.originalUrl);
}
next();
}
configuration(req: Request, res: Response, next: NextFunction): void {
const config = this.modelService.getConfiguration(req.params.id);
if (config) {
console.log('returning the option...');
res.json({config: config});
}
else {
console.log('unable to find configuration associated with request: ' + req.originalUrl);
}
next();
}
updateOption(req: Request, res: Response, next: NextFunction): void {
const config = this.modelService.getConfiguration(req.params.id);
if (config) {
const body = req.body;
console.log(body);
console.log(JSON.stringify(body));
console.log('type: ' + body.type);
console.log('name: ' + body.name);
console.log('value: ' + body.value);
if (body.type === 'boolean') {
console.log('boolean option. checking for true option');
if (body.value) {
console.log('creating option...');
const option = this.modelService.createOption(config, body.name, true);
console.log('option added: returning...');
console.log(JSON.stringify(option));
res.status(200).json(option);
console.log('sent the response back to client');
}
else {
console.log('checking for false option');
let found = false;
for (const option of config.options) {
if (body.name === option.name) {
found = true;
console.log('found option. deleting... ' + option.name);
console.log('deleting option at index: ' + config.options.indexOf(option));
config.options.splice(config.options.indexOf(option), 1);
res.status(200).json(option);
console.log('we sent out the response...');
break;
}
}
if (!found) {
res.status(500).json(`Cannot find boolean option to delete with name ${body.name}.`);
}
}
}
}
else {
console.log('unable to find configuration associated with request: ' + req.originalUrl);
res.status(500).json(`Cannot find configuration with id: ${req.params.id}`);
}
next();
}
save(req: Request, res: Response, next: NextFunction): void {
// var id = req.params.id;
// var name = req.body.name;
// var cli = req.body.cli;
// var javaHome = req.body.javaHome;
// var options = req.body.options;
// this.modelService.update(id, name, cli, javaHome, options);
// next();
}
run(req: Request, res: Response, next: NextFunction): void {
const config = this.modelService.getConfiguration(req.params.id);
if (config) {
console.log('returning the option...');
res.json({config: config});
}
else {
console.log('unable to find configuration associated with request: ' + req.originalUrl);
}
next();
}
}