2021-11-21 02:14:43 +00:00
|
|
|
const database = require('./../database');
|
|
|
|
const genders = require('../../constants/genders');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Division {
|
|
|
|
constructor(id, name) {
|
|
|
|
this.id = id;
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-11-21 04:25:29 +00:00
|
|
|
function getGenderID(gender) {
|
|
|
|
return (gender == genders.male) ? "M" : "F";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-11-21 02:14:43 +00:00
|
|
|
async function create(name, gender, sportID) {
|
|
|
|
query = `INSERT INTO scores.divisions(division_name,gender,sport_id)
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function rename(id, division) {
|
|
|
|
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) {
|
|
|
|
query = `DELETE FROM scores.divisions
|
|
|
|
WHERE division_id = $1
|
|
|
|
RETURNING division_name;`;
|
|
|
|
name = (await database.executeQuery(query, [id]))[0][0];
|
|
|
|
return new Division(id, name);
|
2021-11-21 04:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function retrieveBySportAndGender(sportID, gender) {
|
|
|
|
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;
|
2021-11-21 02:14:43 +00:00
|
|
|
}
|