From 00cf4a286cceca2ab5a1febc3451c3ecebf029c2 Mon Sep 17 00:00:00 2001 From: Kumar Damani Date: Wed, 13 Feb 2019 23:04:41 -0500 Subject: added a simple readme --- README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..acdfd5a --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# SOME README -- cgit v1.2.3 From 5189458c68f326ba01d803ac515ab222c211cc8f Mon Sep 17 00:00:00 2001 From: ivanshen Date: Mon, 25 Feb 2019 22:08:08 -0500 Subject: feat-19: add route to insert mobile request to database --- web/app.js | 24 ++++++++++++++++++++++++ web/routes/index.js | 52 ++++++++++++++++++++++++++-------------------------- 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/web/app.js b/web/app.js index ab7aed4..488304d 100644 --- a/web/app.js +++ b/web/app.js @@ -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/routes/index.js b/web/routes/index.js index ffe1c21..6c87660 100644 --- a/web/routes/index.js +++ b/web/routes/index.js @@ -1,38 +1,18 @@ 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 + coordinates: Array, + ageRange: String, + clothingDescription: String, + isInjured: Boolean, + reasonForHelp: String }); // Mongoose Model definition @@ -58,4 +38,24 @@ router.get('/map', function(req,res) { }); }); +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 + } + Json.create(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}); + } + }); +}); + module.exports = router; -- cgit v1.2.3 From 0a61d18ba0088b378fcfab6498b3a1c42f9c029f Mon Sep 17 00:00:00 2001 From: Kumar Damani Date: Thu, 28 Feb 2019 00:40:15 -0700 Subject: sending geo data to front end, displaying it as markers, and taken out schema defs from index into models folder --- web/app.js | 24 ++++++++++++++++++++ web/models/mockpoints.js | 16 ++++++++++++++ web/models/points.js | 11 ++++++++++ web/public/javascripts/map.js | 15 +++++++------ web/routes/index.js | 51 ++++++++----------------------------------- web/views/map.pug | 16 +++++++------- 6 files changed, 76 insertions(+), 57 deletions(-) create mode 100644 web/models/mockpoints.js create mode 100644 web/models/points.js diff --git a/web/app.js b/web/app.js index ab7aed4..f0ae3a5 100644 --- a/web/app.js +++ b/web/app.js @@ -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..608c0bd --- /dev/null +++ b/web/models/mockpoints.js @@ -0,0 +1,16 @@ +var mongoose = require('mongoose'); + +var Schema = mongoose.Schema; +var dumbJsonSchema = new Schema({ + type: String, + coordinates: Array +}); + +// Mongoose Model definition +var dumbJson = mongoose.model('dumbJstring', dumbJsonSchema, 'dumbpointscollection'); + +exports.get_dumb_points = function(callback) { + dumbJson.find({}).exec(function(err, docs){ + callback(err, docs) + }); +} diff --git a/web/models/points.js b/web/models/points.js new file mode 100644 index 0000000..18ffe2e --- /dev/null +++ b/web/models/points.js @@ -0,0 +1,11 @@ +var mongoose = require('mongoose'); + +// Mongoose Schema definition +var Schema = mongoose.Schema; +var JsonSchema = new Schema({ + type: String, + coordinates: Array +}); + +// Mongoose Model definition +var Json = mongoose.model('Jstring', JsonSchema, 'pointscollection'); diff --git a/web/public/javascripts/map.js b/web/public/javascripts/map.js index 5e5a303..70c1419 100644 --- a/web/public/javascripts/map.js +++ b/web/public/javascripts/map.js @@ -12,10 +12,11 @@ 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("

City Hall (not really)

Toronto, ON
Information about city hall.

"); - -map.setZoom(13); +L.geoJson(dumbpoints, { + pointToLayer: function (feature, latlng) { + //return L.circleMarker(latlng); + return L.marker(latlng); + } +}).bindPopup(function (layer) { + return layer.feature.geometry.another; +}).addTo(map) diff --git a/web/routes/index.js b/web/routes/index.js index df579fc..54c53be 100644 --- a/web/routes/index.js +++ b/web/routes/index.js @@ -1,56 +1,23 @@ 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 mockdata + Mockpoints.get_dumb_points(function(err, points){ + res.render('map', { + lat : 43.665234, + lng : -79.383370, + points: points + }); }); }); diff --git a/web/views/map.pug b/web/views/map.pug index b7781fa..fcc3abc 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 dumbpoints = !{JSON.stringify(points)}; + //console.log(dumbpoints); + script(src='/javascripts/map.js') -- cgit v1.2.3 From 9eae2a3a228e9fb65970c1e61d9e4f4429def7a1 Mon Sep 17 00:00:00 2001 From: Kumar Damani Date: Thu, 28 Feb 2019 10:09:03 -0700 Subject: changed index to use pointscollection by default --- web/models/mockpoints.js | 24 ++++++++++++++++++++---- web/models/points.js | 23 ++++++++++++++++++++++- web/routes/index.js | 25 +++++++++++++++++++++++-- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/web/models/mockpoints.js b/web/models/mockpoints.js index 608c0bd..ff5e771 100644 --- a/web/models/mockpoints.js +++ b/web/models/mockpoints.js @@ -1,16 +1,32 @@ var mongoose = require('mongoose'); +// Mongoose Schema definition var Schema = mongoose.Schema; var dumbJsonSchema = new Schema({ type: String, - coordinates: Array + coordinates: Array, + ageRange: String, + clothingDescription: String, + isInjured: Boolean, + reasonForHelp: String }); // Mongoose Model definition var dumbJson = mongoose.model('dumbJstring', dumbJsonSchema, 'dumbpointscollection'); -exports.get_dumb_points = function(callback) { - dumbJson.find({}).exec(function(err, docs){ - callback(err, docs) +// 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 index 18ffe2e..3a07c82 100644 --- a/web/models/points.js +++ b/web/models/points.js @@ -4,8 +4,29 @@ var mongoose = require('mongoose'); var Schema = mongoose.Schema; var JsonSchema = new Schema({ type: String, - coordinates: Array + 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/routes/index.js b/web/routes/index.js index 54c53be..b2d0796 100644 --- a/web/routes/index.js +++ b/web/routes/index.js @@ -11,8 +11,9 @@ router.get('/', function(req, res, next) { /* GET Map page. */ router.get('/map', function(req,res) { - // load the map with mockdata - Mockpoints.get_dumb_points(function(err, points){ + // 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, @@ -21,4 +22,24 @@ router.get('/map', function(req,res) { }); }); +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}); + } + }); +}); + module.exports = router; -- cgit v1.2.3 From 9764fba35cdac7e226dad65f061c09fd3d9909cc Mon Sep 17 00:00:00 2001 From: Kumar Damani Date: Thu, 28 Feb 2019 10:32:26 -0700 Subject: displaying all the fields in the marker popup, better var naming --- web/public/javascripts/map.js | 5 +++-- web/views/map.pug | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/web/public/javascripts/map.js b/web/public/javascripts/map.js index 70c1419..190307d 100644 --- a/web/public/javascripts/map.js +++ b/web/public/javascripts/map.js @@ -12,11 +12,12 @@ L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}{r} ext: 'png' }).addTo(map); -L.geoJson(dumbpoints, { +L.geoJson(points, { pointToLayer: function (feature, latlng) { //return L.circleMarker(latlng); return L.marker(latlng); } }).bindPopup(function (layer) { - return layer.feature.geometry.another; + // layer.feature.geometry gives you access to all the fields + return "

" + JSON.stringify(layer.feature.geometry) + "

"; }).addTo(map) diff --git a/web/views/map.pug b/web/views/map.pug index fcc3abc..725c8a8 100644 --- a/web/views/map.pug +++ b/web/views/map.pug @@ -6,6 +6,6 @@ block content script(type='text/javascript'). var map = L.map('map').setView([#{lat},#{lng}], 13); - var dumbpoints = !{JSON.stringify(points)}; - //console.log(dumbpoints); + var points = !{JSON.stringify(points)}; + console.log(points); script(src='/javascripts/map.js') -- cgit v1.2.3