1
- 'use strict' ;
2
-
3
1
const express = require ( 'express' ) ;
2
+
4
3
const app = express ( ) ;
5
4
6
5
const bodyParser = require ( 'body-parser' ) ;
6
+
7
7
const jsonParser = bodyParser . json ( ) ;
8
8
9
9
const swaggerUI = require ( 'swagger-ui-express' ) ;
@@ -12,104 +12,117 @@ const swaggerFile = require('./resources/api-v1-swagger.json');
12
12
const { errorHandler } = require ( './lib/errorHandler' ) ;
13
13
14
14
module . exports = ( db ) => {
15
- app . get ( '/health' , ( req , res ) => res . send ( 'Healthy' ) ) ;
16
-
17
- app . use ( '/api-documentation/v1' , swaggerUI . serve , swaggerUI . setup ( swaggerFile ) ) ;
18
-
19
- app . post ( '/rides' , jsonParser , ( req , res , next ) => {
20
- const startLatitude = Number ( req . body . start_lat ) ;
21
- const startLongitude = Number ( req . body . start_long ) ;
22
- const endLatitude = Number ( req . body . end_lat ) ;
23
- const endLongitude = Number ( req . body . end_long ) ;
24
- const riderName = req . body . rider_name ;
25
- const driverName = req . body . driver_name ;
26
- const driverVehicle = req . body . driver_vehicle ;
27
-
28
- if ( startLatitude < - 90 || startLatitude > 90 || startLongitude < - 180 || startLongitude > 180 ) {
29
- return res . send ( {
30
- error_code : 'VALIDATION_ERROR' ,
31
- message : 'Start latitude and longitude must be between -90 - 90 and -180 to 180 degrees respectively'
32
- } ) ;
33
- }
34
-
35
- if ( endLatitude < - 90 || endLatitude > 90 || endLongitude < - 180 || endLongitude > 180 ) {
36
- return res . send ( {
37
- error_code : 'VALIDATION_ERROR' ,
38
- message : 'End latitude and longitude must be between -90 - 90 and -180 to 180 degrees respectively'
39
- } ) ;
40
- }
41
-
42
- if ( typeof riderName !== 'string' || riderName . length < 1 ) {
43
- return res . send ( {
44
- error_code : 'VALIDATION_ERROR' ,
45
- message : 'Rider name must be a non empty string'
46
- } ) ;
47
- }
48
-
49
- if ( typeof driverName !== 'string' || driverName . length < 1 ) {
50
- return res . send ( {
51
- error_code : 'VALIDATION_ERROR' ,
52
- message : 'Rider name must be a non empty string'
53
- } ) ;
54
- }
55
-
56
- if ( typeof driverVehicle !== 'string' || driverVehicle . length < 1 ) {
57
- return res . send ( {
58
- error_code : 'VALIDATION_ERROR' ,
59
- message : 'Rider name must be a non empty string'
60
- } ) ;
15
+ app . get ( '/health' , ( req , res ) => res . send ( 'Healthy' ) ) ;
16
+
17
+ app . use ( '/api-documentation/v1' , swaggerUI . serve , swaggerUI . setup ( swaggerFile ) ) ;
18
+
19
+ app . post ( '/rides' , jsonParser , ( req , res , next ) => {
20
+ const startLatitude = Number ( req . body . start_lat ) ;
21
+ const startLongitude = Number ( req . body . start_long ) ;
22
+ const endLatitude = Number ( req . body . end_lat ) ;
23
+ const endLongitude = Number ( req . body . end_long ) ;
24
+ const riderName = req . body . rider_name ;
25
+ const driverName = req . body . driver_name ;
26
+ const driverVehicle = req . body . driver_vehicle ;
27
+
28
+ if (
29
+ startLatitude < - 90
30
+ || startLatitude > 90
31
+ || startLongitude < - 180
32
+ || startLongitude > 180
33
+ ) {
34
+ return res . send ( {
35
+ error_code : 'VALIDATION_ERROR' ,
36
+ message : 'Start latitude and longitude must be between -90 - 90 and -180 to 180 degrees respectively' ,
37
+ } ) ;
38
+ }
39
+
40
+ if ( endLatitude < - 90 || endLatitude > 90 || endLongitude < - 180 || endLongitude > 180 ) {
41
+ return res . send ( {
42
+ error_code : 'VALIDATION_ERROR' ,
43
+ message : 'End latitude and longitude must be between -90 - 90 and -180 to 180 degrees respectively' ,
44
+ } ) ;
45
+ }
46
+
47
+ if ( typeof riderName !== 'string' || riderName . length < 1 ) {
48
+ return res . send ( {
49
+ error_code : 'VALIDATION_ERROR' ,
50
+ message : 'Rider name must be a non empty string' ,
51
+ } ) ;
52
+ }
53
+
54
+ if ( typeof driverName !== 'string' || driverName . length < 1 ) {
55
+ return res . send ( {
56
+ error_code : 'VALIDATION_ERROR' ,
57
+ message : 'Rider name must be a non empty string' ,
58
+ } ) ;
59
+ }
60
+
61
+ if ( typeof driverVehicle !== 'string' || driverVehicle . length < 1 ) {
62
+ return res . send ( {
63
+ error_code : 'VALIDATION_ERROR' ,
64
+ message : 'Rider name must be a non empty string' ,
65
+ } ) ;
66
+ }
67
+
68
+ const values = [
69
+ req . body . start_lat ,
70
+ req . body . start_long ,
71
+ req . body . end_lat ,
72
+ req . body . end_long ,
73
+ req . body . rider_name ,
74
+ req . body . driver_name ,
75
+ req . body . driver_vehicle ,
76
+ ] ;
77
+
78
+ db . run ( 'INSERT INTO Rides(startLat, startLong, endLat, endLong, riderName, driverName, driverVehicle) VALUES (?, ?, ?, ?, ?, ?, ?)' , values , function ( insertErr ) {
79
+ if ( insertErr ) {
80
+ return next ( insertErr ) ;
81
+ }
82
+ db . all ( 'SELECT * FROM Rides WHERE rideID = ?' , this . lastID , ( selectErr , rows ) => {
83
+ if ( selectErr ) {
84
+ return next ( selectErr ) ;
61
85
}
62
-
63
- var values = [ req . body . start_lat , req . body . start_long , req . body . end_lat , req . body . end_long , req . body . rider_name , req . body . driver_name , req . body . driver_vehicle ] ;
64
-
65
- const result = db . run ( 'INSERT INTO Rides(startLat, startLong, endLat, endLong, riderName, driverName, driverVehicle) VALUES (?, ?, ?, ?, ?, ?, ?)' , values , function ( err ) {
66
- if ( err ) {
67
- return next ( err ) ;
68
- }
69
- db . all ( 'SELECT * FROM Rides WHERE rideID = ?' , this . lastID , function ( err , rows ) {
70
- if ( err ) {
71
- return next ( err ) ;
72
- }
73
- res . send ( rows ) ;
74
- } ) ;
75
- } ) ;
86
+ res . send ( rows ) ;
87
+ } ) ;
76
88
} ) ;
77
-
78
- app . get ( '/rides' , ( req , res , next ) => {
79
- db . all ( 'SELECT * FROM Rides' , function ( err , rows ) {
80
- if ( err ) {
81
- return next ( err ) ;
82
- }
83
-
84
- if ( rows . length === 0 ) {
85
- return res . send ( {
86
- error_code : 'RIDES_NOT_FOUND_ERROR' ,
87
- message : 'Could not find any rides'
88
- } ) ;
89
- }
90
-
91
- res . send ( rows ) ;
89
+ } ) ;
90
+
91
+ app . get ( '/rides' , ( req , res , next ) => {
92
+ db . all ( 'SELECT * FROM Rides' , ( err , rows ) => {
93
+ if ( err ) {
94
+ return next ( err ) ;
95
+ }
96
+
97
+ if ( rows . length === 0 ) {
98
+ return res . send ( {
99
+ error_code : 'RIDES_NOT_FOUND_ERROR' ,
100
+ message : 'Could not find any rides' ,
92
101
} ) ;
93
- } ) ;
94
-
95
- app . get ( '/rides/:id' , ( req , res , next ) => {
96
- db . all ( `SELECT * FROM Rides WHERE rideID='${ req . params . id } '` , function ( err , rows ) {
97
- if ( err ) {
98
- return next ( err ) ;
99
- }
100
-
101
- if ( rows . length === 0 ) {
102
- return res . send ( {
103
- error_code : 'RIDES_NOT_FOUND_ERROR' ,
104
- message : 'Could not find any rides'
105
- } ) ;
106
- }
102
+ }
107
103
108
- res . send ( rows ) ;
104
+ res . send ( rows ) ;
105
+ } ) ;
106
+ } ) ;
107
+
108
+ app . get ( '/rides/:id' , ( req , res , next ) => {
109
+ db . all ( `SELECT * FROM Rides WHERE rideID='${ req . params . id } '` , ( err , rows ) => {
110
+ if ( err ) {
111
+ return next ( err ) ;
112
+ }
113
+
114
+ if ( rows . length === 0 ) {
115
+ return res . send ( {
116
+ error_code : 'RIDES_NOT_FOUND_ERROR' ,
117
+ message : 'Could not find any rides' ,
109
118
} ) ;
119
+ }
120
+
121
+ res . send ( rows ) ;
110
122
} ) ;
123
+ } ) ;
111
124
112
- app . use ( errorHandler ) ;
125
+ app . use ( errorHandler ) ;
113
126
114
- return app ;
127
+ return app ;
115
128
} ;
0 commit comments