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/sports.js

58 lines
1.3 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;
}
exports.add = add;
exports.rename = rename;
exports.remove = remove;
exports.retrieveAll = retrieveAll;