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 express = require('express');
const fs = require('fs');
function createRouter(db) { function createRouter(db) {
const router = express.Router(); const router = express.Router();
...@@ -30,16 +31,16 @@ function createRouter(db) { ...@@ -30,16 +31,16 @@ function createRouter(db) {
(error, results) => { (error, results) => {
if (error) { if (error) {
console.log(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 { } else {
numberResults = results[0].Maps; numberResults = results[0].Maps;
if (numberResults === 1) next(); if (numberResults === 1) next();
else if (numberResults === 0) addMap(req, res, 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 { } 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) { ...@@ -53,10 +54,10 @@ function createRouter(db) {
(error, results) => { (error, results) => {
if (error) { if (error) {
console.log(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 { } else {
if (results.affectedRows === 1) next(); 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) { ...@@ -72,15 +73,15 @@ function createRouter(db) {
(error, results) => { (error, results) => {
if (error) { if (error) {
console.log(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 { } else {
if (results.length === 0) insertVote(req, res, next); if (results.length === 0) insertVote(req, res, next);
else if (results.length === 1) updateVote(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 { } 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) { ...@@ -96,14 +97,14 @@ function createRouter(db) {
(error, results) => { (error, results) => {
if (error) { if (error) {
console.log(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 { } else {
if (results.affectedRows === 1) next(); 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 { } 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) { ...@@ -119,14 +120,14 @@ function createRouter(db) {
(error, results) => { (error, results) => {
if (error) { if (error) {
console.log(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 { } else {
if (results.affectedRows === 1) next(); 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 { } 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) { ...@@ -141,21 +142,21 @@ function createRouter(db) {
(error, results) => { (error, results) => {
if (error) { if (error) {
console.log(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 { } else {
if (results.length === 1) { if (results.length === 1) {
let average = results[0].average; let average = results[0].average;
let votes = results[0].votes; let votes = results[0].votes;
if (!average) average = 0; if (!average) average = 0;
if (!votes) votes = 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; 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 { } 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) { ...@@ -170,7 +171,7 @@ function createRouter(db) {
(error, results) => { (error, results) => {
if (error) { if (error) {
console.log(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 { } else {
if (results.length < 2) { if (results.length < 2) {
let vote = -1; let vote = -1;
...@@ -182,24 +183,30 @@ function createRouter(db) { ...@@ -182,24 +183,30 @@ function createRouter(db) {
getMapInfo(req, res); getMapInfo(req, res);
return; 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 { } 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 { } 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 // 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('/setVote', [hasMap, hasVote, getMapInfo]);
router.post('/getMapInfo', [hasMap, getVote]); 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