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

Content transferred from Mongo Compass to Atlas, with testing completed.

parent a9596dab
No related branches found
No related tags found
No related merge requests found
REACT_APP_GOOGLE_MAPS_API_KEY = "AIzaSyBjY11PT3iGwoczzs6zNw7ot0rsWCVWBDU"
REACT_APP_MONGO_URI = "mongodb://localhost:27017/ParkingLocations"
\ No newline at end of file
REACT_APP_MONGO_URI_LOCAL = "mongodb://localhost:27017/ParkingLocations"
USERNAME= "user"
PASSWORD= "WsfePqVVojP4lgZk"
\ No newline at end of file
const express = require('express');
const mongoose = require('mongoose');
const { MONGO_URI, API_KEY } = APIKeyAccess();
const { MONGO_URI_LOCAL, API_KEY} = APIKeyAccess();
require('dotenv').config();
const app = express();
const port = 3000;
......@@ -9,15 +9,21 @@ const bodyParser = require('body-parser');
const jwt = require('jsonwebtoken');
const geolib = require('geolib');
const axios = require('axios');
const { MongoClient, ServerApiVersion } = require('mongodb');
const username = process.env.USERNAME;
const password = process.env.PASSWORD;
function APIKeyAccess() {
const API_KEY = process.env.REACT_APP_GOOGLE_MAPS_API_KEY;
const MONGO_URI = "mongodb://localhost:27017/ParkingLocations";
return { MONGO_URI, API_KEY };
//const MONGO_URI_LOCAL = process.env.MONGO_URI_LOCAL;
// const MONGO_URI = "mongodb://localhost:27017/ParkingLocations";
return { API_KEY };
}
app.use(bodyParser.json());
const MONGO_URI = `mongodb+srv://${username}:${password}@locationservice.dqljyov.mongodb.net/ParkingLocations?retryWrites=true&w=majority`;
mongoose.connect(MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
......@@ -86,28 +92,35 @@ function isAdmin(request, response, next) {
}
app.post('/parking-locations', isAdmin, async (request, response) => {
try {
const { ID, Title, Description, lat, lon, city, postcode, spaces_available, total_spaces, street_address } = request.body;
const parkingLocation = new ParkingLocation({
ID,
Title,
Description,
lat,
lon,
city,
postcode,
total_spaces,
spaces_available,
street_address
});
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.' });
}
});
try {
const { Title, Description,city, street_address, postcode, lat, lon, spaces_available, total_spaces } = request.body;
// Check if street_address already exists in the database
const existingLocation = await ParkingLocation.findOne({ street_address });
if (existingLocation) {
return response.status(400).json({ error: '⛔️: A parking location with this street address already exists.' });
}
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.' });
}
});
......@@ -126,8 +139,6 @@ app.delete('/parking-locations/:id', isAdmin, async (request, response) => {
}
});
app.listen(port, () => {
console.log(`🤖: Now tuning into http://localhost:${port}`);
});
......@@ -5,9 +5,11 @@ const app = express();
const port = 3000;
const ParkingLocationsSchema = new mongoose.Schema({
ID: Number,
Title: String,
Description: String,
city: String,
street_address: String,
postcode: String,
lat: {
type: Number, // Define the type as Number
min: -90,
......@@ -18,11 +20,8 @@ const ParkingLocationsSchema = new mongoose.Schema({
min: -180,
max: 180
},
city: String,
postcode: String,
total_spaces: Number,
spaces_available: Number,
street_address: String
total_spaces: Number
});
......
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