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

transfer of files

parent 215d92cd
No related branches found
No related tags found
No related merge requests found
#dependencies
/node_modules
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
.env
This diff is collapsed.
...@@ -7,5 +7,9 @@ ...@@ -7,5 +7,9 @@
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"author": "", "author": "",
"license": "ISC" "license": "ISC",
"dependencies": {
"express": "^4.18.2",
"mongoose": "^7.0.4"
}
} }
const { MongoClient } = require('mongodb');
async function main(){
const uri = "mongodb://localhost:27017";
const client = new MongoClient(uri);
try {
// Connect to the MongoDB cluster
await client.connect();
// Access the "ParkingLocations" database
const db = client.db("ParkingLocations");
// Find all documents in the "ParkingLocations" collection and print their properties
const documents = await db.collection("ParkingLocations").find().toArray();
documents.forEach((doc) => {
console.log(`ID: ${doc.ID}`);
console.log(`Title: ${JSON.stringify(doc.Title)}`);
console.log(`Description: ${JSON.stringify(doc.Description)}`);
console.log(`lat: ${doc.lat}`);
console.log(`lat: ${doc.lon}`);
console.log(`Address: ${JSON.stringify(doc.street_address)}`);
console.log(`City: ${JSON.stringify(doc.spaces_available)}`);
console.log(`Spaces Available: ${JSON.stringify(doc.spaces_available)}`);
console.log(`--------------------------------------------`);
});
} catch (e) {
console.error(e);
} finally {
await client.close();
}
}
main().catch(console.error);
const express = require('express');
const mongoose = require('mongoose');
const {MONGO_URI, API_KEY } = APIKeyAccess();
require('dotenv').config();
const app = express();
const port = 3000;
const ParkingLocation = require('./model/ParkingLocation');
const bodyParser = require('body-parser');
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 };
}
app.use(bodyParser.json());
mongoose.connect(MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(() => {
console.log('🤖: Successfully connected to:', MONGO_URI);
}).catch((x) => {
console.error(x);
console.log('⛔️ Database Error: An error has occured, please try again later')
});
app.get('/parking-locations', async (request, response) => {
try {
const parkingLocations = await ParkingLocation.find({});
console.log(`🤖: Successfully retrieved all ${parkingLocations.length} parking locations:`);
response.send(parkingLocations);
} catch (x) {
console.error(x);
response.status(500).send('⛔️ Server Error: An error has occured, please try again later');
}
});
app.post('/parking-locations', async (request, response) => {
try {
const { ID, Title, Description, lat, lon, city,postcode,total_spaces, spaces_available, street_address } = request.body;
const parkingLocation = new ParkingLocation({
ID,
Title,
Description,
lat,
lon,
city,
postcode,
total_spaces,
spaces_available,
street_address
});
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.listen(port, () => {
console.log(`🤖: Now tuning into http://localhost:${port}`);
});
const mongoose = require('mongoose');
const express = require('express');
const router = express.Router();
const app = express();
const port = 3000;
const ParkingLocationsSchema = new mongoose.Schema({
ID: Number,
Title: String,
Description: String,
lat: {
type: Number, // Define the type as Number
min: -90,
max: 90
},
lon: {
type: Number, // Define the type as Number
min: -180,
max: 180
},
city: String,
postcode: String,
spaces_available: Number,
street_address: String
});
const ParkingLocation = mongoose.model('ParkingLocations', ParkingLocationsSchema);
// app.get('/parking-locations', async (request, response) => {
// try {
// const parkingLocations = await ParkingLocation.find({});
// console.log(`🤖: Successfully retrieved all ${parkingLocations.length} parking locations:`);
// response.send(parkingLocations);
// } catch (x) {
// console.error(x);
// response.status(500).send('⛔️ Server Error: An error has occured, please try again later');
// }
// });
// // POST a new parking location
// router.post('/', async (req, res) => {
// try {
// const { title, description, lat, lon, spaces_available } = req.body;
// const newParkingLocation = new ParkingLocation({
// title,
// description,
// lat,
// lon,
// spaces_available,
// });
// await newParkingLocation.save();
// res.json(newParkingLocation);
// } catch (x) {
// console.error(x);
// res.status(500).send('⛔️ Server Error: An error has occured, please try again later');
// }
// });
module.exports = ParkingLocation;
const {mongoose} = require('mongoose');
mongoose.connect('mongodb://localhost:27017/ParkingLocations', { useNewUrlParser: true });
const parkingLocationSchema = new mongoose.Schema({
ID: Number,
Title: String,
Description: String,
lat: Number,
lon: Number
});
const ParkingLocation = mongoose.model('ParkingLocations', parkingLocationSchema);
const db = mongoose.connection;
db.once('open', async () => {
console.log('Connection to database successful!');
try {
const parkingLocations = await ParkingLocation.find({});
console.log(`Retrieved ${parkingLocations.length} parking locations:`);
parkingLocations.forEach(parkingLocation => {
console.log(`ID: ${parkingLocation.ID}`);
console.log(`Title: ${JSON.stringify(parkingLocation.Title)}`);
console.log(`Description: ${JSON.stringify(parkingLocation.Description)}`);
console.log(`lat: ${parkingLocation.lat}`);
console.log(`lon: ${parkingLocation.lon}`);
console.log(`--------------------------------------------`);
});
} catch (err) {
console.error(err);
} finally {
mongoose.connection.close();
console.log('Connection to database closed.');
}
});
db.on('error', (err) => {
console.error('Connection error:', err);
});
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