Add functionality to get all games from database
This commit is contained in:
		
							parent
							
								
									6fdbaedc79
								
							
						
					
					
						commit
						17f14573e9
					
				
					 3 changed files with 28 additions and 9 deletions
				
			
		| 
						 | 
					@ -5,13 +5,15 @@ const database = require('./../database');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Game {
 | 
					class Game {
 | 
				
			||||||
    constructor(id, date, team1ID, team2ID, team1Score, team2Score) {
 | 
					    constructor(id, date, team1ID, team2ID, team1Score, team2Score, divisionID, seasonID) {
 | 
				
			||||||
        this.id = id;
 | 
					        this.id = id;
 | 
				
			||||||
        this.date = date;
 | 
					        this.date = date;
 | 
				
			||||||
        this.team1ID = team1ID;
 | 
					        this.team1ID = team1ID;
 | 
				
			||||||
        this.team2ID = team2ID;
 | 
					        this.team2ID = team2ID;
 | 
				
			||||||
        this.team1Score = team1Score;
 | 
					        this.team1Score = team1Score;
 | 
				
			||||||
        this.team2Score = team2Score;
 | 
					        this.team2Score = team2Score;
 | 
				
			||||||
 | 
					        this.divisionID = divisionID;
 | 
				
			||||||
 | 
					        this.seasonID = seasonID;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -36,12 +38,23 @@ async function remove(id) {
 | 
				
			||||||
    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) {
 | 
					async function retrieve(teamID, divisionID, seasonID) {
 | 
				
			||||||
    const query = `SELECT *
 | 
					    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
 | 
					            FROM scores.games
 | 
				
			||||||
            WHERE (team1_id = $1 OR team2_id = $1) AND division_id = $2 AND season_id = $3
 | 
					            WHERE (team1_id = $1 OR team2_id = $1) AND division_id = $2 AND season_id = $3
 | 
				
			||||||
            ORDER BY game_date DESC;`;
 | 
					            ORDER BY game_date DESC;`;
 | 
				
			||||||
    const table = (await database.executeQuery(query, [teamID,divisionID,seasonID]));
 | 
					        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 = [];
 | 
					    const gamesList = [];
 | 
				
			||||||
    table.forEach((row) => {
 | 
					    table.forEach((row) => {
 | 
				
			||||||
| 
						 | 
					@ -50,7 +63,7 @@ async function retrieveByTeamDivisionAndSeason(teamID, divisionID, seasonID) {
 | 
				
			||||||
        teamScore = opponentIsTeam2 ? row[6] : row[7];
 | 
					        teamScore = opponentIsTeam2 ? row[6] : row[7];
 | 
				
			||||||
        opponentScore = opponentIsTeam2 ? row[7] : row[6];
 | 
					        opponentScore = opponentIsTeam2 ? row[7] : row[6];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        gamesList.push(new Game(row[0], row[3].toISOString().slice(0,10), teamID, opponentID, teamScore, opponentScore));
 | 
					        gamesList.push(new Game(row[0], row[3].toISOString().slice(0,10), teamID, opponentID, teamScore, opponentScore, row[1], row[2]));
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    return gamesList;
 | 
					    return gamesList;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -61,4 +74,4 @@ async function retrieveByTeamDivisionAndSeason(teamID, divisionID, seasonID) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
exports.add = add;
 | 
					exports.add = add;
 | 
				
			||||||
exports.remove = remove;
 | 
					exports.remove = remove;
 | 
				
			||||||
exports.retrieveByTeamDivisionAndSeason = retrieveByTeamDivisionAndSeason;
 | 
					exports.retrieve = retrieve;
 | 
				
			||||||
| 
						 | 
					@ -47,8 +47,14 @@ export async function getTeamName(teamID) {
 | 
				
			||||||
    return team.name;
 | 
					    return team.name;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export async function getGames(teamID, divisionID, seasonID) {
 | 
					export async function getGames(teamID = undefined, divisionID = undefined, seasonID = undefined) {
 | 
				
			||||||
    const response = await fetch(`/data/games?team=${+teamID}&division=${+divisionID}&season=${+seasonID}`);
 | 
					    let URL = '/data/games?';
 | 
				
			||||||
 | 
					    if(teamID) URL += `team=${+teamID}&`;
 | 
				
			||||||
 | 
					    if(divisionID) URL += `division=${+divisionID}&`;
 | 
				
			||||||
 | 
					    if(seasonID) URL += `season=${+seasonID}`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const response = await fetch(URL);
 | 
				
			||||||
    const gamesList = await response.json();
 | 
					    const gamesList = await response.json();
 | 
				
			||||||
    return gamesList;
 | 
					    return gamesList;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -47,7 +47,7 @@ router.get('/team', function(req, res, next) {
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
router.get('/games', function(req, res, next) {
 | 
					router.get('/games', function(req, res, next) {
 | 
				
			||||||
    games.retrieveByTeamDivisionAndSeason(req.query.team, req.query.division, req.query.season)
 | 
					    games.retrieve(req.query.team, req.query.division, req.query.season)
 | 
				
			||||||
        .then(data => res.json(data));
 | 
					        .then(data => res.json(data));
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Reference in a new issue