Create functions for database management of divisions
parent
f86717d99f
commit
2db5bc4480
|
@ -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);
|
||||||
|
}
|
Reference in New Issue