diff --git a/lib/Class.js b/lib/Class.js index 8a9f44d..11ce169 100644 --- a/lib/Class.js +++ b/lib/Class.js @@ -27,6 +27,18 @@ class Class { */ name; + /** + * The ids of the teachers assigned to the class + * @type {Array} + */ + teacherIds; + + /** + * The teachers assigned to the class + * @type {Array} + */ + teachers; + /** * @param {string} classID - The id of the class to fetch */ @@ -42,10 +54,21 @@ class Class { classId = ?; `; + const teacherIdSQL = ` + select + a.accountId as id + from + account a + join accountClassLink acl using (accountId) + where + acl.classId = ?; + + `; + return (async () => { const conn = await new DatabaseConnectionPool(); const record = await conn.runQuery(sql, [ - classId, + classId ]); if (!record.length) @@ -54,7 +77,19 @@ class Class { for (const [ k, v ] of Object.entries(record[0])) this[k] = v; - this.subject = await this.getSubject(); + const ids = await conn.runQuery(teacherIdSQL, [ + classId + ]); + + this.teacherIds = ids.map(record => record.id); + + const [ teachers, subject ] = await Promise.all([ + this.getTeachers(), + this.getSubject() + ]); + + this.teachers = teachers; + this.subject = subject; return this; })(); @@ -64,17 +99,18 @@ class Class { return new (require('./Subject'))(this.subjectId); } + async getTeachers() { + const teacherPromises = this.teacherIds.map(id => { + return new (require('./Account'))(id); + }); + return Promise.all(teacherPromises); } get tests() { } - get teachers() { - - } - get students() { }