banana
/
definma-api
Archived
2
Fork 0
This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
definma-api/src/routes/prediction.ts

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;