2021-11-21 04:35:56 +00:00
|
|
|
const database = require('./../database');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Team {
|
|
|
|
constructor(id, name) {
|
|
|
|
this.id = id;
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-11-21 05:01:55 +00:00
|
|
|
async function add(name, sportID) {
|
2021-11-21 04:45:28 +00:00
|
|
|
const query = `INSERT INTO scores.teams(team_name, sport_id)
|
2021-11-21 04:35:56 +00:00
|
|
|
VALUES($1, $2)
|
|
|
|
RETURNING team_id;`;
|
|
|
|
const id = (await database.executeQuery(query, [name, sportID]))[0][0];
|
|
|
|
return new Team(id, name);
|
2021-11-21 04:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function rename(id, name) {
|
2021-11-21 04:45:28 +00:00
|
|
|
const query = `UPDATE scores.teams
|
2021-11-21 04:38:08 +00:00
|
|
|
SET team_name = $2
|
|
|
|
WHERE team_id = $1;`;
|
|
|
|
await database.executeQuery(query, [id, name]);
|
|
|
|
return new Team(id, name);
|
2021-11-21 04:39:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function remove(id) {
|
2021-11-21 04:45:28 +00:00
|
|
|
const query = `DELETE FROM scores.teams
|
2021-11-21 04:39:30 +00:00
|
|
|
WHERE team_id = $1
|
|
|
|
RETURNING team_name;`;
|
2021-11-21 04:45:28 +00:00
|
|
|
const name = (await database.executeQuery(query, [id]))[0][0];
|
2021-11-21 04:39:30 +00:00
|
|
|
return new Team(id, name);
|
2021-11-21 04:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function retrieveBySport(sportID) {
|
2021-11-21 04:45:28 +00:00
|
|
|
const query = `SELECT *
|
2021-11-21 04:43:19 +00:00
|
|
|
FROM scores.teams
|
|
|
|
WHERE sport_id = $1
|
|
|
|
ORDER BY team_name;`;
|
2021-11-22 00:08:16 +00:00
|
|
|
const table = await database.executeQuery(query, [sportID]);
|
2021-11-21 04:43:19 +00:00
|
|
|
|
|
|
|
const teamsList = [];
|
|
|
|
table.forEach((row) => {
|
|
|
|
teamsList.push(new Team(row[0], row[1]));
|
|
|
|
});
|
|
|
|
return teamsList;
|
2021-11-21 04:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-11-21 05:01:55 +00:00
|
|
|
exports.add = add;
|
2021-11-21 04:47:15 +00:00
|
|
|
exports.rename = rename;
|
|
|
|
exports.remove = remove;
|
|
|
|
exports.retrieveBySport = retrieveBySport;
|