Commit ff6c70aa authored by Porchet Gaetan's avatar Porchet Gaetan
Browse files

Adds datacenter route and controller

parent 8812fae0
var express = require('express'),
router = express.Router(),
mongoose = require('mongoose');
//GET all datacenters
exports.list_datacenters = function(req, res, next) {
//retrieve all users from Monogo
mongoose.model('Datacenter').find({}, function (err, datacenters) {
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 users/index.html
html: function(){
res.render('./datacenters/list', {
title: 'Datacenter list',
datacenters: datacenters
});
},
//JSON response will show all users in JSON format
json: function(){
res.json(datacenters);
}
});
}
});
}
//POST a new datacenter
exports.add_datacenter = function(req, res) {
// Get values from POST request. These can be done through forms or REST calls. These rely on the "name" attributes for forms
var name = req.body.name;
var lat = req.body.latitude;
var lon = req.body.longitude;
var alt = req.body.altitude;
var location = {'lat' : lat, 'lon' : lon, 'alt' : alt};
console.log(location);
//call the create function for our database
mongoose.model('Datacenter').create({
_id : new mongoose.Types.ObjectId(),
name : name,
location : location
}, function (err, datacenter) {
if (err) {
res.send("There was a problem adding the information to the database.");
} else {
//datacenter has been created
console.log('POST creating new datacenter: ' + datacenter);
res.format({
//HTML response will set the location and redirect back to the home page. You could also create a 'success' page if that's your thing
html: function(){
// If it worked, set the header so the address bar doesn't still say /adduser
res.location("datacenters");
// And forward to success page
res.redirect("/datacenters");
},
//JSON response will show the newly created user
json: function(){
res.json(datacenter);
}
});
}
})
};
exports.view_datacenter = function(req, res) {
mongoose.model('Datacenter').findById(req.params.id, function (err, datacenter) {
if (err) {
console.log('GET Error: There was a problem retrieving: ' + err);
} else {
console.log('GET Retrieving ID: ' + datacenter._id);
res.format({
html: function(){
res.render('datacenters/view', {
title: 'View of ' + datacenter.type,
datacenter : datacenter
});
},
json: function(){
res.json(datacenter);
}
});
}
});
};
//GET the individual user by Mongo ID
exports.edit_datacenter = function(req, res) {
//search for the user within Mongo
mongoose.model('Datacenter').findById(req.params.id, function (err, datacenter) {
if (err) {
console.log('GET Error: There was a problem retrieving: ' + err);
} else {
//Return the user
console.log('GET Retrieving ID: ' + datacenter._id);
res.format({
//HTML response will render the 'edit.jade' template
html: function(){
res.render('datacenters/edit', {
title: 'Edit datacenter #' + datacenter._id,
datacenter : datacenter
});
},
//JSON response will return the JSON output
json: function(){
res.json(datacenter);
}
});
}
});
};
//POST to update a user by ID
exports.post_edit_datacenter = function(req, res) {
// Get our REST and form values.
var id = req.params.id;
var name = req.body.name;
var lat = req.body.latitude;
var lon = req.body.longitude;
var alt = req.body.altitude;
var location = {'lat' : lat, 'lon' : lon, 'alt' : alt};
console.log(req.body);
//find the document by ID
mongoose.model('Datacenter').findByIdAndUpdate(id, {
name : name,
location : location
}, function (err, datacenter) {
if (err) {
res.send("There was a problem updating the information to the database: " + err);
}
else {
//HTML responds by going back to the page or you can be fancy and create a new view that shows a success page.
res.format({
html: function(){
res.redirect("/datacenters/" + datacenter._id);
},
//JSON responds showing the updated values
json: function(){
res.json(datacenter);
}
});
}
})
};
//DELETE a Users by ID
exports.delete_datacenter = function (req, res){
//find user by ID
mongoose.model('Datacenter').findById(req.params.id, function (err, datacenter) {
if (err) {
return console.error(err);
} else {
//remove it from Mongo
datacenter.remove(function (err, datacenter) {
if (err) {
return console.error(err);
} else {
//Returning success messages saying it was deleted
console.log('DELETE removing ID: ' + datacenter._id);
res.format({
//HTML returns us back to the main page, or you can create a success page
html: function(){
res.redirect("/datacenters");
},
//JSON returns the item with the message that is has been deleted
json: function(){
res.json({message : 'deleted',
item : datacenter
});
}
});
}
});
}
});
};
'use strict';
var express = require('express'),
router = express.Router();
var datacenterController = require('../controllers/datacenters');
router.route('/')
.get(datacenterController.list_datacenters)
.post(datacenterController.add_datacenter);
router.route('/:id')
.get(datacenterController.view_datacenter);
router.route('/:id/edit')
.get(datacenterController.edit_datacenter)
.post(datacenterController.post_edit_datacenter);
router.route('/:id/delete')
.get(datacenterController.delete_datacenter);
module.exports = router
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment