aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Shen <iwshen11@gmail.com>2019-02-27 23:02:12 +0000
committerGitHub <noreply@github.com>2019-02-27 23:02:12 +0000
commit76214c83dc52d4dca6e5043fe374da57a54f9738 (patch)
tree6fc8d16a82313275b654ed7ed931e660e7f6d7d2
parent8629a763427cac924a80ba3a3f846b67fe2b1038 (diff)
parent73d4c7c7f01f2d730668b926deb57921b8dfdc54 (diff)
Merge pull request #14 from csc301-winter-2019/feat/19
feat/19: add route to insert mobile request to database
-rw-r--r--web/app.js24
-rw-r--r--web/routes/index.js52
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 df579fc..32a5890 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
@@ -54,4 +34,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;