From 3bad5416a816653518f63e2a8805548076adccde Mon Sep 17 00:00:00 2001 From: "Engelbart Kai Sven (PEA4-Fe)" Date: Wed, 24 Feb 2021 13:59:25 +0100 Subject: [PATCH] Allow predictions to be saved --- src/index.ts | 11 ++++++----- src/models/prediction.ts | 3 ++- src/routes/prediction.spec.ts | 0 src/routes/prediction.ts | 20 ++++++++++++++++++++ 4 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 src/routes/prediction.spec.ts create mode 100644 src/routes/prediction.ts 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 index b379eaf..50cad46 100644 --- a/src/models/prediction.ts +++ b/src/models/prediction.ts @@ -2,7 +2,8 @@ import mongoose from 'mongoose'; const PredictionSchema = new mongoose.Schema({ - values: [{ spectrumName: String, value: Number }] + name: String, + value: Number }); export default mongoose.model>('prediction', PredictionSchema); 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..c45db7c --- /dev/null +++ b/src/routes/prediction.ts @@ -0,0 +1,20 @@ +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()); + }); +}); + + +module.exports = router;