@@ -3,6 +3,7 @@ const slotService = require("../services/slot.service");
3
3
const mbxGeocoding = require ( "@mapbox/mapbox-sdk/services/geocoding" ) ;
4
4
const mapBoxToken = process . env . MAPBOX_TOKEN ;
5
5
const geocoder = mbxGeocoding ( { accessToken : mapBoxToken } ) ;
6
+ var axios = require ( 'axios' ) ;
6
7
7
8
const renderAddGarage = ( req , res ) => {
8
9
res . render ( "garages/newgarages" , { body : req . body } ) ;
@@ -87,11 +88,87 @@ const deleteGarage = async (req, res) => {
87
88
res . redirect ( "/garage/" ) ;
88
89
}
89
90
} ;
91
+
92
+ const renderfindgarage = async ( req , res ) =>
93
+ {
94
+ res . render ( "garages/findgarage" , { body : req . body } ) ;
95
+ }
96
+
97
+ const rendergaragebyip = async ( req , res ) =>
98
+ {
99
+ if ( req . ipv4 !== undefined )
100
+ {
101
+ var config = {
102
+ method : 'get' ,
103
+ url : 'http://ip-api.com/json/'
104
+ } ;
105
+ axios ( config )
106
+ . then ( function ( response ) {
107
+ console . log ( JSON . stringify ( response . data ) ) ;
108
+ } )
109
+ . catch ( function ( error ) {
110
+ console . log ( error ) ;
111
+ } ) ;
112
+ }
113
+ res . render ( "garages/foundgarage" , { body : req . body , by :"IP" } )
114
+ }
115
+
116
+ const rendergaragebyloc = async ( req , res ) =>
117
+ {
118
+ try {
119
+ if ( ! req . body . location )
120
+ {
121
+ req . flash ( 'err' , 'location not given' ) ;
122
+ res . redirect ( '/garage/find' ) ;
123
+ }
124
+ else
125
+ {
126
+ const geoData = await geocoder
127
+ . forwardGeocode ( {
128
+ query : req . body . location ,
129
+ limit : 1 ,
130
+ } )
131
+ . send ( ) ;
132
+ var geometry = geoData . body . features [ 0 ] . geometry ;
133
+ geometry . place_name = req . body . location ;
134
+ var garages = await garageService . AllGarages ( ) ;
135
+ var min_distance = 10000000.0 ;
136
+ var dist = { } ;
137
+ for ( let garage of garages ) {
138
+ var distance = garageService . DistanceCal ( geometry . coordinates [ 1 ] , geometry . coordinates [ 0 ] ,
139
+ garage . geometry . coordinates [ 1 ] , garage . geometry . coordinates [ 0 ] ) ;
140
+ if ( distance <= min_distance )
141
+ {
142
+ dist = garage ;
143
+ min_distance = distance ;
144
+ }
145
+ }
146
+ if ( min_distance > 1000.0 )
147
+ {
148
+ req . flash ( "err" , "Sorry! No garages found within 1000.0 km radius." )
149
+ res . redirect ( "/garage/find" ) ;
150
+ }
151
+ else {
152
+ res . render ( "garages/foundgarage" , { body : req . body , by :"Location" , geometry :geometry , maptoken : mapBoxToken , garage :dist ,
153
+ min_distance :min_distance } ) ;
154
+ }
155
+ }
156
+ }
157
+ catch ( err )
158
+ {
159
+ req . flash ( "err" , "Err: " + err ) ;
160
+ res . redirect ( "/garage/find" ) ;
161
+ }
162
+ }
163
+
90
164
module . exports = {
91
165
renderAddGarage,
92
166
addGarage,
93
167
renderGarage,
94
168
renderAllGarages,
95
169
apiSlotInfo,
96
170
deleteGarage,
171
+ renderfindgarage,
172
+ rendergaragebyip,
173
+ rendergaragebyloc
97
174
} ;
0 commit comments