28 lines
885 B
TypeScript
28 lines
885 B
TypeScript
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', async (req, res, next) => {
|
|
console.log("New Prediction");
|
|
console.log(JSON.stringify(req.body));
|
|
await PredictionModel.findOneAndUpdate( { name: req.body.name }, { value: req.body.value }, { upsert: true, new: true }, (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;
|