1
0
mirror of https://github.com/matt-fidd/stratos.git synced 2026-01-01 16:19:26 +00:00

Added Subject constructor

This commit is contained in:
2022-02-25 19:52:45 +00:00
parent 28e95b8cc3
commit 6ee3e90f9b

View File

@@ -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;
})();
}
}