2021-11-21 02:14:43 +00:00
|
|
|
const database = require('./../database');
|
2021-11-21 23:30:36 +00:00
|
|
|
const genders = require('./genders');
|
2021-11-21 02:14:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Division {
|
2021-11-22 22:20:59 +00:00
|
|
|
constructor(id, name, gender, sportID) {
|
2021-11-21 02:14:43 +00:00
|
|
|
this.id = id;
|
|
|
|
this.name = name;
|
2021-11-22 22:20:59 +00:00
|
|
|
this.gender = gender;
|
|
|
|
this.sportID = sportID;
|
2021-11-21 02:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-11-21 04:25:29 +00:00
|
|
|
function getGenderID(gender) {
|
2021-11-21 23:30:36 +00:00
|
|
|
return (gender == genders.MALE) ? "M" : "F";
|
2021-11-21 04:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 23:27:50 +00:00
|
|
|
function getGenderFromID(genderID) {
|
|
|
|
return (genderID == "F") ? genders.FEMALE : genders.MALE;
|
|
|
|
}
|
|
|
|
|
2021-11-21 04:25:29 +00:00
|
|
|
|
|
|
|
|
2021-11-21 05:01:55 +00:00
|
|
|
async function add(name, gender, sportID) {
|
2021-11-21 04:45:28 +00:00
|
|
|
const query = `INSERT INTO scores.divisions(division_name,gender,sport_id)
|
2021-11-21 02:14:43 +00:00
|
|
|
VALUES($1,$2,$3)
|
|
|
|
RETURNING division_id;`;
|
2021-11-21 04:25:29 +00:00
|
|
|
const genderID = getGenderID(gender);
|
2021-11-21 02:14:43 +00:00
|
|
|
const id = (await database.executeQuery(query, [name, genderID, sportID]))[0][0];
|
|
|
|
return new Division(id, name);
|
|
|
|
}
|
|
|
|
|
2021-11-22 22:20:59 +00:00
|
|
|
async function rename(id, name) {
|
2021-11-21 04:45:28 +00:00
|
|
|
const query = `UPDATE scores.divisions
|
2021-11-21 02:14:43 +00:00
|
|
|
SET division_name = $2
|
|
|
|
WHERE division_id = $1;`;
|
|
|
|
await database.executeQuery(query, [id, name]);
|
|
|
|
return new Division(id, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function remove(id) {
|
2021-11-21 04:45:28 +00:00
|
|
|
const query = `DELETE FROM scores.divisions
|
2021-11-21 02:14:43 +00:00
|
|
|
WHERE division_id = $1
|
|
|
|
RETURNING division_name;`;
|
2021-11-21 04:45:28 +00:00
|
|
|
const name = (await database.executeQuery(query, [id]))[0][0];
|
2021-11-21 02:14:43 +00:00
|
|
|
return new Division(id, name);
|
2021-11-21 04:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 22:20:59 +00:00
|
|
|
async function retrieve(sportID = undefined, gender = undefined) {
|
|
|
|
let table;
|
|
|
|
|
|
|
|
if(sportID && gender) {
|
|
|
|
const query = `SELECT division_id, division_name, gender, sport_id
|
2021-11-21 04:25:29 +00:00
|
|
|
FROM scores.divisions
|
|
|
|
WHERE sport_id = $1 AND gender = $2
|
|
|
|
ORDER BY division_name;`;
|
2021-11-22 22:20:59 +00:00
|
|
|
const genderID = getGenderID(gender);
|
|
|
|
table = await database.executeQuery(query, [sportID, genderID]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const query = `SELECT division_id, division_name, gender, sport_id
|
|
|
|
FROM scores.divisions
|
2021-11-22 22:38:20 +00:00
|
|
|
ORDER BY sport_id,
|
|
|
|
division_name,
|
2021-11-22 22:20:59 +00:00
|
|
|
gender;`;
|
|
|
|
table = await database.executeQuery(query);
|
|
|
|
}
|
2021-11-21 04:25:29 +00:00
|
|
|
|
|
|
|
const divisionsList = [];
|
|
|
|
table.forEach((row) => {
|
2021-11-22 23:27:50 +00:00
|
|
|
divisionsList.push(new Division(row[0], row[1], getGenderFromID(row[2]), row[3]));
|
2021-11-21 04:25:29 +00:00
|
|
|
});
|
|
|
|
return divisionsList;
|
2021-11-21 04:46:28 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 23:27:50 +00:00
|
|
|
async function getFromID(id) {
|
|
|
|
const query = `SELECT division_id, division_name, gender, sport_id
|
|
|
|
FROM scores.divisions
|
|
|
|
WHERE division_id = $1;`;
|
|
|
|
const row = (await database.executeQuery(query, [id]))[0];
|
|
|
|
|
|
|
|
|
|
|
|
return new Division(id, row[1], getGenderFromID(row[2]), row[3]);
|
|
|
|
}
|
|
|
|
|
2021-11-21 04:46:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-11-21 05:01:55 +00:00
|
|
|
exports.add = add;
|
2021-11-21 04:46:28 +00:00
|
|
|
exports.rename = rename;
|
|
|
|
exports.remove = remove;
|
2021-11-22 23:27:50 +00:00
|
|
|
exports.retrieve = retrieve;
|
|
|
|
exports.getFromID = getFromID;
|