Commit 10370a35 authored by jjaouen's avatar jjaouen
Browse files

Merge branch 'feature/views' into develop

Conflicts:
	app.js
	views/checklists/view.html
parents 201802c0 50e2e89c
...@@ -33,7 +33,11 @@ var personnages = require('./models/users'); ...@@ -33,7 +33,11 @@ var personnages = require('./models/users');
var resources = require('./models/resources'); var resources = require('./models/resources');
var listes = require('./models/checklists'); var listes = require('./models/checklists');
var source = require('./models/sources'); var source = require('./models/sources');
<<<<<<< HEAD
var datacenter = require('./models/datacenters'); var datacenter = require('./models/datacenters');
=======
var tache = require('./models/tasks');
>>>>>>> feature/views
// Load templating and statics // Load templating and statics
app.use(express.static(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, 'public')));
......
var express = require('express'), var express = require('express'),
router = express.Router(), router = express.Router(),
mongoose = require('mongoose'); mongoose = require('mongoose'),
Task = mongoose.model('Task');
//GET all checklists //GET all checklists
exports.list_checklists = function(req, res, next) { exports.list_checklists = function(req, res, next) {
...@@ -60,6 +61,55 @@ mongoose = require('mongoose'); ...@@ -60,6 +61,55 @@ mongoose = require('mongoose');
}) })
}; };
//POST a new resource
exports.add_task = 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;
var checked = false;
var deadline = req.body.deadline;
mongoose.model('Task').create({
_id : new mongoose.Types.ObjectId(),
title: title,
description: description,
checked: checked,
deadline: deadline
}, function(err, tmpTask)
{
if (err) {
console.log(err);
} else {
console.log("ceujkfdgbluzeyuilfzge " + tmpTask);
mongoose.model('Checklist').findByIdAndUpdate(req.params.id, {$push: {task: tmpTask}}, function(err, checklist) {
if(err){
console.log(err);
console.log(checklist);
res.send("There was a problem adding the information to the database.");
} else {
console.log(checklist);
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) { exports.view_checklist = function(req, res) {
mongoose.model('Checklist').findById(req.params.id, function (err, checklist) { mongoose.model('Checklist').findById(req.params.id, function (err, checklist) {
if (err) { if (err) {
...@@ -70,7 +120,8 @@ mongoose = require('mongoose'); ...@@ -70,7 +120,8 @@ mongoose = require('mongoose');
html: function(){ html: function(){
res.render('checklists/view', { res.render('checklists/view', {
title: 'View of ' + checklist.title, title: 'View of ' + checklist.title,
checklist : checklist checklist : checklist,
tasks : checklist.task
}); });
}, },
json: function(){ json: function(){
...@@ -116,10 +167,10 @@ mongoose = require('mongoose'); ...@@ -116,10 +167,10 @@ mongoose = require('mongoose');
console.log(req.body); console.log(req.body);
//find the document by ID //find the document by ID
mongoose.model('Description').findByIdAndUpdate(id, { mongoose.model('Checklist').findByIdAndUpdate(id, {
title : title, title : title,
description : description description : description
}, function (err, description) { }, function (err, checklist) {
if (err) { if (err) {
res.send("There was a problem updating the information to the database: " + err); res.send("There was a problem updating the information to the database: " + err);
} }
...@@ -127,11 +178,11 @@ mongoose = require('mongoose'); ...@@ -127,11 +178,11 @@ mongoose = require('mongoose');
//HTML responds by going back to the page or you can be fancy and create a new view that shows a success page. //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({ res.format({
html: function(){ html: function(){
res.redirect("/descriptions/" + description._id); res.redirect("/checklists/" + checklist._id);
}, },
//JSON responds showing the updated values //JSON responds showing the updated values
json: function(){ json: function(){
res.json(description); res.json(checklist);
} }
}); });
} }
......
var express = require('express'),
router = express.Router(),
mongoose = require('mongoose');
//GET all checklists
exports.list_tasks = function(req, res, next) {
//retrieve all checklists from Monogo
mongoose.model('Task').find({}, function (err, tasks) {
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('./tasks/list', {
title: 'Tasks list',
tasks: tasks
});
},
//JSON response will show all users in JSON format
json: function(){
res.json(tasks);
}
});
}
});
}
//POST a new resource
exports.add_task = 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;
var checked = false;
var deadline = req.body.deadline;
//call the create function for our database
mongoose.model('task').create({
_id : new mongoose.Types.ObjectId(),
title : title,
description : description,
checked : checked,
deadline : deadline
}, function (err, task) {
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("tasks");
// And forward to success page
res.redirect("/tasks");
},
//JSON response will show the newly created user
json: function(){
res.json(tasks);
}
});
}
})
};
//GET the individual user by Mongo ID
exports.edit_task = function(req, res) {
//search for the user within Mongo
mongoose.model('Task').findById(req.params.id, function (err, task) {
if (err) {
console.log('GET Error: There was a problem retrieving: ' + err);
} else {
//Return the user
console.log('GET Retrieving ID: ' + task._id);
res.format({
//HTML response will render the 'edit.jade' template
html: function(){
res.render('tasks/edit', {
title: 'Edit task #' + task._id,
task : task
});
},
//JSON response will return the JSON output
json: function(){
res.json(task);
}
});
}
});
};
//POST to update a user by ID
exports.post_edit_task = function(req, res) {
// Get our REST and form values.
var id = req.params.id;
var title = req.body.title;
var description = req.body.description;
var checked = req.body.checked;
var deadline = req.body.deadline;
console.log(req.body);
//find the document by ID
mongoose.model('Task').findByIdAndUpdate(id, {
title : title,
description : description,
checked : checked,
deadline : deadline
}, function (err, task) {
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("/tasks/" + task._id);
},
//JSON responds showing the updated values
json: function(){
res.json(task);
}
});
}
})
};
//DELETE a Users by ID
exports.delete_task = function (req, res){
//find user by ID
mongoose.model('Task').findById(req.params.id, function (err, task) {
if (err) {
return console.error(err);
} else {
//remove it from Mongo
task.remove(function (err, task) {
if (err) {
return console.error(err);
} else {
//Returning success messages saying it was deleted
console.log('DELETE removing ID: ' + task._id);
res.format({
//HTML returns us back to the main page, or you can create a success page
html: function(){
res.redirect("/tasks");
},
//JSON returns the item with the message that is has been deleted
json: function(){
res.json({message : 'deleted',
item : task
});
}
});
}
});
}
});
};
\ No newline at end of file
var mongoose = require('mongoose'); var mongoose = require('mongoose');
const Schema = mongoose.Schema; const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId; const ObjectId = Schema.ObjectId;
var taskSchema = require('./tasks').schema;
const checklistSchema = new Schema({ const checklistSchema = new Schema({
_id: ObjectId, _id: ObjectId,
task: [{type: ObjectId, ref: 'task'}], task: [taskSchema],
title: { title: {
type: String, type: String,
required: true required: true
......
...@@ -18,3 +18,4 @@ const taskSchema = new Schema({ ...@@ -18,3 +18,4 @@ const taskSchema = new Schema({
}); });
var Task = mongoose.model('Task', taskSchema); var Task = mongoose.model('Task', taskSchema);
module.exports = Task;
...@@ -3,7 +3,7 @@ var express = require('express'), ...@@ -3,7 +3,7 @@ var express = require('express'),
router = express.Router(); router = express.Router();
var checklistController = require('../controllers/checklists'); var checklistController = require('../controllers/checklists');
// var resourceController = require('../controllers/tasks') var taskController = require('../controllers/tasks');
router.route('/') router.route('/')
.get(checklistController.list_checklists) .get(checklistController.list_checklists)
...@@ -20,4 +20,12 @@ var express = require('express'), ...@@ -20,4 +20,12 @@ var express = require('express'),
.get(checklistController.delete_checklist); .get(checklistController.delete_checklist);
/////////////////////////
// router.route(':id/tasks/')
// .get(taskController.list_tasks);
router.route('/:id/tasks')
.post(checklistController.add_task);
module.exports = router module.exports = router
...@@ -3,9 +3,19 @@ ...@@ -3,9 +3,19 @@
{% block content %} {% block content %}
<main role="main" class="container"> <main role="main" class="container">
<<<<<<< HEAD
<div> <div>
<!--Bouton Add --> <!--Bouton Add -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#add-task-modal" data-whatever="@mdo">Add a new task</button> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#add-task-modal" data-whatever="@mdo">Add a new task</button>
=======
<h2>View of checklist #{{ checklist._id }}</h2>
{% include "../partials/modals/add-task.html" %}
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#add-task-modal">
Add new task
</button>
>>>>>>> feature/views
<div class="dropdown float-right"> <div class="dropdown float-right">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
...@@ -21,6 +31,7 @@ ...@@ -21,6 +31,7 @@
{% include "partials/modals/add-task.html" %} {% include "partials/modals/add-task.html" %}
</div> </div>
<<<<<<< HEAD
<hr> <hr>
<!--Liste task--> <!--Liste task-->
...@@ -45,6 +56,35 @@ ...@@ -45,6 +56,35 @@
</div> </div>
<div class="col-sm-1 col-md-1"> <!--colonne checkbox --> <div class="col-sm-1 col-md-1"> <!--colonne checkbox -->
<a data-toggle="collapse" href="#description1" role="button" aria-expanded="false" aria-controls="description1"><i class="far fa-caret-square-down"></i></a> <a data-toggle="collapse" href="#description1" role="button" aria-expanded="false" aria-controls="description1"><i class="far fa-caret-square-down"></i></a>
=======
<div class="list-group-item list-group-item-action align-items-start list-group-item-flex">
<div class="list-group-item-tools">
<a href="/checklists/{{ checklist._id }}/edit" title="Edit"><i class="fas fa-pencil-alt"></i></a>
<a href="/checklists/{{ checklist._id }}/delete" onclick="return confirm('Are you sure to delete this checklist?')" 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">{{ checklist.title }}</h5>
</div>
<p class="mb-1">{{ checklist.description }}</p>
{% for task in tasks %}
<div style="border: solid 1px grey;">
<p>Tache:
<!-- <a href="/checklists/{{ checklist._id }}/tasks/{{ task._id }}" title="Edit"><i class="fas fa-pencil-alt"></i></a> -->
<!-- <a href="/checklists/{{ checklist._id }}/tasks/{{ task._id }}" onclick="return confirm('Are you sure to delete this task?')" title="Delete"><i class="far fa-trash-alt"></i></a> -->
</p>
<p class="mb-1">Title: {{ task.title }}</p>
<p class="mb-1">Description: {{ task.description }}</p>
{% if task.checked == "true" %}
<p class="mb-1">State: Done</p>
{% else %}
<p class="mb-1">State: Undone</p>
{% endif %}
</div>
{% endfor %}
</div>
>>>>>>> feature/views
</div> </div>
</div> </div>
......
<div class="modal fade" id="add-task-modal" tabindex="-1" role="dialog" aria-labelledby="add-task-modal-label" aria-hidden="true"> <div class="modal fade" id="add-task-modal" tabindex="-1" role="dialog" aria-labelledby="add-task-modal-label" aria-hidden="true">
<div class="modal-dialog" role="document"> <div class="modal-dialog" role="document">
<form action="/checklist/checklist.id/tasks/" method="POST"> <form action="/checklists/{{checklist.id}}/tasks/" method="POST">
<div class="modal-content"> <div class="modal-content">
<div class="modal-header"> <div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add new task</h5> <h5 class="modal-title" id="exampleModalLabel">Add new task</h5>
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="input-deadline">Deadline</label> <label for="input-deadline">Deadline</label>
<input type="text" class="form-control" id="input-deadline" name="deadline" aria-describedby="deadlineHelp" placeholder="Enter deadline"> <input type="date" class="form-control" id="input-deadline" name="deadline" aria-describedby="deadlineHelp" placeholder="Enter deadline">
</div> </div>
......
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
<div class="input-group-prepend"> <div class="input-group-prepend">
<span class="input-group-text" id="input-done">Done</span> <span class="input-group-text" id="input-done">Done</span>
</div> </div>
<input type="text" name="done" class="form-control" placeholder="{{ task.done }}" value="{{ task.done }}" aria-label="Deadline"> <input type="checkbox" name="checked" class="form-control" placeholder="{{ task.done }}" value="{{ task.done }}" aria-label="Deadline">
</div> </div>
......
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