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
Raw Normal View History

2021-11-21 00:52:09 +00:00
const database = require('./../database');
2021-11-20 23:53:13 +00:00
class Sport {
2021-11-21 00:52:09 +00:00
constructor(id, name) {
2021-11-20 23:53:13 +00:00
this.id = id;
2021-11-21 00:52:09 +00:00
this.name = name;
2021-11-20 23:53:13 +00:00
}
}
2021-11-21 00:52:09 +00:00
async function add(name) {
const query = `INSERT INTO scores.sports(sport_name)
2021-11-21 00:52:09 +00:00
VALUES($1)
RETURNING sport_id;`;
2021-11-21 00:58:29 +00:00
const id = (await database.executeQuery(query, [name]))[0][0];
return new Sport(id, name);
2021-11-21 00:52:09 +00:00
}
2021-11-20 23:53:13 +00:00
2021-11-21 00:52:09 +00:00
async function rename(id, name) {
const query = `UPDATE scores.sports
2021-11-21 00:52:09 +00:00
SET sport_name = $2
2021-11-21 00:58:29 +00:00
WHERE sport_id = $1;`;
2021-11-21 00:52:09 +00:00
await database.executeQuery(query, [id, name]);
return new Sport(id, name);
2021-11-20 23:53:13 +00:00
}
2021-11-21 00:58:29 +00:00
async function remove(id) {
const query = `DELETE FROM scores.sports
2021-11-21 00:58:29 +00:00
WHERE sport_id = $1
RETURNING sport_name;`;
const name = (await database.executeQuery(query, [id]))[0][0];
2021-11-21 00:58:29 +00:00
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;
}
2021-11-20 23:53:13 +00:00
2021-11-21 00:52:09 +00:00
exports.add = add;
2021-11-21 00:58:29 +00:00
exports.rename = rename;
exports.remove = remove;
exports.retrieveAll = retrieveAll;