diff --git a/lib/Class.js b/lib/Class.js index e464bf2..2586ea3 100644 --- a/lib/Class.js +++ b/lib/Class.js @@ -40,6 +40,18 @@ class Class { */ teachers; + /** + * The ids of the students assigned to the class + * @type {Array} + */ + studentIds; + + /** + * The students assigned to the class + * @type {Array} + */ + students; + /** * @param {string} classID - The id of the class to fetch */ @@ -55,7 +67,7 @@ class Class { classId = ?; `; - const teacherIdSQL = ` + const teacherSQL = ` select a.accountId as id from @@ -66,8 +78,20 @@ class Class { `; + const studentSQL = ` + select + s.studentId as id + from + student s + join studentClassLink scl using (studentId) + where + scl.classId = ?; + + `; + return (async () => { const conn = await new DatabaseConnectionPool(); + const record = await conn.runQuery(sql, [ classId ]); @@ -78,20 +102,32 @@ class Class { for (const [ k, v ] of Object.entries(record[0])) this[k] = v; - const ids = await conn.runQuery(teacherIdSQL, [ - classId + const [ teacherRes, studentRes ] = await Promise.all([ + conn.runQuery(teacherSQL, [ + classId + ]), + conn.runQuery(studentSQL, [ + classId + ]) ]); conn.close(); - this.teacherIds = ids.map(record => record.id); + this.teacherIds = teacherRes.map(record => record.id); + this.studentIds = studentRes.map(record => record.id); - const [ teachers, subject ] = await Promise.all([ + const [ + teachers, + students, + subject, + ] = await Promise.all([ this.getUsers(this.teacherIds, 'account'), + this.getUsers(this.studentIds, 'student'), this.getSubject() ]); this.teachers = teachers; + this.students = students; this.subject = subject; return this; @@ -159,10 +195,6 @@ class Class { return await Promise.all(testObjects); } - get students() { - - } - addTeacher() { }