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