Commit 6bd3944e authored by Yann Caumartin's avatar Yann Caumartin
Browse files

Adds mission/view

parent b224bb4a
var express = require('express'),
router = express.Router(),
mongoose = require('mongoose');
//GET all missions
exports.list_missions = function(req, res, next) {
//retrieve all users from Monogo
mongoose.model('Mission').find({}, function (err, missions) {
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('./missions/list', {
title: 'Missions list',
missions: missions
});
},
//JSON response will show all users in JSON format
json: function(){
res.json(missions);
}
});
}
});
}
//POST a new mission
exports.add_mission = 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 start_date = req.body.start_date;
var end_date = req.body.end_date;
var description = req.body.description;
var priority = req.body.priority;
//call the create function for our database
mongoose.model('Mission').create({
_id : new mongoose.Types.ObjectId(),
title : title ,
start_date : start_date ,
end_date : end_date,
description : description,
priority : priority
}, function (err, mission) {
if (err) {
res.send("There was a problem adding the information to the database.");
} else {
//mission has been created
console.log('POST creating new mission: ' + mission);
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("missions");
// And forward to success page
res.redirect("/missions");
},
//JSON response will show the newly created user
json: function(){
res.json(mission);
}
});
}
})
};
exports.view_mission = function(req, res) {
mongoose.model('Mission').findById(req.params.id, function (err, mission) {
if (err) {
console.log('GET Error: There was a problem retrieving: ' + err);
} else {
console.log('GET Retrieving ID: ' + mission._id);
res.format({
html: function(){
res.render('missions/view', {
title: 'View of ' + mission.type,
mission : mission
});
},
json: function(){
res.json(mission);
}
});
}
});
};
......@@ -8,4 +8,7 @@ var express = require('express'),
.get(missionController.list_missions)
.post(missionController.add_mission);
router.route('/:id')
.get(missionController.view_mission);
module.exports = router
......@@ -8,7 +8,7 @@
{% include "partials/modals/add-mission.html" %}
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#add-mission-modal">
Add new misssion
Add new mission
</button>
<hr>
......@@ -18,11 +18,11 @@
{% for mission in missions %}
<div class="list-group-item list-group-item-action align-items-start list-group-item-flex">
<div class="list-group-item-tools">
<a href="misssions/{{ misssion._id }}" title="Look mission"><i class="fas fa-eye"></i></a>
<a href="misssions/{{ misssion._id }}/edit" title="Edit mission"><i class="fas fa-pencil-alt"></i></a>
<a href="{{ mission._id }}" title="Look mission"><i class="fas fa-eye"></i></a>
<a href="{{ mission._id }}/edit" title="Edit mission"><i class="fas fa-pencil-alt"></i></a>
</div>
<div class="list-group-item-image">
<svg width="80" height="80" data-jdenticon-value="{{ misssion._id }}"></svg>
<svg width="80" height="80" data-jdenticon-value="{{ mission._id }}"></svg>
</div>
<div class="list-group-item-content">
<div class="d-flex w-90 justify-content-between">
......
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