diff options
Diffstat (limited to 'web')
| -rw-r--r-- | web/app.js | 24 | ||||
| -rw-r--r-- | web/models/mockpoints.js | 32 | ||||
| -rw-r--r-- | web/models/points.js | 32 | ||||
| -rw-r--r-- | web/public/javascripts/map.js | 16 | ||||
| -rw-r--r-- | web/routes/index.js | 72 | ||||
| -rw-r--r-- | web/views/map.pug | 16 |
6 files changed, 135 insertions, 57 deletions
@@ -3,6 +3,7 @@ var express = require('express'); var path = require('path'); var cookieParser = require('cookie-parser'); var logger = require('morgan'); +var mongoose = require('mongoose'); var indexRouter = require('./routes/index'); var usersRouter = require('./routes/users'); @@ -19,6 +20,29 @@ app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); +var env = process.env.NODE_ENV || 'development'; +var uri; +if (env === 'development') { + uri = 'mongodb://localhost:27017/helpthehome' + // use for small testing only if really necessary + // uri = 'mongodb+srv://development:dreamteam@cluster0-krnr4.mongodb.net/helpthehome?retryWrites=true' +} else if (env === 'qa') { + uri = process.env.MONGODB_URI +} else if (env === 'production') { + uri = '' +} + +// Mongoose connection to MongoDB +mongoose.connect(uri, { useNewUrlParser: true }, function (error) { + if (error) { + console.log(error); + console.log("App was not able to connect to the mongo server!"); + console.log("... double check that the mongo server is running locally"); + } else { + console.log(`connected to ${uri}`); + } +}); + app.use('/', indexRouter); app.use('/users', usersRouter); diff --git a/web/models/mockpoints.js b/web/models/mockpoints.js new file mode 100644 index 0000000..ff5e771 --- /dev/null +++ b/web/models/mockpoints.js @@ -0,0 +1,32 @@ +var mongoose = require('mongoose'); + +// Mongoose Schema definition +var Schema = mongoose.Schema; +var dumbJsonSchema = new Schema({ + type: String, + coordinates: Array, + ageRange: String, + clothingDescription: String, + isInjured: Boolean, + reasonForHelp: String +}); + +// Mongoose Model definition +var dumbJson = mongoose.model('dumbJstring', dumbJsonSchema, 'dumbpointscollection'); + +// this function gets ALL the points from the db +// returns the entire document without modifications +exports.get_points = function(callback) { + dumbJson.find({}).exec(function(err, docs) { + if (err) callback(err, null); + callback(null, docs); + }); +} + +// this function stores A user report into the db +exports.save_request = function(data, callback) { + dumbJson.create(data, function(err, result) { + if (err) callback(err, null); + callback(null, result); + }); +} diff --git a/web/models/points.js b/web/models/points.js new file mode 100644 index 0000000..3a07c82 --- /dev/null +++ b/web/models/points.js @@ -0,0 +1,32 @@ +var mongoose = require('mongoose'); + +// Mongoose Schema definition +var Schema = mongoose.Schema; +var JsonSchema = new Schema({ + type: String, + coordinates: Array, + ageRange: String, + clothingDescription: String, + isInjured: Boolean, + reasonForHelp: String +}); + +// Mongoose Model definition +var Json = mongoose.model('Jstring', JsonSchema, 'pointscollection'); + +// this function gets ALL the points from the db +// returns the entire document without modifications +exports.get_points = function(callback) { + Json.find({}).exec(function(err, docs) { + if (err) callback(err, null); + callback(null, docs); + }); +} + +// this function stores A user report into the db +exports.save_request = function(data, callback) { + Json.create(data, function(err, result) { + if (err) callback(err, null); + callback(null, result); + }); +} diff --git a/web/public/javascripts/map.js b/web/public/javascripts/map.js index 5e5a303..190307d 100644 --- a/web/public/javascripts/map.js +++ b/web/public/javascripts/map.js @@ -12,10 +12,12 @@ L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}{r} ext: 'png' }).addTo(map); -// Create point feature for somewhere downtown -var myDataPoint = L.marker([43.6529, -79.3849]).addTo(map); - -// Bind popup to Data Point object -myDataPoint.bindPopup("<h3>City Hall (not really)</h3><p>Toronto, ON<br>Information about city hall.</p>"); - -map.setZoom(13); +L.geoJson(points, { + pointToLayer: function (feature, latlng) { + //return L.circleMarker(latlng); + return L.marker(latlng); + } +}).bindPopup(function (layer) { + // layer.feature.geometry gives you access to all the fields + return "<p>" + JSON.stringify(layer.feature.geometry) + "</p>"; +}).addTo(map) diff --git a/web/routes/index.js b/web/routes/index.js index df579fc..b2d0796 100644 --- a/web/routes/index.js +++ b/web/routes/index.js @@ -1,56 +1,44 @@ var express = require('express'); var router = express.Router(); - -// Mongoose import -var mongoose = require('mongoose'); - -var env = process.env.NODE_ENV || 'development'; -var uri; -if (env === 'development') { - uri = 'mongodb://localhost:27017/helpthehome' - // use for small testing only if really necessary - // uri = 'mongodb+srv://development:dreamteam@cluster0-krnr4.mongodb.net/helpthehome?retryWrites=true' -} else if (env === 'qa') { - uri = process.env.MONGODB_URI -} else if (env === 'production') { - uri = '' -} - -// Mongoose connection to MongoDB -mongoose.connect(uri, { useNewUrlParser: true }, function (error) { - if (error) { - console.log(error); - console.log("App was not able to connect to the mongo server!"); - console.log("... double check that the mongo server is running locally"); - } else { - console.log(`connected to ${uri}`); - } -}); - -// Mongoose Schema definition -var Schema = mongoose.Schema; -var JsonSchema = new Schema({ - name: String, - type: Schema.Types.Mixed, - coordinates: Array -}); - -// Mongoose Model definition -var Json = mongoose.model('Jstring', JsonSchema, 'pointscollection'); +var Mockpoints = require('../models/mockpoints'); +var Points = require('../models/points'); /* GET home page. */ router.get('/', function(req, res, next) { // for now just redirect to /map - //res.render('index', { title: 'Welcome' }); res.redirect('/map'); }); /* GET Map page. */ router.get('/map', function(req,res) { - // send the view lat, and long. city center - res.render('map', { - lat : 43.654127, - lng : -79.383370 + // load the map with all the points + // Mockpoints.get_points(function(err, points){ + Points.get_points(function(err, points){ + res.render('map', { + lat : 43.665234, + lng : -79.383370, + points: points + }); + }); +}); + +router.post('/mobilerequest', function(req, res) { + var data = { + 'coordinates': req.body.coordinates, + 'type': req.body.type, + 'isInjured': req.body.isInjured, + 'reasonForHelp': req.body.reasonForHelp, + 'ageRange': req.body.ageRange, + 'clothingDescription': req.body.clothingDescription + } + Points.save_request(data, function(err, result) { + if (err) { + console.log(`err inserting mobile request into db: ${err}`); + res.status(500).send({ error: "boo:(" }); + } else { + console.log(result); + res.status(200).send({'status': 'success', 'data': data}); + } }); }); diff --git a/web/views/map.pug b/web/views/map.pug index b7781fa..725c8a8 100644 --- a/web/views/map.pug +++ b/web/views/map.pug @@ -1,11 +1,11 @@ extends layout.pug block content - #sidebar - h1 This is a sidebar - #map - - script(type='text/javascript'). - var map = L.map('map').setView([#{lat},#{lng}], 15); - script(src='/javascripts/map.js') - + #sidebar + h1 This is a sidebar + #map + script(type='text/javascript'). + var map = L.map('map').setView([#{lat},#{lng}], 13); + var points = !{JSON.stringify(points)}; + console.log(points); + script(src='/javascripts/map.js') |
