@@ -10,143 +10,28 @@ const swaggerUI = require('swagger-ui-express');
10
10
const swaggerFile = require ( './resources/api-v1-swagger.json' ) ;
11
11
12
12
const { errorHandler } = require ( './lib/errorHandler' ) ;
13
+ const RideRepository = require ( './modules/Ride/RideRepository' ) ;
14
+ const RideController = require ( './modules/Ride/RideController' ) ;
15
+ const RideEntity = require ( './modules/Ride/RideEntity' ) ;
16
+ const { selectQuery, insertQuery } = require ( './lib/databaseQuery' ) ;
13
17
14
18
module . exports = ( db ) => {
19
+ // Manually inject instances
20
+ const repository = new RideRepository ( db , RideEntity , selectQuery , insertQuery ) ;
21
+ const controller = new RideController ( repository ) ;
22
+
15
23
app . get ( '/health' , ( req , res ) => res . send ( 'Healthy' ) ) ;
16
24
17
25
app . use ( '/api-documentation/v1' , swaggerUI . serve , swaggerUI . setup ( swaggerFile ) ) ;
18
26
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 ) ;
85
- }
86
- res . send ( rows ) ;
87
- } ) ;
88
- } ) ;
27
+ app . post ( '/rides' , jsonParser , function ( req , res , next ) {
28
+ controller . postRide ( req , res , next ) ;
89
29
} ) ;
90
-
91
- app . get ( '/rides' , ( req , res , next ) => {
92
- const { page = 1 , limit = 10 } = req . query ;
93
- if ( isNaN ( page ) || page < 1 || isNaN ( limit ) || limit < 1 ) {
94
- return res . send ( {
95
- error_code : 'INVALID_PAGINATION_VALUES' ,
96
- message : 'Pagination values should be a number and not be less than 1' ,
97
- } ) ;
98
- }
99
-
100
- const offset = ( page - 1 ) * limit ;
101
- const parsedPage = Number ( page ) ;
102
- const nextUrl = `/rides?page=${ parsedPage + 1 } &limit=${ limit } ` ;
103
- const previousUrl = parsedPage === 1 ? null : `/rides?page=${ parsedPage - 1 } &limit=${ limit } ` ;
104
-
105
- db . all ( 'SELECT * FROM Rides LIMIT ? OFFSET ?' , [ limit , offset ] , ( err , rideEntities ) => {
106
- if ( err ) {
107
- return next ( err ) ;
108
- }
109
-
110
- if ( rideEntities . length === 0 ) {
111
- return res . send ( {
112
- error_code : 'RIDES_NOT_FOUND_ERROR' ,
113
- message : 'Could not find any rides' ,
114
- } ) ;
115
- }
116
-
117
- db . all ( 'SELECT COUNT(*) as count FROM Rides' , ( countError , rows ) => {
118
- if ( countError ) {
119
- return next ( countError ) ;
120
- }
121
-
122
- const totalCount = rows [ 0 ] . count ;
123
- const hasNextPage = ( page * limit ) <= totalCount ;
124
- const response = {
125
- next : hasNextPage ? nextUrl : null ,
126
- previous : previousUrl ,
127
- totalCount : rows [ 0 ] . count ,
128
- results : rideEntities ,
129
- } ;
130
- res . send ( response ) ;
131
- } ) ;
132
- } ) ;
30
+ app . get ( '/rides' , function ( req , res , next ) {
31
+ controller . getRides ( req , res , next ) ;
133
32
} ) ;
134
-
135
- app . get ( '/rides/:id' , ( req , res , next ) => {
136
- db . all ( `SELECT * FROM Rides WHERE rideID='${ req . params . id } '` , ( err , rows ) => {
137
- if ( err ) {
138
- return next ( err ) ;
139
- }
140
-
141
- if ( rows . length === 0 ) {
142
- return res . send ( {
143
- error_code : 'RIDES_NOT_FOUND_ERROR' ,
144
- message : 'Could not find any rides' ,
145
- } ) ;
146
- }
147
-
148
- res . send ( rows ) ;
149
- } ) ;
33
+ app . get ( '/rides/:id' , function ( req , res , next ) {
34
+ controller . getRide ( req , res , next ) ;
150
35
} ) ;
151
36
152
37
app . use ( errorHandler ) ;
0 commit comments