Commit f08d3d64 authored by Matthieu - Greep's avatar Matthieu - Greep

Delete events.js, I don't need you anymore cause we have route

parent 04d5a096
Pipeline #448 passed with stage
in 50 seconds
const express = require('express');
const fs = require('fs');
function createRouter(db) {
const router = express.Router();
function isMapIdAndNameNotNull(req) {
const mapId = req.body.mapId;
const name = req.body.name;
return mapId != null && name != null;
}
function isVoteRequestValid(req) {
const userId = req.body.userId;
const vote = req.body.vote;
return userId != null && vote != null && isMapIdAndNameNotNull(req);
}
function isgetMapInfoValid(req) {
const userId = req.body.userId;
return userId != null && isMapIdAndNameNotNull(req);
}
const hasMap = function hasMap(req, res, next) {
const mapId = req.body.mapId;
if (isMapIdAndNameNotNull(req)) {
const sql = 'SELECT count(*) as Maps From Map WHERE mapId = \"'+mapId+'\";';
console.log('Map Select: ', sql);
db.query(
sql,
(error, results) => {
if (error) {
console.log(error);
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.status(500).json({status: 'There are multiple maps with the same id'});
}
});
} else {
res.status(500).json({status: 'JSON-Parameter are not valid'});
}
};
const addMap = function addMap(req, res, next) {
const mapId = req.body.mapId;
const name = req.body.name;
const sql = 'INSERT INTO Map (mapId, name) VALUES (\"'+mapId+'\", \"'+name+'\");';
console.log('Map Insert: ', sql);
db.query(
sql,
(error, results) => {
if (error) {
console.log(error);
res.status(500).json({status: 'error in addMap'});
} else {
if (results.affectedRows === 1) next();
else res.status(500).json({status: 'error in insertMap'});
}
});
};
const hasVote = function hasVote(req, res, next) {
if (isVoteRequestValid(req)) {
const userId = req.body.userId;
const mapId = req.body.mapId;
const sql = 'SELECT userId FROM Vote WHERE userId = \"'+ userId + '\" AND mapId = \"' + mapId + '\";';
console.log('Map Insert: ', sql);
db.query(
sql,
(error, results) => {
if (error) {
console.log(error);
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.status(500).json({status: 'too many votes by user'});
}
});
} else {
res.status(500).json({status: 'Vote Request is not valid'});
}
};
const insertVote = function insertVote(req, res, next) {
if (isVoteRequestValid(req)) {
const userId = req.body.userId;
const mapId = req.body.mapId;
const vote = req.body.vote;
const sql = 'INSERT INTO Vote (userId, mapId, vote) VALUES (\"'+ userId + '\", \"'+ mapId + '\", \"' + vote +'\");';
console.log('Insert Vote: ', sql);
db.query(
sql,
(error, results) => {
if (error) {
console.log(error);
res.status(500).json({status: 'error in insertVote'});
} else {
if (results.affectedRows === 1) next();
else res.status(500).json({status: 'error in insertVote'});
}
});
} else {
res.status(500).json({status: 'Vote Request is not valid'});
}
};
const updateVote = function updateVote(req, res, next) {
if (isVoteRequestValid(req)) {
const userId = req.body.userId;
const mapId = req.body.mapId;
const vote = req.body.vote;
const sql = 'UPDATE Vote SET userId = \"'+ userId + '\", mapId = \"'+ mapId + '\", vote = \"' + vote +'\" WHERE userID = \"'+ userId +'\" AND mapId = \"' + mapId + '\";';
console.log('Update Vote: ', sql);
db.query(
sql,
(error, results) => {
if (error) {
console.log(error);
res.status(500).json({status: 'error in updateVote'});
} else {
if (results.affectedRows === 1) next();
else res.status(500).json({status: 'error in updateVote'});
}
});
} else {
res.status(500).json({status: 'Vote Request is not valid'});
}
};
const getMapInfo = function getMapInfo(req, res) {
if (isVoteRequestValid(req)) {
const mapId = req.body.mapId;
const vote = req.body.vote;
const sql = 'SELECT count(userId) as votes, round(avg(vote)) as average FROM Vote WHERE mapId LIKE \"'+ mapId + '\";';
console.log('getMapInfo: ', sql);
db.query(
sql,
(error, results) => {
if (error) {
console.log(error);
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.json({votes: votes, average: average, vote: vote});
return;
}
res.status(500).json({status: 'unexpected result in getMapInfo'});
}
});
} else {
res.status(500).json({status: 'Vote Request is not valid'});
}
};
const getVote = function getVote(req, res) {
if (isgetMapInfoValid(req)) {
const mapId = req.body.mapId;
const userId = req.body.userId;
const sql = 'SELECT vote FROM Vote WHERE mapId LIKE \"'+ mapId + '\" AND userId LIKE \"' + userId + '\";';
console.log('Update Vote: ', sql);
db.query(
sql,
(error, results) => {
if (error) {
console.log(error);
res.status(500).json({status: 'error in getVote'});
} else {
if (results.length < 2) {
let vote = -1;
if (results.length === 1) {
vote = results[0].vote;
}
if (!isNaN(vote)) {
req.body.vote = vote;
getMapInfo(req, res);
return;
}
res.status(500).json({status: 'unexpected result in getVote'});
} else {
res.status(500).json({status: 'too many results in getVote'});
}
}
});
} else {
res.status(500).json({status: 'Vote Request is not 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.post('/setVote', [hasMap, hasVote, getMapInfo]);
router.post('/getMapInfo', [hasMap, getVote]);
return router;
}
module.exports = createRouter;
...@@ -5,7 +5,6 @@ const express = require('express'), ...@@ -5,7 +5,6 @@ const express = require('express'),
logger = require('morgan'), logger = require('morgan'),
cookieParser = require('cookie-parser'), cookieParser = require('cookie-parser'),
mysql = require('mysql'), mysql = require('mysql'),
//events = require('./events'),
path = require('path'), path = require('path'),
fs = require('fs'); fs = require('fs');
......
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