Skip to content

Instantly share code, notes, and snippets.

@FredLoh
Created January 31, 2016 23:47
Show Gist options
  • Save FredLoh/e9acaa5e6419f0ce3438 to your computer and use it in GitHub Desktop.
Save FredLoh/e9acaa5e6419f0ce3438 to your computer and use it in GitHub Desktop.
extends ../layout
block content
h1.
#{title}
form#formAddRenta(name="addrenta",method="post",action="/api/rentas")
p Cancha:
select#cancha_id(name="cancha_id")
for item in canchas
option(value='#{item.cancha_id}', selected=item.cancha_id name="cancha_id") #{item.name}
p Duration:
input#inputLatitude(type="number", name="duration")
p
button#btnSubmit(type="submit") submit
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 cancha: ' + renta);
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/rentas");
// And forward to success page
res.redirect("/api/rentas");
},
//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