mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-02 05:39:32 +00:00
Refactor Class.getTeachers and Class.getStudents into their own functions
This commit is contained in:
79
lib/Class.js
79
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',
|
||||
|
||||
Reference in New Issue
Block a user