diff --git a/.env b/.env index adfc2fd196017d0d3c887406dbd73717d0fbcb07..76d294d8280e3f2449937760e89629696c9872a9 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 6ccbc70d5e95039665707d872841772ff1143a09..7593cd0d1fd6da689461756571cee1dc66749cac 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 48cf440770e20bad2e370a7d11e7b02fc5bb802d..600fc0c831f27daf8f7149c029c31bafdb0e8e56 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 });