diff --git a/src/App.js b/src/App.js index 0bac0618110d139e6863148bf6a33d090243d81e..98bc4a35dee9e4655dd6eba3f9daadc1701e1c28 100644 --- a/src/App.js +++ b/src/App.js @@ -27,87 +27,6 @@ const JWT_ALGORITHM = 'HS512'; // }); - -// app.get('/location/parking-locations', async (request, response) => { -// try { -// const { postcode, radius } = request.query; - -// // Validate UK postcode -// if (!postcodeValidator(postcode, 'GB')) { -// return response.status(400).send('â›”ï¸ Invalid postcode format'); -// } - -// // Use OpenStreetMap Nominatim API to get latitude and longitude -// const url = `https://nominatim.openstreetmap.org/search?q=${postcode}&format=json&limit=1`; -// const result = await axios.get(url); -// const { lat, lon } = result.data[0]; -// console.log(lat,lon) - - -// // Query the database for parking locations -// const parkingLocations = await ParkingLocation.find({ -// location: { -// $near: { -// $geometry: { -// type: "Point", -// coordinates: [parseFloat(lon), parseFloat(lat)] -// }, -// $maxDistance: radius * 1609.34 // convert miles to meters -// } -// } -// }); - -// console.log(`🤖: Successfully retrieved ${parkingLocations.length} parking locations near ${postcode}:`); -// response.send(parkingLocations); -// } catch (x) { -// console.error(x); -// response.status(500).send('â›”ï¸ Server Error: An error has occurred, please try again later'); -// } -// }); - -// app.get('/location/parking-locations', async (request, response) => { -// try { -// const { postcode, radius } = request.query; -// console.log('postcode:', postcode); -// console.log('radius:', radius); - -// // Validate UK postcode -// if (!postcodeValidator(postcode, 'GB')) { -// console.log('Invalid postcode format'); -// return response.status(400).send('â›”ï¸ Invalid postcode format'); -// } - -// // Use OpenStreetMap Nominatim API to get latitude and longitude -// const url = `https://nominatim.openstreetmap.org/search?q=${postcode}&format=json&limit=1`; -// const result = await axios.get(url); -// console.log('Nominatim API result:', result.data); -// const { lat, lon } = result.data[0]; -// console.log('lat:', lat); -// console.log('lon:', lon); - -// // Query the database for parking locations -// const parkingLocations = await ParkingLocation.find({ -// location: { -// $near: { -// $geometry: { -// type: "Point", -// coordinates: [parseFloat(lon), parseFloat(lat)] -// }, -// $maxDistance: radius * 1609.34 // convert miles to meters -// } -// } -// }); -// console.log('parkingLocations:', parkingLocations); - -// console.log(`🤖: Successfully retrieved ${parkingLocations.length} parking locations near ${postcode}:`); -// response.send(parkingLocations); -// } catch (x) { -// console.error(x); -// response.status(500).send('â›”ï¸ Server Error: An error has occurred, please try again later'); -// } -// }); - - app.get('/location/parking-locations', async (request, response) => { try { const { postcode, radius } = request.query; @@ -130,8 +49,7 @@ app.get('/location/parking-locations', async (request, response) => { // Query the database for parking locations const parkingLocations = await ParkingLocation.find({ - postcode: { - $ne: null, + location: { $near: { $geometry: { type: "Point", @@ -143,14 +61,8 @@ app.get('/location/parking-locations', async (request, response) => { }); console.log('parkingLocations:', parkingLocations); - const nearbyParkingLocations = parkingLocations.filter(location => { - const locationPostcode = location.postcode.replace(/\s/g, ''); - const inputPostcode = postcode.replace(/\s/g, ''); - return locationPostcode.startsWith(inputPostcode.substring(0, inputPostcode.length - 2)); - }); - - console.log(`🤖: Successfully retrieved ${nearbyParkingLocations.length} parking locations near ${postcode}:`); - response.send(nearbyParkingLocations); + console.log(`🤖: Successfully retrieved ${parkingLocations.length} parking locations near ${postcode}:`); + response.send(parkingLocations); } catch (x) { console.error(x); response.status(500).send('â›”ï¸ Server Error: An error has occurred, please try again later'); @@ -158,6 +70,8 @@ app.get('/location/parking-locations', async (request, response) => { }); + + function isAdmin(request, response, next) { const authHeader = request.headers.authorization; const token = authHeader && authHeader.split(' ')[1]; @@ -180,34 +94,79 @@ function isAdmin(request, response, next) { } } -app.post('/location/parking-locations', isAdmin, async (request, response) => { + +app.post('/location/parking-locations', async (request, response) => { try { - const { Title, Description,city, street_address, postcode, lat, lon, spaces_available, total_spaces } = request.body; - - const existingLocation = await ParkingLocation.findOne({ street_address }); - if (existingLocation) { - return response.status(400).json({ error: 'â›”ï¸: A parking location with this street address already exists. Please try another alternative location. ' }); - } - - const parkingLocation = new ParkingLocation({ - Title, - Description, - city, - street_address, - postcode, - lat, - lon, - spaces_available, - total_spaces, - }); - console.log('🤖 Request Body:', request.body); - await parkingLocation.save(); - response.status(201).json({ message: '🤖: Parking was successfully added to database.' }); + const { + Title, + Description, + city, + street_address, + postcode, + lat, + lon, + spaces_available, + total_spaces + } = request.body; + const existingLocation = await ParkingLocation.findOne({ street_address }); + if (existingLocation) { + return response.status(400).json({ error: 'â›”ï¸: A parking location with this street address already exists. Please try another alternative location. ' }); + } + + // Use the lat and lon coordinates to create a GeoJSON point + const point = { + type: "Point", + coordinates: [parseFloat(lon), parseFloat(lat)] + }; + + const parkingLocation = new ParkingLocation({ + Title, + Description, + city, + street_address, + postcode, + location: point, + spaces_available, + total_spaces, + }); + console.log('🤖 Request Body:', request.body); + await parkingLocation.save(); + response.status(201).json({ message: '🤖: Parking was successfully added to database.' }); } catch (x) { - console.error(x); - response.status(500).json({ error: 'â›”ï¸: Failed to add parking to database, please try again later.' }); + console.error(x); + response.status(500).json({ error: 'â›”ï¸: Failed to add parking to database, please try again later.' }); } - }); +}); + + +// app.post('/location/parking-locations', isAdmin, async (request, response) => { +// try { +// const { Title, Description,city, street_address, postcode, lat, lon, spaces_available, total_spaces } = request.body; + +// const existingLocation = await ParkingLocation.findOne({ street_address }); +// if (existingLocation) { +// return response.status(400).json({ error: 'â›”ï¸: A parking location with this street address already exists. Please try another alternative location. ' }); +// } + +// const parkingLocation = new ParkingLocation({ +// Title, +// Description, +// city, +// street_address, +// postcode, +// lat, +// lon, +// spaces_available, +// total_spaces, +// }); +// console.log('🤖 Request Body:', request.body); +// await parkingLocation.save(); +// response.status(201).json({ message: '🤖: Parking was successfully added to database.' }); +// } catch (x) { +// console.error(x); +// response.status(500).json({ error: 'â›”ï¸: Failed to add parking to database, please try again later.' }); +// } +// }); app.delete('/location/parking-locations/:id', isAdmin ,async (request, response) => {