Merge remote-tracking branch 'origin/master' into login

This commit is contained in:
2020-04-25 02:41:15 +02:00
3 changed files with 70 additions and 20 deletions

View File

@@ -36,6 +36,17 @@ const saltRounds = 13;
var serverStartupComplete = false;
function twoDigits(d) {
if (0 <= d && d < 10) return "0" + d.toString();
if (-10 < d && d < 0) return "-0" + (-1 * d).toString();
return d.toString();
}
Date.prototype.toMysqlFormat = function () {
return this.getFullYear() + "-" + twoDigits(1 + this.getMonth()) + "-" + twoDigits(this.getDate()) + " " + twoDigits(this.getHours()) + ":" + twoDigits(this.getMinutes()) + ":" + twoDigits(this.getSeconds());
};
async function loadData() {
var rL = await fetch("https://www.simcompanies.com/api/v3/en/encyclopedia/resources/");
resourceList = await rL.json();
@@ -141,20 +152,20 @@ app.get("/API/testlogin", function (req, res) {
});
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 (Number.isInteger(kind)) {
//Mock Data:
if (kind === -1) return res.send(mockDataDay);
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}"`
if (kind >= 1 && kind <= 113) {
var dayend = new Date().toMysqlFormat();
var daybegin = new Date();
daybegin.setDate(daybegin.getDate() - 1);
daybegin = daybegin.toISOString().slice(0, 19).replace('T', ' ');
const querystring = `SELECT time, price, quality FROM marketv2 WHERE kind = ${kind} AND time > "${daybegin}" AND time < "${dayend}" ORDER BY time, quality`;
connection.query(querystring, function (error, results, fields) {
if (error) {
throw error;
res.status(status.INTERNAL_SERVER_ERROR).send("database connection failed");
}
res.send(results);
});