diff --git a/src/index.ts b/src/index.ts index d01d1aa..5817599 100644 --- a/src/index.ts +++ b/src/index.ts @@ -110,14 +110,15 @@ if (process.env.NODE_ENV !== 'production') { // Require routes -app.use('/', require('./routes/root')); -app.use('/', require('./routes/sample')); +app.use('/', require('./routes/help')); app.use('/', require('./routes/material')); app.use('/', require('./routes/measurement')); -app.use('/', require('./routes/template')); app.use('/', require('./routes/model')); +app.use('/', require('./routes/prediction')); +app.use('/', require('./routes/root')); +app.use('/', require('./routes/sample')); +app.use('/', require('./routes/template')); app.use('/', require('./routes/user')); -app.use('/', require('./routes/help')); // Static files app.use('/static', express.static('static')); @@ -140,4 +141,4 @@ const server = app.listen(port, () => { console.info(process.env.NODE_ENV === 'test' ? '' : `Listening on http://localhost:${port}`); }); -module.exports = server; \ No newline at end of file +module.exports = server; diff --git a/src/models/prediction.ts b/src/models/prediction.ts new file mode 100644 index 0000000..dfd2b07 --- /dev/null +++ b/src/models/prediction.ts @@ -0,0 +1,12 @@ +import mongoose from 'mongoose'; + + +const PredictionSchema = new mongoose.Schema({ + name: { + type: String, + unique: true + }, + value: Number +}); + +export default mongoose.model>('prediction', PredictionSchema); diff --git a/src/models/user.ts b/src/models/user.ts index a70791b..51ad234 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -1,6 +1,7 @@ import mongoose from 'mongoose'; import db from '../db'; import ModelModel from './model'; +import PredictionModel from './prediction'; const UserSchema = new mongoose.Schema({ name: {type: String, index: {unique: true}}, @@ -11,6 +12,7 @@ const UserSchema = new mongoose.Schema({ location: String, devices: [String], models: [{type: mongoose.Schema.Types.ObjectId, ref: ModelModel}], + predictions: [{type: mongoose.Schema.Types.ObjectId, ref: PredictionModel}], status: String }); diff --git a/src/routes/prediction.spec.ts b/src/routes/prediction.spec.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/routes/prediction.ts b/src/routes/prediction.ts new file mode 100644 index 0000000..9fc2bf6 --- /dev/null +++ b/src/routes/prediction.ts @@ -0,0 +1,27 @@ +import express from 'express'; +import mongoose from 'mongoose'; + +import db from '../db'; +import PredictionModel from '../models/prediction'; + +const router = express.Router(); + + +router.post('/prediction/new', (req, res, next) => { + console.log("New Prediction"); + console.log(JSON.stringify(req.body)); + new PredictionModel(req.body).save((err, data) => { + if(err) return next(err); + db.log(req, 'predictions', {_id: data._id}, data.toObject()); + }); +}); + +router.post('/prediction/compare', async (req, res, next) => { + const valA = await PredictionModel.findOne({ name: req.body.nameA }); + const valB = await PredictionModel.findOne({ name: req.body.nameB }); + if(!valA || !valB) next(new Error("invalid prediction name")); + res.json(valB.value - valA.value); +}); + + +module.exports = router;