Create functions for database management of divisions

main
sudoer777 2021-11-20 19:14:43 -07:00
parent f86717d99f
commit 2db5bc4480
1 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,40 @@
const database = require('./../database');
const genders = require('../../constants/genders');
class Division {
constructor(id, name) {
this.id = id;
this.name = name;
}
}
async function create(name, gender, sportID) {
query = `INSERT INTO scores.divisions(division_name,gender,sport_id)
VALUES($1,$2,$3)
RETURNING division_id;`;
const genderID = (gender == genders.male) ? "M" : "F";
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);
}