This repository has been archived on 2024-04-05. You can view files and clone it, but cannot push or open issues/pull-requests.
score-tracker/database/scores/games.js

119 lines
4.2 KiB
JavaScript
Raw Normal View History

2021-11-21 05:14:12 +00:00
const database = require('./../database');
class Game {
constructor(id, date, team1ID, team2ID, team1Score, team2Score, divisionID, seasonID) {
2021-11-21 05:14:12 +00:00
this.id = id;
this.date = date;
this.team1ID = team1ID;
this.team2ID = team2ID;
this.team1Score = team1Score;
this.team2Score = team2Score;
this.divisionID = divisionID;
this.seasonID = seasonID;
2021-11-21 05:14:12 +00:00
}
}
async function add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score, userID) {
const query = `INSERT INTO scores.games(division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score, submitter_id)
VALUES($1, $2, $3, $4, $5, $6, $7, $8)
2021-11-21 05:14:12 +00:00
RETURNING game_id;`;
const id = (await database.executeQuery(query, [divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score, userID]))[0][0];
2021-11-21 05:14:12 +00:00
return new Game(id, date, team1ID, team2ID, team1Score, team2Score);
}
async function remove(id) {
const query = `DELETE FROM scores.games
WHERE game_id = $1
RETURNING * ;`;
const row = (await database.executeQuery(query, [id]))[0];
return new Game(id, row[3], row[4], row[5], row[6], row[7]);
}
async function retrieve(teamID, divisionID, seasonID) {
let table;
if(teamID && divisionID && seasonID) {
const query = `SELECT game_id, division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score
FROM scores.games
WHERE (team1_id = $1 OR team2_id = $1) AND division_id = $2 AND season_id = $3
ORDER BY game_date DESC;`;
table = await database.executeQuery(query, [teamID,divisionID,seasonID]);
}
else {
const query = `SELECT game_id, division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score
FROM scores.games
ORDER BY game_date DESC;`;
table = await database.executeQuery(query);
}
const gamesList = [];
table.forEach((row) => {
2021-11-22 23:13:21 +00:00
if(teamID) {
const opponentIsTeam2 = teamID != row[5];
const opponentID = opponentIsTeam2 ? row[5] : row[4];
const teamScore = opponentIsTeam2 ? row[6] : row[7];
const opponentScore = opponentIsTeam2 ? row[7] : row[6];
gamesList.push(new Game(row[0], row[3].toISOString().slice(0,10), teamID, opponentID, teamScore, opponentScore, row[1], row[2]));
}
else {
gamesList.push(new Game(row[0], row[3].toISOString().slice(0,10), row[4], row[5], row[6], row[7], row[1], row[2]));
}
});
return gamesList;
2021-11-21 06:02:22 +00:00
}
async function retrieveByUser(userID) {
const query = `SELECT game_id, division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score
FROM scores.games
WHERE submitter_id = $1
ORDER BY game_date DESC;`;
const table = await database.executeQuery(query, [userID]);
const gamesList = [];
table.forEach((row) => {
gamesList.push(new Game(row[0], row[3].toISOString().slice(0,10), row[4], row[5], row[6], row[7], row[1], row[2]));
});
return gamesList;
}
2021-11-23 07:49:11 +00:00
async function edit(gameID, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score) {
const query = `UPDATE scores.games
SET division_id = $2,
season_id = $3,
game_date = $4,
team1_id = $5,
team2_id = $6,
team1_score = $7,
team2_score = $8
WHERE game_id = $1;`;
await database.executeQuery(query, [gameID, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score]);
return new Game(gameID, date, team1ID, team2ID, team1Score, team2Score, divisionID, seasonID);
}
async function getFromID(gameID) {
const query = `SELECT game_id, division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score
FROM scores.games
WHERE game_id = $1;`;
const row = (await database.executeQuery(query, [gameID]))[0];
return new Game(row[0], row[3].toISOString().slice(0,10), row[4], row[5], row[6], row[7], row[1], row[2]);
}
2021-11-21 06:02:22 +00:00
exports.add = add;
exports.remove = remove;
2021-11-23 07:49:11 +00:00
exports.retrieve = retrieve;
exports.retrieveByUser = retrieveByUser;
2021-11-23 07:49:11 +00:00
exports.edit = edit;
exports.getFromID = getFromID;