diff --git a/lib/Class.js b/lib/Class.js index 0a94c13..2007c73 100644 --- a/lib/Class.js +++ b/lib/Class.js @@ -74,28 +74,6 @@ class Class { classId = ?; `; - const teacherSQL = ` - select - a.accountId as id - from - account a - join accountClassLink acl using (accountId) - where - acl.classId = ?; - - `; - - const studentSQL = ` - select - s.studentId as id - from - student s - join studentClassLink scl using (studentId) - where - scl.classId = ?; - - `; - return (async () => { const record = await this.#conn.runQuery(sql, [ classId @@ -107,25 +85,13 @@ class Class { for (const [ k, v ] of Object.entries(record[0])) this[k] = v; - const [ teacherRes, studentRes ] = await Promise.all([ - this.#conn.runQuery(teacherSQL, [ - classId - ]), - this.#conn.runQuery(studentSQL, [ - classId - ]) - ]); - - this.teacherIds = teacherRes.map(record => record.id); - this.studentIds = studentRes.map(record => record.id); - const [ teachers, students, subject, ] = await Promise.all([ - this.getUsers(this.teacherIds, 'account'), - this.getUsers(this.studentIds, 'student'), + this.getTeachers(), + this.getStudents(), this.getSubject() ]); @@ -141,6 +107,47 @@ class Class { return new (require('./Subject'))(this.#conn, this.subjectId); } + async getTeachers() { + const sql = ` + select + a.accountId as id + from + account a + join accountClassLink acl using (accountId) + where + acl.classId = ?; + + `; + + const res = await this.#conn.runQuery(sql, [ this.id ]); + + this.teacherIds = res.map(record => record.id); + + this.teachers = await this.getUsers(this.teacherIds, 'account'); + + return this.teachers; + } + + async getStudents() { + const sql = ` + select + s.studentId as id + from + student s + join studentClassLink scl using (studentId) + where + scl.classId = ?; + `; + + const res = await this.#conn.runQuery(sql, [ this.id ]); + + this.studentIds = res.map(record => record.id); + + this.students = await this.getUsers(this.studentIds, 'student'); + + return this.students; + } + getUsers(ids, type) { const types = { account: 'Account',