Created
February 1, 2016 01:12
-
-
Save FredLoh/e39c9625b9bf8fe312bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
var uniqueValidator = require('mongoose-unique-validator'); | |
var Schema = mongoose.Schema; | |
var LocationSchema = new mongoose.Schema({ | |
name: { | |
type: String, | |
required: true, | |
unique: true | |
}, | |
location: { | |
type: [Number], | |
required: true, | |
index: '2d', | |
}, | |
update_date: Date, | |
rentas: [{ | |
type: Schema.Types.ObjectId, | |
ref: 'renta' | |
}] | |
}, { | |
versionKey: false | |
}); | |
var RentaSchema = new mongoose.Schema({ | |
cancha_id: { | |
type: String, | |
required: true | |
}, | |
update_date: Date, | |
duration: Number, | |
}, { | |
versionKey: false | |
}); | |
LocationSchema.plugin(uniqueValidator); | |
mongoose.model('cancha', LocationSchema); | |
LocationSchema.plugin(uniqueValidator); | |
mongoose.model('renta', RentaSchema); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'), | |
router = express.Router(), | |
mongoose = require('mongoose'), //mongo connection | |
bodyParser = require('body-parser'), //parses information from POST | |
methodOverride = require('method-override'); //used to manipulate POST | |
router.use(bodyParser.urlencoded({ | |
extended: true | |
})); | |
router.use(methodOverride(function(req, res) { | |
if (req.body && typeof req.body === 'object' && '_method' in req.body) { | |
// look in urlencoded POST bodies and delete it | |
var method = req.body._method; | |
delete req.body._method; | |
return method; | |
} | |
})); | |
router.route('/') | |
//GET all canchas | |
.get(function(req, res, next) { | |
//retrieve all canchas from Monogo | |
mongoose.model('renta').find({}, function(err, rentas) { | |
if (err) { | |
return console.error(err); | |
} else { | |
//respond to both HTML and JSON. JSON responses require 'Accept: application/json;' in the Request Header | |
res.format({ | |
//HTML response will render the index.jade file in the views/canchas folder. We are also setting "canchas" to be an accessible variable in our jade view | |
// html: function(){ | |
// res.render('canchas/index', { | |
// title: 'Canchas List', | |
// "canchas" : canchas | |
// }); | |
// }, | |
//JSON response will show all blobs in JSON format | |
JSON: function() { | |
console.log("CANCHAS"); | |
res.json(rentas); | |
} | |
}); | |
} | |
}); | |
}) | |
//POST a new blob | |
.post(function(req, res) { | |
console.log("POSTing new Renta"); | |
// Get values from POST request. These can be done through forms or REST calls. These rely on the "name" attributes for forms | |
var cancha_id = req.body.cancha_id; | |
var datetime = Date.now(); | |
var duration = req.body.duration; | |
console.log("CANCHAID"); | |
console.log(cancha_id); | |
//call the create function for our database | |
mongoose.model('renta').create({ | |
cancha_id: cancha_id, | |
updated_date: datetime, | |
duration: duration | |
}, function(err, renta) { | |
if (err) { | |
console.log(err); | |
res.send("There was a problem adding the information to the database."); | |
} else { | |
//Cancha has been created | |
console.log('POST creating new renta: ' + renta); | |
mongoose.model('cancha').findByIdAndUpdate(cancha_id, function(err, cancha) { | |
if (err) { | |
console.log(err); | |
res.send("There was a problem adding the information to the database."); | |
} else { | |
console.log(cancha.rentas); | |
console.log(renta.id); | |
cancha.rentas.addToSet(renta.id); | |
} | |
}); | |
res.format({ | |
//HTML response will set the location and redirect back to the home page. | |
html: function() { | |
// If it worked, set the header so the address bar doesn't still say /adduser | |
res.location("/api/canchas/"); | |
// And forward to success page | |
res.redirect("/api/canchas/"); | |
}, | |
//JSON response will show the newly created blob | |
json: function() { | |
res.json(renta); | |
} | |
}); | |
} | |
}); | |
}); | |
/* GET New cancha page. */ | |
router.get('/new', function(req, res) { | |
mongoose.model('cancha').find({}, function(err, canchas) { | |
res.render('rentas/new', { | |
title: "Create new Cancha", | |
canchas: canchas | |
}); | |
}); | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment