From f16cab9f8b9fde43aabc2f43496334395d02e28f Mon Sep 17 00:00:00 2001 From: matt Date: Fri, 25 Feb 2022 13:16:34 +0000 Subject: [PATCH] Added Test constructor --- lib/Test.js | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/lib/Test.js b/lib/Test.js index 299e47b..4ee4d42 100644 --- a/lib/Test.js +++ b/lib/Test.js @@ -1,5 +1,10 @@ 'use strict'; +// Import user defined modules +const DatabaseConnectionPool = require('./DatabaseConnectionPool'); + +const TestTemplate = require('./TestTemplate'); + /** * A class that represents the date of a test */ @@ -16,13 +21,75 @@ class TestDate extends Date { } class Test { - testId; - testTemplateId; + /** + * The id of the test + * @type {string} + */ + id; + + /** + * The id of the test template it is based on + * @type {string} + */ + templateId; + + /** + * The test template object that it is based on + * @type {TestTemplate} + */ + template; + + /** + * The id of the class it is assigned to + * @type {string} + */ classId; - testDate; - constructor () { + /** + * The test date in epoch seconds + * @type {number} + */ + epochDate; + /** + * The test date + * @type {TestDate} + */ + date; + + /** + * @param {string} testId - The id of the test to fetch + */ + constructor(testId) { + const sql = ` + select + testId as id, + testTemplateId as templateId, + classId, + UNIX_TIMESTAMP(testDate) as epochDate + from + test + where + testId = ?; + `; + + return (async () => { + const conn = await new DatabaseConnectionPool(); + const record = await conn.runQuery(sql, [ + testId, + ]); + + if (!record.length) + throw new Error('No test found'); + + for (const [ k, v ] of Object.entries(record[0])) + this[k] = v; + + this.date = new TestDate(this.epochDate * 1000); + this.template = await new TestTemplate(this.templateId); + + return this; + })(); } get class() {