From f6efa74a89fafeced98b1b7c53432b5e5b96b21f Mon Sep 17 00:00:00 2001 From: matt Date: Fri, 25 Feb 2022 15:52:07 +0000 Subject: [PATCH] Added Class constructor --- lib/Class.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/Class.js b/lib/Class.js index 7a5e578..559d329 100644 --- a/lib/Class.js +++ b/lib/Class.js @@ -1,10 +1,57 @@ 'use strict'; +const DatabaseConnectionPool = require('./DatabaseConnectionPool'); + class Class { - classId; + /** + * The id of the class + * @type {string} + */ + id; + + /** + * The id of the subject the class is for + * @type {string} + */ subjectId; + + /** + * The name of the class + * @type {string} + */ name; + /** + * @param {string} classID - The id of the class to fetch + */ + constructor(classId) { + const sql = ` + select + classId as id, + name, + subjectId + from + class + where + classId = ?; + `; + + return (async () => { + const conn = await new DatabaseConnectionPool(); + const record = await conn.runQuery(sql, [ + classId, + ]); + + if (!record.length) + throw new Error('No class found'); + + for (const [ k, v ] of Object.entries(record[0])) + this[k] = v; + + return this; + })(); + } + get subject() { }