Add functions to get latest game

main
sudoer777 2021-11-26 21:28:20 -07:00
parent a76a3a30fb
commit 2bec2b4be5
3 changed files with 25 additions and 2 deletions

View File

@ -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];
}
}
@ -118,3 +128,4 @@ exports.retrieve = retrieve;
exports.retrieveByUser = retrieveByUser;
exports.edit = edit;
exports.getFromID = getFromID;
exports.getLatest = getLatest;

View File

@ -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();

View File

@ -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);