Commit 368c5391 authored by Dennis Willers's avatar Dennis Willers 🏀

Implement API add and remove Points

parent 2fa78730
Pipeline #297 failed with stages
in 2 minutes and 4 seconds
...@@ -15,9 +15,7 @@ function createRouter(db) { ...@@ -15,9 +15,7 @@ function createRouter(db) {
if (error) { if (error) {
console.error(error); console.error(error);
res.status(500).json({status: 'false'}); res.status(500).json({status: 'false'});
} } else {
}
);
//Init new Gamestatus //Init new Gamestatus
var redTurn = Math.random(); var redTurn = Math.random();
redTurn = Math.round(redTurn); redTurn = Math.round(redTurn);
...@@ -30,7 +28,11 @@ function createRouter(db) { ...@@ -30,7 +28,11 @@ function createRouter(db) {
} else { } else {
res.status(200).json({status: 'true'}); res.status(200).json({status: 'true'});
} }
}); }
);
}
}
);
} else { } else {
res.status(500).json({status: 'error'}); res.status(500).json({status: 'error'});
} }
...@@ -217,7 +219,80 @@ function createRouter(db) { ...@@ -217,7 +219,80 @@ function createRouter(db) {
} }
}); });
//Add Point
router.post('/addPoint', (req, res) => {
console.log(req.body);
const spielname = req.body.spielname;
const team = req.body.team;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null && team.match(/^[0-9a-zA-Z]+$/) != null) {
if (team === 'red') {
db.query(
'UPDATE Gamestatus SET Red = 1+(SELECT Red FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")) WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")',
(error) => {
if (error) {
console.log(error);
res.status(500).json({status: 'false'});
} else {
res.status(200).json({status: 'true'});
}
}
);
} else if (team === 'blue') {
db.query(
'UPDATE Gamestatus SET Blue = 1+(SELECT Blue FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")) WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")',
(error) => {
if (error) {
console.log(error);
res.status(500).json({status: 'false'});
} else {
res.status(200).json({status: 'true'});
}
}
);
}
} else {
res.status(500).json({status: 'false'});
}
});
//Remove Point
router.post('/removePoint', (req, res) => {
console.log(req.body);
const spielname = req.body.spielname;
const team = req.body.team;
if (spielname.match(/^[0-9a-zA-Z]+$/) != null && team.match(/^[0-9a-zA-Z]+$/) != null) {
if (team === 'red') {
db.query(
'UPDATE Gamestatus SET Red = -1+(SELECT Red FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")) WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")',
(error) => {
if (error) {
console.log(error);
res.status(500).json({status: 'false'});
} else {
res.status(200).json({status: 'true'});
}
}
);
} else if (team === 'blue') {
db.query(
'UPDATE Gamestatus SET Blue = -1+(SELECT Blue FROM Gamestatus WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")) WHERE SessionID = (SELECT SessionID FROM Session WHERE SessionName = \"'+spielname+'\")',
(error) => {
if (error) {
console.log(error);
res.status(500).json({status: 'false'});
} else {
res.status(200).json({status: 'true'});
}
}
);
}
} else {
res.status(500).json({status: 'false'});
}
});
return router; return router;
} }
module.exports = createRouter; module.exports = createRouter;
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