Add proper date handling functionality to "add" function of "games.js"

main
sudoer777 2021-11-20 23:01:13 -07:00
parent 9ca8641f20
commit 42ccda2aba
1 changed files with 27 additions and 1 deletions

View File

@ -21,7 +21,14 @@ async function add(divisionID, seasonID, date, team1ID, team2ID, team1Score, tea
const query = `INSERT INTO scores.games(division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score) const query = `INSERT INTO scores.games(division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score)
VALUES($1, $2, $3, $4, $5, $6, $7) VALUES($1, $2, $3, $4, $5, $6, $7)
RETURNING game_id;`; RETURNING game_id;`;
const id = (await database.executeQuery(query, [divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score]))[0][0];
const day = ("0" + date.getDate()).slice(-2);
const month = ("0" + (date.getMonth() + 1)).slice(-2);
const year = date.getFullYear();
const dateISO = year + '-' + month + '-' + day;
const id = (await database.executeQuery(query, [divisionID, seasonID, dateISO, team1ID, team2ID, team1Score, team2Score]))[0][0];
return new Game(id, date, team1ID, team2ID, team1Score, team2Score); return new Game(id, date, team1ID, team2ID, team1Score, team2Score);
} }
@ -31,4 +38,23 @@ async function remove(id) {
RETURNING * ;`; RETURNING * ;`;
const row = (await database.executeQuery(query, [id]))[0]; const row = (await database.executeQuery(query, [id]))[0];
return new Game(id, row[3], row[4], row[5], row[6], row[7]); return new Game(id, row[3], row[4], row[5], row[6], row[7]);
}
async function retrieveByTeamDivisionAndSeason(teamID, divisionID, seasonID) {
const query = `SELECT *
FROM scores.games
WHERE (team1_id = $1 OR team2_id = $1) AND division_id = $2 AND season_id = $3
ORDER BY game_date DESC;`;
const table = (await database.executeQuery(query, [teamID,divisionID,seasonID]));
const gamesList = [];
table.forEach((row) => {
opponentIsTeam2 = teamID != row[5];
opponentID = opponentIsTeam2 ? row[5] : row[4];
teamScore = opponentIsTeam2 ? row[6] : row[7];
opponentScore = opponentIsTeam2 ? row[7] : row[6];
gamesList.push(new Game(row[0], row[3], teamID, opponentID, teamScore, opponentScore));
});
return gamesList;
} }