Commit 56a3fc3d authored by jjaouen's avatar jjaouen
Browse files

crud resource + consume

parent 7da288b0
......@@ -20,6 +20,7 @@ app.use(cookieParser());
var db = require('./models/db');
var personnages = require('./models/users');
var resources = require('./models/resources');
// Load templating and statics
app.use(express.static(path.join(__dirname, 'public')));
......
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) {
console.log('GET Error: There was a problem retrieving: ' + err);
} else {
console.log('GET Retrieving ID: ' + resource._id);
res.format({
html: function(){
res.render('resources/view', {
title: 'View of ' + resource.type,
resource : resource
});
},
json: function(){
res.json(resource);
}
});
}
});
};
//GET the individual user by Mongo ID
exports.edit_resource = function(req, res) {
//search for the user within Mongo
mongoose.model('Resource').findById(req.params.id, function (err, resource) {
if (err) {
console.log('GET Error: There was a problem retrieving: ' + err);
} else {
//Return the user
console.log('GET Retrieving ID: ' + resource._id);
res.format({
//HTML response will render the 'edit.jade' template
html: function(){
res.render('resources/edit', {
title: 'Edit resource #' + resource._id,
resource : resource
});
},
//JSON response will return the JSON output
json: function(){
res.json(resource);
}
});
}
});
};
//POST to update a user by ID
exports.post_edit_resource = function(req, res) {
// Get our REST and form values.
var id = req.params.id;
var type = req.body.type;
var quantity = req.body.quantity;
var unit = req.body.unit;
console.log(req.body);
//find the document by ID
mongoose.model('Resource').findByIdAndUpdate(id, {
type : type,
quantity : quantity,
unit : unit
}, function (err, resource) {
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("/resources/" + resource._id);
},
//JSON responds showing the updated values
json: function(){
res.json(resource);
}
});
}
})
};
//DELETE a Users by ID
exports.delete_resource = function (req, res){
//find user by ID
mongoose.model('Resource').findById(req.params.id, function (err, resource) {
if (err) {
return console.error(err);
} else {
//remove it from Mongo
resource.remove(function (err, resource) {
if (err) {
return console.error(err);
} else {
//Returning success messages saying it was deleted
console.log('DELETE removing ID: ' + resource._id);
res.format({
//HTML returns us back to the main page, or you can create a success page
html: function(){
res.redirect("/resources");
},
//JSON returns the item with the message that is has been deleted
json: function(){
res.json({message : 'deleted',
item : resource
});
}
});
}
});
}
});
};
exports.consume = function(req, res){
mongoose.model('Resource').findById(req.params.id, function(err, resource){
if (err) {
return console.error(err);
} else {
//Return the user
console.log('GET Retrieving ID: ' + resource._id);
res.format({
//HTML response will render the 'edit.jade' template
html: function(){
res.render('resources/consume', {
title: 'Consume resource ' + resource.type,
resource : resource
});
},
//JSON response will return the JSON output
json: function(){
res.json(resource);
}
});
}
})
};
exports.post_consume_resource = function(req, res) {
// Get our REST and form values.
var consume = parseInt(req.body.quantity,10);
var id_resource = req.params.id;
//find the document by ID
mongoose.model('Resource').findById(id_resource, function (err, resource) {
resource.quantity = resource.quantity - consume;
resource.save();
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("/resources/" + id_resource);
},
//JSON responds showing the updated values
json: function(){
res.json(resource);
}
});
}
})
};
\ No newline at end of file
......@@ -4,6 +4,7 @@ var express = require('express'),
var indexController = require('../controllers/index');
router.use('/users', require('./userRoute'));
router.use('/resources', require('./resourceRoute'));
router.route('/').get(indexController.welcome);
......
'use strict';
var express = require('express'),
router = express.Router();
var resourceController = require('../controllers/resources');
router.route('/')
.get(resourceController.list_resources)
.post(resourceController.add_resource);
router.route('/:id')
.get(resourceController.view_resource);
router.route('/:id/edit')
.get(resourceController.edit_resource)
.post(resourceController.post_edit_resource);
router.route('/:id/delete')
.get(resourceController.delete_resource);
router.route('/:id/consume')
.get(resourceController.consume)
.post(resourceController.post_consume_resource)
module.exports = router
<div class="modal fade" id="add-resource-modal" tabindex="-1" role="dialog" aria-labelledby="add-resource-modal-label" aria-hidden="true">
<div class="modal-dialog" role="document">
<form action="/resources/" method="POST">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add new resource</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label for="input-type">Type</label>
<input type="text" class="form-control" id="input-type" name="type" aria-describedby="quantityHelp" placeholder="Enter type">
</div>
<div class="form-group">
<label for="input-quantity">quantity</label>
<input type="quantity" class="form-control" id="input-quantity" name="quantity" aria-describedby="quantityHelp" placeholder="Enter quantity">
</div>
<div class="form-group">
<label for="input-unit">unit</label>
<input type="unit" class="form-control" id="input-unit" name="unit" aria-describedby="unitHelp" placeholder="Enter unit">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Add resource</button>
</div>
</div>
</form>
</div>
</div>
\ No newline at end of file
{% extends "layout/base.html" %}
{% block content %}
<main role="main" class="container">
<h2>Consume resource #{{ resource.type }}</h2>
<hr>
<form action="" method="POST">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="input-quantity">Consume</span>
</div>
<input type="quantity" name="quantity" class="form-control" aria-label="Quantity"> {{ resource.unit }}
</div>
<button type="submit" type="button" class="btn btn-primary float-right">Submit modifications</button>
</form>
</main><!-- /.container -->
{% endblock %}
\ No newline at end of file
{% extends "layout/base.html" %}
{% block content %}
<main role="main" class="container">
<h2>Edit resource #{{ resource.type }}</h2>
<hr>
<form action="" method="POST">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="input-name">Type</span>
</div>
<input type="text" name="type" class="form-control" placeholder="{{ resource.type }}" value="{{ resource.type }}" aria-label="Type">
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="input-title">Quantity</span>
</div>
<input type="quantity" name="quantity" class="form-control" placeholder="{{ resource.quantity }}" value="{{ resource.quantity }}" aria-label="Quantity">
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="input-title">Unit</span>
</div>
<input type="unit" name="unit" class="form-control" placeholder="{{ resource.unit }}" value="{{ resource.unit }}" aria-label="Unit">
</div>
<button type="submit" type="button" class="btn btn-primary float-right">Submit modifications</button>
</form>
</main><!-- /.container -->
{% endblock %}
\ No newline at end of file
{% extends "layout/base.html" %}
{% block content %}
<main role="main" class="container">
<div class="row">
<div class="col-12">
{% include "../partials/modals/add-resource.html" %}
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#add-resource-modal">
Add new resource
</button>
<hr>
<div class="list-group">
{% for resource in resources %}
<div class="list-group-item list-group-item-action align-items-start list-group-item-flex">
<div class="list-group-item-tools">
<a href="resources/{{ resource._id }}" title="Look resource"><i class="fas fa-eye"></i></a>
<a href="resources/{{ resource._id }}/edit" title="Edit resource"><i class="fas fa-pencil-alt"></i></a>
</div>
<div class="list-group-item-content">
<div class="d-flex w-90 justify-content-between">
<h5 class="mb-1">{{ resource.type }}</h5>
</div>
<p class="mb-1">{{ resource.quantity }} {{ resource.unit }}</p>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
</div>
</main><!-- /.container -->
{% endblock %}
\ No newline at end of file
{% extends "layout/base.html" %}
{% block content %}
<main role="main" class="container">
<div class="row">
<div class="col-12">
<h2>View of resource: {{ resource.type }}</h2>
<hr>
<div class="list-group">
<div class="list-group-item list-group-item-action align-items-start list-group-item-flex">
<div class="list-group-item-tools">
<a href="/resources/{{ resource._id }}/edit" title="Edit"><i class="fas fa-pencil-alt"></i></a>
<a href="/resources/{{ resource._id }}/delete" onclick="return confirm('Are you sure to delete this resource?')" title="Delete"><i class="far fa-trash-alt"></i></a>
</div>
<div class="list-group-item-content">
<div class="d-flex w-90 justify-content-between">
<h5 class="mb-1">{{ resource.type }}</h5>
</div>
<p class="mb-1">{{ resource.quantity }} {{ resource.unit }}</p>
</div>
</div>
</div>
</div>
</div>
</main><!-- /.container -->
{% endblock %}
\ No newline at end of file
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