From 92f6b5cc0a99f3f5ebf06ad8bcfed28295e2bb6f Mon Sep 17 00:00:00 2001 From: "Dookarun, Jason J (PG/T - Computer Science)" <jd00795@surrey.ac.uk> Date: Fri, 21 Apr 2023 15:06:11 +0100 Subject: [PATCH] Content transferred from Mongo Compass to Atlas, with testing completed. --- .env | 4 ++- src/App.js | 65 +++++++++++++++++++++--------------- src/model/ParkingLocation.js | 9 +++-- 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/.env b/.env index adfc2fd..76d294d 100644 --- a/.env +++ b/.env @@ -1,2 +1,4 @@ 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 diff --git a/src/App.js b/src/App.js index 6ccbc70..7593cd0 100644 --- a/src/App.js +++ b/src/App.js @@ -1,6 +1,6 @@ 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}`); }); diff --git a/src/model/ParkingLocation.js b/src/model/ParkingLocation.js index 48cf440..600fc0c 100644 --- a/src/model/ParkingLocation.js +++ b/src/model/ParkingLocation.js @@ -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 }); -- GitLab