1
- var configuration1 = { id : Math . random ( ) , name : "host1" , hostname : "nessus-ntp.lab.com" , port : "1241" , username : "toto" } ;
2
- var configuration2 = { id : Math . random ( ) , name : "host2" , hostname : "nessus-xml.lab.com" , port : "3384" , username : "admin" } ;
3
- var existingConfigurations = [ configuration1 , configuration2 ] ;
1
+ var configGenerator = require ( './configGenerator' ) ;
2
+ var utility = require ( './utility' ) ;
3
+
4
+ var pageSize = 5 ;
5
+ var existingConfigurations = configGenerator . generateConfigs ( 30 ) ;
6
+ var acceptedSortByProperties = [ 'hostname' , 'name' , 'username' , 'port' ] ;
4
7
5
8
module . exports = {
6
- process : function process ( method , requestedResource , key , value , requestBody ) {
7
- var result = {
8
- success : true ,
9
- statusCode : 200 ,
10
- header : { "Content-Type" : "application/json" }
11
- }
12
- var data ;
13
- if ( method === 'POST' || method === 'PUT' ) {
14
- data = JSON . parse ( requestBody ) ;
15
- }
16
-
17
- switch ( method ) {
18
- case 'GET' :
19
- if ( ! key ) {
20
- result . message = JSON . stringify ( existingConfigurations ) ; //return all configurations.
21
- } else {
22
- var matchingConfigs = existingConfigurations . filter ( function ( el ) {
23
- return el [ key ] == value ;
24
- } ) ;
25
-
26
- if ( matchingConfigs ) {
27
- result . message = JSON . stringify ( matchingConfigs ) ;
28
- } else {
29
- result . statusCode = 404 ;
30
- result . success = false ;
31
- result . message = 'No configuration found with key ' + key + ' and value ' + value ;
32
- }
33
- }
34
- break ;
35
- case 'POST' :
36
- if ( data ) {
37
- var newConfig = data ;
38
- data . id = Math . random ( ) ;
39
- existingConfigurations . push ( data ) ;
40
- result . message = 'Created new config with id ' + data . id
41
- result . statusCode = 201 ;
42
- result . header . location = "localhost:3000/configuration/id/" + data . id
43
- } else {
44
- result . statusCode = 400 ;
45
- result . success = false ;
46
- result . message = 'Please supply data to create new config.' ;
47
- }
48
- break ;
49
- case 'PUT' :
50
- if ( 'id' === key ) {
51
- var matchingConfig = getMatchingConfig ( key , value ) ;
52
- console . log ( 'Matching config for update ' + matchingConfig )
53
- if ( ! matchingConfig ) {
54
- result . message = 'No matching config found to update for id ' + value ;
55
- } else if ( matchingConfig [ key ] !== data [ key ] ) {
56
- result . message = 'Config ID in the path ' + value + ' does not match the config ID ' + data [ key ] + ' in the request body.'
57
- } else {
58
- for ( var k in data ) {
59
- if ( data . hasOwnProperty ( k ) && k !== 'id' ) {
60
- matchingConfig [ k ] = data [ k ]
61
- }
62
- }
63
- result . message = 'Config with id ' + value + ' has been updated.'
64
- }
65
-
66
- } else {
67
- result . statusCode = 400 ;
68
- result . success = false ;
69
- result . message = 'Please provide config unique ID to update an existing config.' ;
70
- }
71
- break ;
72
- case 'DELETE' :
73
- if ( 'id' === key ) {
74
- var matchingConfig = getMatchingConfig ( key , value ) ;
75
- existingConfigurations . splice ( existingConfigurations . indexOf ( matchingConfig ) , 1 ) ;
76
- result . message = 'Config with id ' + value + ' has been deleted.' ;
77
- } else {
78
- result . statusCode = 400 ;
79
- result . success = false ;
80
- result . message = 'Please provide config unique ID to delete an existing config.' ;
81
- }
82
- break ;
83
- default :
84
- result . statusCode = 400 ;
85
- result . success = false ;
86
- result . message = 'This action is not supported.' ;
87
- break ;
88
- }
89
- return result ;
90
- }
91
- }
9
+ process : function process ( method , requestedResource , key , value , requestBody , queries ) {
10
+ var result = {
11
+ success : true ,
12
+ statusCode : 200 ,
13
+ header : { "Content-Type" : "application/json" }
14
+ } ;
15
+ var data , matchingConfig ;
16
+ if ( method === 'POST' || method === 'PUT' ) {
17
+ data = JSON . parse ( requestBody ) ;
18
+ }
19
+
20
+ switch ( method ) {
21
+ case 'GET' :
22
+ if ( ! key ) {
23
+ if ( Object . keys ( queries ) . length ) {
24
+ result . message = JSON . stringify ( massageData ( existingConfigurations , queries ) ) ;
25
+ } else {
26
+ result . message = JSON . stringify ( existingConfigurations ) ; //return all configurations.
27
+ }
28
+ } else {
29
+ var matchingConfigs = existingConfigurations . filter ( function ( el ) {
30
+ return el [ key ] == value ;
31
+ } ) ;
32
+
33
+ if ( matchingConfigs ) {
34
+ if ( Object . keys ( queries ) . length ) {
35
+ result . message = JSON . stringify ( massageData ( matchingConfigs , queries ) ) ;
36
+ } else {
37
+ result . message = JSON . stringify ( matchingConfigs ) ;
38
+ }
39
+
40
+ } else {
41
+ result . statusCode = 404 ;
42
+ result . success = false ;
43
+ result . message = 'No configuration found with key ' + key + ' and value ' + value ;
44
+ }
45
+ }
46
+ break ;
47
+ case 'POST' :
48
+ if ( data ) {
49
+ data . id = Math . random ( ) ;
50
+ existingConfigurations . push ( data ) ;
51
+ result . message = 'Created new config with id ' + data . id ;
52
+ result . statusCode = 201 ;
53
+ result . header . location = "localhost:3000/configuration/id/" + data . id ;
54
+ } else {
55
+ result . statusCode = 400 ;
56
+ result . success = false ;
57
+ result . message = 'Please supply data to create new config.' ;
58
+ }
59
+ break ;
60
+ case 'PUT' :
61
+ if ( 'id' === key ) {
62
+ matchingConfig = getMatchingConfig ( key , value ) ;
63
+
64
+ if ( ! matchingConfig ) {
65
+ result . message = 'No matching config found to update for id ' + value ;
66
+ } else if ( matchingConfig [ key ] !== data [ key ] ) {
67
+ result . message = 'Config ID in the path ' + value + ' does not match the config ID ' + data [ key ] + ' in the request body.' ;
68
+ } else {
69
+ for ( var k in data ) {
70
+ if ( data . hasOwnProperty ( k ) && k !== 'id' ) {
71
+ matchingConfig [ k ] = data [ k ]
72
+ }
73
+ }
74
+ result . message = 'Config with id ' + value + ' has been updated.' ;
75
+ }
76
+
77
+ } else {
78
+ result . statusCode = 400 ;
79
+ result . success = false ;
80
+ result . message = 'Please provide config unique ID to update an existing config.' ;
81
+ }
82
+ break ;
83
+ case 'DELETE' :
84
+ if ( 'id' === key ) {
85
+ matchingConfig = getMatchingConfig ( key , value ) ;
86
+ existingConfigurations . splice ( existingConfigurations . indexOf ( matchingConfig ) , 1 ) ;
87
+ result . message = 'Config with id ' + value + ' has been deleted.' ;
88
+ } else {
89
+ result . statusCode = 400 ;
90
+ result . success = false ;
91
+ result . message = 'Please provide config unique ID to delete an existing config.' ;
92
+ }
93
+ break ;
94
+ default :
95
+ result . statusCode = 400 ;
96
+ result . success = false ;
97
+ result . message = 'This action is not supported.' ;
98
+ break ;
99
+ }
100
+ return result ;
101
+ }
102
+ } ;
92
103
93
104
/*
94
- Returns the first config that matches the key value pair. Use it isolation only when you are expecting unique key.
95
- */
96
- function getMatchingConfig ( key , value ) {
97
- for ( var index in existingConfigurations ) {
98
- var config = existingConfigurations [ index ] ;
99
- if ( config [ key ] == value ) {
100
- return config ;
101
- }
102
- }
103
- }
105
+ Returns the first config that matches the key value pair. Use it isolation only when you are expecting unique key.
106
+ */
107
+ function getMatchingConfig ( key , value ) {
108
+ for ( var index in existingConfigurations ) {
109
+ var config = existingConfigurations [ index ] ;
110
+ if ( config [ key ] == value ) {
111
+ return config ;
112
+ }
113
+ }
114
+ }
115
+
116
+ function massageData ( configs , queries ) {
117
+ var result = configs ;
118
+ if ( queries . hasOwnProperty ( 'page' ) ) {
119
+ if ( queries [ 'page' ] > 0 ) {
120
+ var startIndex = ( queries [ 'page' ] - 1 ) * 5 ;
121
+ result = configs . slice ( startIndex , startIndex + pageSize )
122
+ }
123
+ }
124
+
125
+ if ( queries . hasOwnProperty ( 'sort_by' ) ) {
126
+ var sortBy = queries [ 'sort_by' ] ;
127
+ if ( acceptedSortByProperties . indexOf ( sortBy ) < 0 ) {
128
+ return result ;
129
+ }
130
+
131
+ var primer = function ( a ) { return a . toUpperCase ( ) } ;
132
+
133
+ if ( 'port' == sortBy ) {
134
+ primer = parseInt ;
135
+ }
136
+
137
+ var sortOrder = 'asc' ;
138
+
139
+ if ( queries . hasOwnProperty ( 'sort_order' ) && queries [ 'sort_order' ] == 'desc' ) {
140
+ sortOrder = 'desc' ;
141
+ }
142
+ result . slice ( ) . sort ( utility . customSort ( sortBy , sortOrder , primer ) ) ;
143
+ }
144
+
145
+ return result ;
146
+ }
147
+
148
+
0 commit comments