From d1fc857c758a82651a501a79e9f3cda23303028c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?In=C3=A8s=20Yeterian?= <ines.yeterian@student-cs.fr> Date: Mon, 14 Feb 2022 16:02:11 +0100 Subject: [PATCH] try to add last toucan image route --- backend/routes/routesToucan.js | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/backend/routes/routesToucan.js b/backend/routes/routesToucan.js index a5f46c7..620b84d 100644 --- a/backend/routes/routesToucan.js +++ b/backend/routes/routesToucan.js @@ -149,7 +149,7 @@ router.get('/pdf/:year/:title', (req, res) => { }); }); -router.get('/pdf/lastToucan', (req, res) => { +router.get('/pdf-lastToucan', (req, res) => { Toucan.find() .exec(function(err, toucans){ if(err) {res.send(err);} @@ -171,10 +171,31 @@ router.get('/pdf/lastToucan', (req, res) => { } ); +}); -} - -) +router.get('/img-lastToucan', (req, res) => { + Toucan.find() + .exec(function(err, toucans){ + if(err) {res.send(err);} + else + { + var max = new Date(toucans[0].date); + var idMax = toucans[0]._id; + const nbToucans = toucans.length; + var value = toucans[0]; + for (let i = 1; i < nbToucans; i++){ + value = new Date(toucans[i].date); + if (value > max){ + max = value; + idMax = toucans[i]._id; + } + } + res.redirect('/img/'+idMax); + } + } + + ); +}); module.exports = router; \ No newline at end of file -- GitLab