From 6ee3e90f9b3a87694106991ed8fe7c535cd80088 Mon Sep 17 00:00:00 2001 From: matt Date: Fri, 25 Feb 2022 19:52:45 +0000 Subject: [PATCH] Added Subject constructor --- lib/Subject.js | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/Subject.js b/lib/Subject.js index 61ec68e..8fae310 100644 --- a/lib/Subject.js +++ b/lib/Subject.js @@ -1,11 +1,48 @@ 'use strict'; +const DatabaseConnectionPool = require('./DatabaseConnectionPool'); + class Subject { - subjectId; + /** + * The id of the subject + * @type {number} + */ + id; + + /** + * The name of the subject + * @type {string} + */ name; - constructor() { + /** + * @param {number} subjectID - The id of the subject to fetch + */ + constructor(subjectId) { + const sql = ` + select + subjectId as id, + name + from + subject + where + subjectId = ?; + `; + return (async () => { + const conn = await new DatabaseConnectionPool(); + const record = await conn.runQuery(sql, [ + subjectId, + ]); + + if (!record.length) + throw new Error('No subject found'); + + for (const [ k, v ] of Object.entries(record[0])) + this[k] = v; + + return this; + })(); } }