Skip to content

Commit 9dd9095

Browse files
committed
Add file containing studentController class with two static methods
1 parent dc7dd4f commit 9dd9095

File tree

1 file changed

+34
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)