67 lines
1.5 KiB
JavaScript
67 lines
1.5 KiB
JavaScript
const database = require('./../database');
|
|
|
|
|
|
|
|
class Sport {
|
|
constructor(id, name) {
|
|
this.id = id;
|
|
this.name = name;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async function add(name) {
|
|
const query = `INSERT INTO scores.sports(sport_name)
|
|
VALUES($1)
|
|
RETURNING sport_id;`;
|
|
const id = (await database.executeQuery(query, [name]))[0][0];
|
|
return new Sport(id, name);
|
|
}
|
|
|
|
async function rename(id, name) {
|
|
const query = `UPDATE scores.sports
|
|
SET sport_name = $2
|
|
WHERE sport_id = $1;`;
|
|
await database.executeQuery(query, [id, name]);
|
|
return new Sport(id, name);
|
|
}
|
|
|
|
async function remove(id) {
|
|
const query = `DELETE FROM scores.sports
|
|
WHERE sport_id = $1
|
|
RETURNING sport_name;`;
|
|
const name = (await database.executeQuery(query, [id]))[0][0];
|
|
return new Sport(id, name);
|
|
}
|
|
|
|
async function retrieveAll() {
|
|
const query = `SELECT *
|
|
FROM scores.sports
|
|
ORDER BY sport_name;`;
|
|
const table = await database.executeQuery(query);
|
|
|
|
const sportsList = [];
|
|
table.forEach((row) => {
|
|
sportsList.push(new Sport(row[0], row[1]));
|
|
});
|
|
return sportsList;
|
|
}
|
|
|
|
async function getFromID(id) {
|
|
const query = `SELECT sport_name
|
|
FROM scores.sports
|
|
WHERE sport_id = $1;`;
|
|
const name = (await database.executeQuery(query, [id]))[0][0];
|
|
return new Sport(id, name);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
exports.add = add;
|
|
exports.rename = rename;
|
|
exports.remove = remove;
|
|
exports.retrieveAll = retrieveAll;
|
|
exports.getFromID = getFromID; |