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

70 lines
1.7 KiB
JavaScript

const database = require('./../database');
const genders = require('./genders');
class Division {
constructor(id, name) {
this.id = id;
this.name = name;
}
}
function getGenderID(gender) {
return (gender == genders.MALE) ? "M" : "F";
}
async function add(name, gender, sportID) {
const query = `INSERT INTO scores.divisions(division_name,gender,sport_id)
VALUES($1,$2,$3)
RETURNING division_id;`;
const genderID = getGenderID(gender);
const id = (await database.executeQuery(query, [name, genderID, sportID]))[0][0];
return new Division(id, name);
}
async function rename(id, division) {
const query = `UPDATE scores.divisions
SET division_name = $2
WHERE division_id = $1;`;
await database.executeQuery(query, [id, name]);
return new Division(id, name);
}
async function remove(id) {
const query = `DELETE FROM scores.divisions
WHERE division_id = $1
RETURNING division_name;`;
const name = (await database.executeQuery(query, [id]))[0][0];
return new Division(id, name);
}
async function retrieveBySportAndGender(sportID, gender) {
const query = `SELECT *
FROM scores.divisions
WHERE sport_id = $1 AND gender = $2
ORDER BY division_name;`;
const genderID = getGenderID(gender);
const table = await database.executeQuery(query, [sportID, genderID]);
const divisionsList = [];
table.forEach((row) => {
divisionsList.push(new Division(row[0], row[1]));
});
return divisionsList;
}
exports.add = add;
exports.rename = rename;
exports.remove = remove;
exports.retrieveBySportAndGender = retrieveBySportAndGender;