70 lines
1.6 KiB
JavaScript
70 lines
1.6 KiB
JavaScript
const database = require('./../database');
|
|
|
|
|
|
|
|
|
|
|
|
class Team {
|
|
constructor(id, name) {
|
|
this.id = id;
|
|
this.name = name;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async function add(name, sportID) {
|
|
const query = `INSERT INTO scores.teams(team_name, sport_id)
|
|
VALUES($1, $2)
|
|
RETURNING team_id;`;
|
|
const id = (await database.executeQuery(query, [name, sportID]))[0][0];
|
|
return new Team(id, name);
|
|
}
|
|
|
|
async function rename(id, name) {
|
|
const query = `UPDATE scores.teams
|
|
SET team_name = $2
|
|
WHERE team_id = $1;`;
|
|
await database.executeQuery(query, [id, name]);
|
|
return new Team(id, name);
|
|
}
|
|
|
|
async function remove(id) {
|
|
const query = `DELETE FROM scores.teams
|
|
WHERE team_id = $1
|
|
RETURNING team_name;`;
|
|
const name = (await database.executeQuery(query, [id]))[0][0];
|
|
return new Team(id, name);
|
|
}
|
|
|
|
async function retrieveBySport(sportID) {
|
|
const query = `SELECT *
|
|
FROM scores.teams
|
|
WHERE sport_id = $1
|
|
ORDER BY team_name;`;
|
|
const table = await database.executeQuery(query, [sportID]);
|
|
|
|
const teamsList = [];
|
|
table.forEach((row) => {
|
|
teamsList.push(new Team(row[0], row[1]));
|
|
});
|
|
return teamsList;
|
|
}
|
|
|
|
async function getFromID(id) {
|
|
const query = `SELECT team_name
|
|
FROM scores.teams
|
|
WHERE team_id = $1;`;
|
|
const name = (await database.executeQuery(query, [id]))[0][0];
|
|
return new Team(id, name);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
exports.add = add;
|
|
exports.rename = rename;
|
|
exports.remove = remove;
|
|
exports.retrieveBySport = retrieveBySport;
|
|
exports.getFromID = getFromID; |