Commit 1cab9dfa authored by Matthieu - Greep's avatar Matthieu - Greep

Moving Events to multiple files (still in WIP)

parent fb2145d5
Pipeline #446 canceled with stage
in 1 minute and 4 seconds
const express = require('express');
const fs = require('fs');
function createRouter(db) {
const router = express.Router();
......@@ -30,16 +31,16 @@ function createRouter(db) {
(error, results) => {
if (error) {
console.log(error);
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error in hasMap'});
res.status(500).json({status: 'error in hasMap'});
} else {
numberResults = results[0].Maps;
if (numberResults === 1) next();
else if (numberResults === 0) addMap(req, res, next);
else res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'There are multiple maps with the same id'});
else res.status(500).json({status: 'There are multiple maps with the same id'});
}
});
} else {
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'JSON-Parameter are not valid'});
res.status(500).json({status: 'JSON-Parameter are not valid'});
}
};
......@@ -53,10 +54,10 @@ function createRouter(db) {
(error, results) => {
if (error) {
console.log(error);
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error in addMap'});
res.status(500).json({status: 'error in addMap'});
} else {
if (results.affectedRows === 1) next();
else res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error in insertMap'});
else res.status(500).json({status: 'error in insertMap'});
}
});
};
......@@ -72,15 +73,15 @@ function createRouter(db) {
(error, results) => {
if (error) {
console.log(error);
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error in hasVote'});
res.status(500).json({status: 'error in hasVote'});
} else {
if (results.length === 0) insertVote(req, res, next);
else if (results.length === 1) updateVote(req, res, next);
else res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'too many votes by user'});
else res.status(500).json({status: 'too many votes by user'});
}
});
} else {
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'Vote Request is not valid'});
res.status(500).json({status: 'Vote Request is not valid'});
}
};
......@@ -96,14 +97,14 @@ function createRouter(db) {
(error, results) => {
if (error) {
console.log(error);
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error in insertVote'});
res.status(500).json({status: 'error in insertVote'});
} else {
if (results.affectedRows === 1) next();
else res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error in insertVote'});
else res.status(500).json({status: 'error in insertVote'});
}
});
} else {
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'Vote Request is not valid'});
res.status(500).json({status: 'Vote Request is not valid'});
}
};
......@@ -119,14 +120,14 @@ function createRouter(db) {
(error, results) => {
if (error) {
console.log(error);
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error in updateVote'});
res.status(500).json({status: 'error in updateVote'});
} else {
if (results.affectedRows === 1) next();
else res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error in updateVote'});
else res.status(500).json({status: 'error in updateVote'});
}
});
} else {
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'Vote Request is not valid'});
res.status(500).json({status: 'Vote Request is not valid'});
}
};
......@@ -141,21 +142,21 @@ function createRouter(db) {
(error, results) => {
if (error) {
console.log(error);
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error in getMapInfo'});
res.status(500).json({status: 'error in getMapInfo'});
} else {
if (results.length === 1) {
let average = results[0].average;
let votes = results[0].votes;
if (!average) average = 0;
if (!votes) votes = 0;
res.header('Access-Control-Allow-Origin', "*").status(200).json({votes: votes, average: average, vote: vote});
res.json({votes: votes, average: average, vote: vote});
return;
}
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'unexpected result in getMapInfo'});
res.status(500).json({status: 'unexpected result in getMapInfo'});
}
});
} else {
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'Vote Request is not valid'});
res.status(500).json({status: 'Vote Request is not valid'});
}
};
......@@ -170,7 +171,7 @@ function createRouter(db) {
(error, results) => {
if (error) {
console.log(error);
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error in getVote'});
res.status(500).json({status: 'error in getVote'});
} else {
if (results.length < 2) {
let vote = -1;
......@@ -182,24 +183,30 @@ function createRouter(db) {
getMapInfo(req, res);
return;
}
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'unexpected result in getVote'});
res.status(500).json({status: 'unexpected result in getVote'});
} else {
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'too many results in getVote'});
res.status(500).json({status: 'too many results in getVote'});
}
}
});
} else {
res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'Vote Request is not valid'});
res.status(500).json({status: 'Vote Request is not valid'});
}
};
const test = function test(req, res) {
res.header('Access-Control-Allow-Origin', "*").status(200).json({status: "Test valid!"});
};
// the routes are defined here
fs.readdir('./route', (err, files) => {
if (err) {
console.error(err);
} else {
files.forEach(file => {
if (file.endsWith('.js')) {
require('./route/' + file)(router);
}
});
}
});
router.get('/test', [test]);
router.post('/setVote', [hasMap, hasVote, getMapInfo]);
router.post('/getMapInfo', [hasMap, getVote]);
......
const path = require('path'),
scriptName = path.basename(__filename).replace(/\.js$/i,'');
/**
* @param {import('express').Express} app
* @param {import('mysql').Connection} sql
*/
module.exports = function(app, sql, errorHandler) {
app.get('/'+scriptName, function(req, res) {
const mapId = req.query.map;
if (!mapId) {
return errorHandler(res, 400, 'Missing map id');
}
sql.query('SELECT map, date, COUNT(*) AS count, round(avg(vote)) as average FROM votes WHERE map = ?', mapId, (err, sqlRes)=>{
if (err) {
console.log(err);
errorHandler(res, 500, 'Internal server error');
} else {
if (sqlRes[0].map == null) {
return errorHandler(res, 404, 'Map not found');
}
// Get the latest date from an array
const latestDate = new Date(Math.max.apply(null, sqlRes.map(function(e) {
return new Date(e.date);
})));
res.json({
mapUid: sqlRes[0].map,
votes: sqlRes[0].count,
average: sqlRes[0].average,
lastVoteDate: latestDate
});
}
});
});
};
\ No newline at end of file
const path = require('path'),
scriptName = path.basename(__filename).replace(/\.js$/i,'');
module.exports = function(router) {
router.get('/'+scriptName, function(req, res) {
res.json({status: "Test valid!"});
});
};
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment