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 create(name) {
|
2021-11-21 04:45:28 +00:00
|
|
|
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) {
|
2021-11-21 04:45:28 +00:00
|
|
|
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) {
|
2021-11-21 04:45:28 +00:00
|
|
|
const query = `DELETE FROM scores.sports
|
2021-11-21 00:58:29 +00:00
|
|
|
WHERE sport_id = $1
|
|
|
|
RETURNING sport_name;`;
|
2021-11-21 04:45:28 +00:00
|
|
|
const name = (await database.executeQuery(query, [id]))[0][0];
|
2021-11-21 00:58:29 +00:00
|
|
|
return new Sport(id, name);
|
|
|
|
}
|
|
|
|
|
2021-11-21 01:07:37 +00:00
|
|
|
async function retrieveAll() {
|
2021-11-21 04:45:28 +00:00
|
|
|
const query = `SELECT *
|
2021-11-21 01:07:37 +00:00
|
|
|
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.create = create;
|
2021-11-21 00:58:29 +00:00
|
|
|
exports.rename = rename;
|
2021-11-21 01:07:37 +00:00
|
|
|
exports.remove = remove;
|
|
|
|
exports.retrieveAll = retrieveAll;
|