mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-02 10:39:33 +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 = ?;
|
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 () => {
|
return (async () => {
|
||||||
const record = await this.#conn.runQuery(sql, [
|
const record = await this.#conn.runQuery(sql, [
|
||||||
classId
|
classId
|
||||||
@@ -107,25 +85,13 @@ class Class {
|
|||||||
for (const [ k, v ] of Object.entries(record[0]))
|
for (const [ k, v ] of Object.entries(record[0]))
|
||||||
this[k] = v;
|
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 [
|
const [
|
||||||
teachers,
|
teachers,
|
||||||
students,
|
students,
|
||||||
subject,
|
subject,
|
||||||
] = await Promise.all([
|
] = await Promise.all([
|
||||||
this.getUsers(this.teacherIds, 'account'),
|
this.getTeachers(),
|
||||||
this.getUsers(this.studentIds, 'student'),
|
this.getStudents(),
|
||||||
this.getSubject()
|
this.getSubject()
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -141,6 +107,47 @@ class Class {
|
|||||||
return new (require('./Subject'))(this.#conn, this.subjectId);
|
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) {
|
getUsers(ids, type) {
|
||||||
const types = {
|
const types = {
|
||||||
account: 'Account',
|
account: 'Account',
|
||||||
|
|||||||
Reference in New Issue
Block a user