File tree 1 file changed +34
-0
lines changed
0x05-Node_JS_basic/full_server/controllers
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments