Skip to content
Snippets Groups Projects
Commit 2bfdf1fb authored by Rudolf Copi Eckelberg's avatar Rudolf Copi Eckelberg
Browse files

First persistent simulation model

parent 4512efc1
No related branches found
No related tags found
2 merge requests!116Release v1.0.0,!25Auth
Pipeline #
const mongoose = require('mongoose') const mongoose = require('mongoose')
const libs = `${process.cwd()}/libs`; const libs = `${process.cwd()}/libs`;
const config = require(`${libs}/config`);
const log = require(`${libs}/log`)(module); const log = require(`${libs}/log`)(module);
const locations = require(`${libs}/models/locations`); const locations = require(`${libs}/models/locations`);
......
...@@ -12,6 +12,8 @@ const city = require('./city'); ...@@ -12,6 +12,8 @@ const city = require('./city');
const school = require('./school'); const school = require('./school');
const simulation = require('./simulation');
api.get('/api/v1', (req, res) => { api.get('/api/v1', (req, res) => {
res.json({ msg: 'SimCAQ API is running' }); res.json({ msg: 'SimCAQ API is running' });
}); });
...@@ -22,5 +24,6 @@ api.use('/api/v1/state', state); ...@@ -22,5 +24,6 @@ api.use('/api/v1/state', state);
api.use('/api/v1/region', region); api.use('/api/v1/region', region);
api.use('/api/v1/city', city); api.use('/api/v1/city', city);
api.use('/api/v1/school', school); api.use('/api/v1/school', school);
api.use('/api/v1/simulation', simulation);
module.exports = api; module.exports = api;
const express = require('express');
const simulationApp = express();
const libs = `${process.cwd()}/libs`;
const log = require(`${libs}/log`)(module);
const config = require(`${libs}/config`);
const squel = require('squel');
const query = require(`${libs}/middlewares/query`);
const response = require(`${libs}/middlewares/response`);
const Simulation = require(`${libs}/models/simulation`);
simulationApp.get('/', (req, res) => {
res.send({ success: true, msg: 'controller working' });
});
simulationApp.post('/', (req, res, next) => {
// This method must associate new entry with user.
/* Creates a new simulation. Requires a name. */
if (!req.body.name) {
res.send({ success: false, msg: 'Must send a name for new entry' });
} else {
next();
}
}, (req, res) => {
let newSimulation = new Simulation({
name: req.body.name,
});
newSimulation.save((err) => {
if (err) {
res.send({ success: false, msg: err });
} else {
res.send({
success: true,
msg: 'new sim created',
id: newSimulation._id,
});
}
});
});
simulationApp.get('/:id', (req, res) => {
/* Can be used to check simulation construction status */
Simulation.findById(req.params.id, (err, simulation) => {
log.debug(req.params.id);
if (err) {
res.send({ success: false, msg: err });
return;
}
if (!simulation) {
res.send({ success: false, msg: 'Entry not found' });
} else {
res.send(JSON.stringify(simulation));
}
});
});
simulationApp.post('/:id', (req, res, next) => {
if (!req.body) {
res.send({ success: false, msg: 'No field informed to update' });
} else {
next();
}
}, (req, res, next) => {
let simulation = Simulation.findById(req.params.id, (err, simulation) => {
if (err) {
res.send({ success: false, msg: err });
} else {
if (!simulation) {
res.send({ success: false, msg: 'Entry not found' });
} else {
req.simulation = simulation;
next();
}
}
});
}, (req, res) => {
for (let property in req.body) {
if(Simulation.schema.tree.hasOwnProperty(property)) {
req.simulation[property] = req.body[property];
}
}
req.simulation.save((err) => {
if (err) {
res.send({ success: false, msg: err });
} else {
res.send({
success: true,
msg: 'sim updated',
id: req.simulation._id,
});
}
});
});
module.exports = simulationApp;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment