-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudentsController.js
executable file
·34 lines (31 loc) · 1.18 KB
/
StudentsController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// file containing studentController class with 2 static methods
const readDatabase = require('../utils');
class StudentsController {
static getAllStudents(request, response) {
readDatabase(process.argv[2].toString()).then((students) => {
const output = [];
output.push('This is the list of our students');
const keys = Object.keys(students);
keys.sort();
for (let i = 0; i < keys.length; i += 1) {
output.push(`Number of students in ${keys[i]}: ${students[keys[i]].length}. List: ${students[keys[i]].join(', ')}`);
}
response.status(200).send(output.join('\n'));
}).catch(() => {
response.status(500).send('Cannot load the database');
});
}
static getAllStudentsByMajor(request, response) {
const field = request.params.major;
readDatabase(process.argv[2].toString()).then((students) => {
if (!(field in students)) {
response.status(500).send('Major parameter must be CS or SWE');
} else {
response.status(200).send(`List: ${students[field].join(', ')}`);
}
}).catch(() => {
response.status(500).send('Cannot load the database');
});
}
}
module.exports = StudentsController;