Commit 4d5bb03e authored by Corentin Mors's avatar Corentin Mors
Browse files

Merge branch 'develop'

parents 47f6036a 081b0f92
# ExpressJS Starter Kit
This project suggest a MVC (model - view - controller) architecture for an ExpressJS project.
It uses MongoDB to store data as well as Nunjucks for templating.
# X-Cockpit
> A cockpit for your next missions
## Project structure
......@@ -13,6 +11,8 @@ It uses MongoDB to store data as well as Nunjucks for templating.
|-- models
|-- db.js (to connect to database)
|-- users.js (define users contenttype)
|-- routes
|-- index.js (specify routes for the app)
|-- controllers
|-- index.js (for homepage)
|-- users.js (for users contenttype)
......@@ -40,7 +40,7 @@ Go into root folder and enter the following line in your terminal :
Install gulp (and associated components) :
`npm install --global gulp-cli gulp-rename gulp-clean-css gulp-minify`
`npm install --global gulp gulp-cli gulp-rename gulp-clean-css gulp-minify`
## Launch project
......@@ -58,8 +58,7 @@ Your project is accessible at [localhost:3000](http://localhost:3000).
## About design
The administrative panel template is from [Ad.min](https://github.com/Mikescops/ad.min) which is a minimal admin template.
<to be completed>
## Various documentation
......@@ -69,16 +68,3 @@ The administrative panel template is from [Ad.min](https://github.com/Mikescops/
- GulpJS (development automation) : https://gulpjs.com/
- FontAwesome (icons font) : https://fontawesome.com/icons
- Kacole2's skeleton (inspiration of this project) : https://git.io/fxQXK
## Contributing
Feel free to contribute to this project, fork and pull request your ideas.
Don't include work that is not open source or not from you.
## Authors
| [![twitter/mikescops](https://avatars0.githubusercontent.com/u/4266283?s=100&v=4)](http://twitter.com/mikescops "Follow @mikescops on Twitter") |
|---|
| [Corentin Mors](https://pixelswap.fr/) |
\ No newline at end of file
......@@ -5,9 +5,10 @@ var express = require('express'),
favicon = require('serve-favicon'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser');
bodyParser = require('body-parser'),
session = require('express-session');
const MongoStore = require('connect-mongo')(session);
app.use(logger('dev'));
app.use(bodyParser.json());
......@@ -19,7 +20,20 @@ app.use(cookieParser());
// Load DB
var db = require('./models/db');
//use sessions for tracking logins
app.use(session({
secret: 'monsupersecrettropbien',
resave: true,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: db.connection })
}));
var personnages = require('./models/users');
var resources = require('./models/resources');
var listes = require('./models/checklists');
var source = require('./models/sources');
var datacenter = require('./models/datacenters');
// Load templating and statics
app.use(express.static(path.join(__dirname, 'public')));
......@@ -34,8 +48,8 @@ app.set('view engine', 'html');
// Call controllers
app.use(require('./controllers'))
// Call routes
app.use(require('./routes'))
// catch 404 and forward to error handler
......@@ -79,4 +93,4 @@ app.use(function(err, req, res, next) {
});
module.exports = app;
\ No newline at end of file
module.exports = app;
var express = require('express'),
router = express.Router(),
mongoose = require('mongoose');
//GET all checklists
exports.list_checklists = function(req, res, next) {
//retrieve all checklists from Monogo
mongoose.model('Checklist').find({}, function (err, checklists) {
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('./checklists/list', {
title: 'Checklist list',
checklists: checklists
});
},
//JSON response will show all users in JSON format
json: function(){
res.json(checklists);
}
});
}
});
}
//POST a new resource
exports.add_checklist = 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 title = req.body.title;
var description = req.body.description;
//call the create function for our database
mongoose.model('Checklist').create({
_id : new mongoose.Types.ObjectId(),
title : title,
description : description
}, function (err, checklist) {
if (err) {
res.send("There was a problem adding the information to the database.");
} else {
//resource has been created
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("checklists");
// And forward to success page
res.redirect("/checklists");
},
//JSON response will show the newly created user
json: function(){
res.json(checklist);
}
});
}
})
};
exports.view_checklist = function(req, res) {
mongoose.model('Checklist').findById(req.params.id, function (err, checklist) {
if (err) {
console.log('GET Error: There was a problem retrieving: ' + err);
} else {
console.log('GET Retrieving ID: ' + checklist._id);
res.format({
html: function(){
res.render('checklists/view', {
title: 'View of ' + checklist.title,
checklist : checklist
});
},
json: function(){
res.json(checklist);
}
});
}
});
};
//GET the individual user by Mongo ID
exports.edit_checklist = function(req, res) {
//search for the user within Mongo
mongoose.model('Checklist').findById(req.params.id, function (err, checklist) {
if (err) {
console.log('GET Error: There was a problem retrieving: ' + err);
} else {
//Return the user
console.log('GET Retrieving ID: ' + checklist._id);
res.format({
//HTML response will render the 'edit.jade' template
html: function(){
res.render('checklists/edit', {
title: 'Edit checklist #' + checklist._id,
checklist : checklist
});
},
//JSON response will return the JSON output
json: function(){
res.json(checklist);
}
});
}
});
};
//POST to update a user by ID
exports.post_edit_checklist = function(req, res) {
// Get our REST and form values.
var id = req.params.id;
var title = req.body.title;
var description = req.body.description;
console.log(req.body);
//find the document by ID
mongoose.model('Description').findByIdAndUpdate(id, {
title : title,
description : description
}, function (err, description) {
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("/descriptions/" + description._id);
},
//JSON responds showing the updated values
json: function(){
res.json(description);
}
});
}
})
};
//DELETE a Users by ID
exports.delete_checklist = function (req, res){
//find user by ID
mongoose.model('Checklist').findById(req.params.id, function (err, checklist) {
if (err) {
return console.error(err);
} else {
//remove it from Mongo
checklist.remove(function (err, checklist) {
if (err) {
return console.error(err);
} else {
//Returning success messages saying it was deleted
console.log('DELETE removing ID: ' + checklist._id);
res.format({
//HTML returns us back to the main page, or you can create a success page
html: function(){
res.redirect("/checklists");
},
//JSON returns the item with the message that is has been deleted
json: function(){
res.json({message : 'deleted',
item : checklist
});
}
});
}
});
}
});
};
\ No newline at end of file
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
});
}
});
}
});
}
});
};
exports.guides_index = function(req, res, next) {
res.render('guides/index');
}
exports.faq = function(req, res, next) {
res.render('guides/faq');
}
\ No newline at end of file
var express = require('express'),
router = express.Router(),
var users = require('../models/users'),
mongoose = require('mongoose');
router.use('/users', require('./users'))
router.get('/', function(req, res) {
exports.welcome = function(req, res) {
mongoose.model('Users').find({}, function (err, users) {
if (err) {
return console.error(err);
......@@ -23,7 +19,52 @@ router.get('/', function(req, res) {
});
}
});
})
}
// Login and logout functions
exports.getLogin = function(req, res, next) {
res.render('login', {error: next});
}
exports.postLogin = function(req, res){
users.authenticate(req.body.email, req.body.password, function (error, user) {
if (error || !user) {
var err = new Error('Wrong email or password.');
err.status = 401;
return next(err);
} else {
mongoose.model('Users').findByIdAndUpdate(user._id, {
acc_lastloggin_date : Date.now(),
}, function (err, user) {
if (err) {
res.send("There was a problem updating the information to the database: " + err);
}
else {
req.session.userId = user._id;
return res.redirect('/');
}
});
}
});
}
exports.logout = function(req, res, next) {
if (req.session) {
// delete session object
req.session.destroy(function(err) {
if(err) {
return next(err);
} else {
return res.redirect('/');
}
});
}
}
exports.location = function(req, res, next) {
res.render('map');
}
module.exports = router
\ No newline at end of file
exports.dashboard = function(req, res, next) {
res.render('dashboard');
}
var express = require('express'),
router = express.Router(),
mongoose = require('mongoose');
//GET all resources
exports.list_resources = function(req, res, next) {
//retrieve all users from Monogo
mongoose.model('Resource').find({}, function (err, resources) {
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('./resources/list', {
title: 'Resources list',
resources: resources
});
},
//JSON response will show all users in JSON format
json: function(){
res.json(resources);
}
});
}
});
}
//POST a new resource
exports.add_resource = 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 type = req.body.type;
var quantity = req.body.quantity;
var unit = req.body.unit;
//call the create function for our database
mongoose.model('Resource').create({
_id : new mongoose.Types.ObjectId(),
type : type,
quantity : quantity,
unit : unit
}, function (err, resource) {
if (err) {
res.send("There was a problem adding the information to the database.");
} else {
//resource has been created
console.log('POST creating new resource: ' + resource);
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("resources");
// And forward to success page
res.redirect("/resources");
},
//JSON response will show the newly created user
json: function(){
res.json(resource);
}
});
}
})
};
exports.view_resource = function(req, res) {
mongoose.model('Resource').findById(req.params.id, function (err, resource) {
if (err) {