From 2bec2b4be5fa5935a110888cbee4695a83f6e730 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Fri, 26 Nov 2021 21:28:20 -0700 Subject: [PATCH] Add functions to get latest game --- database/scores/games.js | 13 ++++++++++++- public/scripts/data.js | 8 ++++++++ routes/data.js | 6 +++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/database/scores/games.js b/database/scores/games.js index 8792b2e..e01352c 100644 --- a/database/scores/games.js +++ b/database/scores/games.js @@ -108,6 +108,16 @@ async function getFromID(gameID) { return new Game(row[0], row[3].toISOString().slice(0,10), row[4], row[5], row[6], row[7], row[1], row[2], row[8]); } +async function getLatest(userID = undefined) { + if(userID) { + const games = await retrieveByUser(userID); + return games[0]; + } else { + const games = await retrieve(); + return games[0]; + } +} + @@ -117,4 +127,5 @@ exports.remove = remove; exports.retrieve = retrieve; exports.retrieveByUser = retrieveByUser; exports.edit = edit; -exports.getFromID = getFromID; \ No newline at end of file +exports.getFromID = getFromID; +exports.getLatest = getLatest; \ No newline at end of file diff --git a/public/scripts/data.js b/public/scripts/data.js index e8c5898..28ec018 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -78,6 +78,14 @@ export async function getGame(gameID) { return game; } +export async function getLatestGame(userID = undefined) { + let URL = `/data/game?`; + if(userID) URL += `user=${userID}`; + const response = await fetch(URL); + const game = await response.json(); + return game; +} + export async function getAccounts() { const response = await fetch(`/data/accounts`); const accounts = await response.json(); diff --git a/routes/data.js b/routes/data.js index dfe6b8d..ae4fac3 100644 --- a/routes/data.js +++ b/routes/data.js @@ -122,7 +122,11 @@ router.get('/games', async function(req, res, next) { router.get('/game', async function(req, res, next) { try { const gameID = req.query.game; - const data = await games.getFromID(gameID); + const userID = req.query.user; + + let data; + if(gameID) data = await games.getFromID(gameID); + else data = await games.getLatest(userID); res.json(data); } catch(err) { console.error("ERROR: " + err.message);