From 2db5bc4480e69d00e2ccfc470e0b819abe6f4993 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 19:14:43 -0700 Subject: [PATCH] Create functions for database management of divisions --- database/scores/divisions.js | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 database/scores/divisions.js diff --git a/database/scores/divisions.js b/database/scores/divisions.js new file mode 100644 index 0000000..0a0e8be --- /dev/null +++ b/database/scores/divisions.js @@ -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); +} \ No newline at end of file