From 6a36fae78ca4ce754c6c0be7214f5693658a58af Mon Sep 17 00:00:00 2001 From: matt Date: Thu, 31 Mar 2022 13:46:52 +0000 Subject: [PATCH] Refactor Class.getTeachers and Class.getStudents into their own functions --- lib/Class.js | 79 ++++++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 36 deletions(-) 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',