Added important admin frontend functions with error handling, the corresponding api endpoints, and the timer logic

This commit is contained in:
2024-05-06 22:19:20 +02:00
parent 340ab1236e
commit 536c1272d8
3 changed files with 112 additions and 31 deletions
+29 -11
View File
@@ -3,27 +3,45 @@ var router = express.Router();
let timer = require('../controllers/timer');
router.post('/timerStart', function(req, res, next) {
console.log("Timer gestartet");
timer.start()
res.render('admin');
// Express router entpoint to start the timer
router.get('/timerStart', function(req, res, next) {
if(timer.start()) { // If successfully started the timer
res.status(200); // Set http status code to 200 (success)
res.json(timer.getValues()); // Respond with all important values to update the frontend
} else { // If not successfull, for example because timer is already running or not time is left
res.status(406); // Set http status code to 406 (not acceptable)
res.send(); // Respond with empty payload
}
});
// Express router endpoint to pause the timer
router.get('/timerPause', function(req, res, next) {
if(timer.pause()) { // If successfully paused the timer
res.status(200); // Set http status code to 200 (success)
res.json(timer.getValues()); // Respond with all important values to update the frontend
} else { // If not successfull, for example if timer already is paused
res.status(406); // Set http status code to 406 (not acceptable)
res.send(); // Respond with empty payload
}
});
// Express router endpoint to reset the timer
router.post('/timerReset', function(req, res, next) {
console.log("Timer Zurückgesetzt");
timer.reset();
res.render('admin');
timer.reset(req.body.duration); // Reset the timer with the duration in seconds received from the request
res.json(timer.getValues()); // Respond with all important values to update the frontend
});
// Express router endpoint to switch the score after halftime
router.get('/timerGetValues', function(req, res, next) {
res.json(timer.getValues()); // Respond with important values for frontend
});
router.post('/timerPause', function(req, res, next) {
console.log("Timer pausiert");
timer.pause();
res.render('admin');
});
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('admin', {});
res.render('admin');
});