Skip to content
Snippets Groups Projects
Commit d2ecc1d8 authored by Dookarun, Jason J (PG/T - Comp Sci & Elec Eng)'s avatar Dookarun, Jason J (PG/T - Comp Sci & Elec Eng)
Browse files

modifications made to POST and GET to incorporate radius search by using...

modifications made to POST and GET to incorporate radius search by using postcode. Testing was successful
parent b3158ec4
No related branches found
No related tags found
No related merge requests found
......@@ -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) => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment