From 03323caa69ccc1ff7c5aab4e6eaa48087d5ec6ba Mon Sep 17 00:00:00 2001 From: Oliver Boehlk Date: Sun, 23 Feb 2020 15:33:43 +0100 Subject: [PATCH] implement basic day query --- backend/index.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 backend/index.js diff --git a/backend/index.js b/backend/index.js new file mode 100644 index 0000000..e5182a6 --- /dev/null +++ b/backend/index.js @@ -0,0 +1,34 @@ +const express = require('express') +const app = express(); +const status = require('http-status'); +var mysql = require('mysql'); +var connection = mysql.createConnection({ + host: 'localhost', + user: 'simcompanies', + password: '', + database: 'simcompanies' +}); + +app.get('/API/day', function (req, res) { + var date = new Date(req.query.date); + const kind = parseInt(req.query.kind); + if (date instanceof Date && Number.isInteger(kind)) + if (!isNaN(date.getTime()) && kind >= 1 && kind <= 113) { + const daybegin = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`; + date.setDate(date.getDate() + 1); + const dayend = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`; + const querystring = `SELECT time, price FROM market WHERE kind = ${kind} AND time > "${daybegin}" AND time < "${dayend}"` + connection.query(querystring, function (error, results, fields) { + if (error) { + throw error; + } + res.send(results); + }); + } + else + res.status(status.BAD_REQUEST).send("invalid data provided"); + else + res.status(status.BAD_REQUEST).send("invalid data provided"); +}); + +app.listen(3000); \ No newline at end of file