Commit 7b7f599f authored by jjaouen's avatar jjaouen
Browse files

view + controller Task/checklist

parent e3516d1b
......@@ -23,6 +23,7 @@ var personnages = require('./models/users');
var resources = require('./models/resources');
var listes = require('./models/checklists');
var source = require('./models/sources');
var tache = require('./models/tasks');
// Load templating and statics
app.use(express.static(path.join(__dirname, 'public')));
......
var express = require('express'),
router = express.Router(),
mongoose = require('mongoose');
mongoose = require('mongoose'),
Task = mongoose.model('Task');
//GET all checklists
exports.list_checklists = function(req, res, next) {
......@@ -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) {
mongoose.model('Checklist').findById(req.params.id, function (err, checklist) {
if (err) {
......@@ -70,7 +120,8 @@ mongoose = require('mongoose');
html: function(){
res.render('checklists/view', {
title: 'View of ' + checklist.title,
checklist : checklist
checklist : checklist,
tasks : checklist.task
});
},
json: function(){
......@@ -116,10 +167,10 @@ mongoose = require('mongoose');
console.log(req.body);
//find the document by ID
mongoose.model('Description').findByIdAndUpdate(id, {
mongoose.model('Checklist').findByIdAndUpdate(id, {
title : title,
description : description
}, function (err, description) {
}, function (err, checklist) {
if (err) {
res.send("There was a problem updating the information to the database: " + err);
}
......@@ -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.
res.format({
html: function(){
res.redirect("/descriptions/" + description._id);
res.redirect("/checklists/" + checklist._id);
},
//JSON responds showing the updated values
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');
const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;
var taskSchema = require('./tasks').schema;
const checklistSchema = new Schema({
_id: ObjectId,
task: [{type: ObjectId, ref: 'task'}],
task: [taskSchema],
title: {
type: String,
required: true
......
......@@ -18,3 +18,4 @@ const taskSchema = new Schema({
});
var Task = mongoose.model('Task', taskSchema);
module.exports = Task;
......@@ -3,7 +3,7 @@ var express = require('express'),
router = express.Router();
var checklistController = require('../controllers/checklists');
// var resourceController = require('../controllers/tasks')
var taskController = require('../controllers/tasks');
router.route('/')
.get(checklistController.list_checklists)
......@@ -20,4 +20,12 @@ var express = require('express'),
.get(checklistController.delete_checklist);
/////////////////////////
// router.route(':id/tasks/')
// .get(taskController.list_tasks);
router.route('/:id/tasks')
.post(checklistController.add_task);
module.exports = router
......@@ -5,7 +5,13 @@
<div class="row">
<div class="col-12">
<h2>View of user #{{ checklist._id }}</h2>
<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>
<hr>
......@@ -21,6 +27,22 @@
<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>
</div>
......
{% extends "layout/base.html" %}
{% block content %}
<main role="main" class="container">
<h2>Edit task #{{ task.title }}</h2>
<hr>
<form action="" method="POST">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="input-title">Title</span>
</div>
<input type="text" name="title" class="form-control" placeholder="{{ task.title }}" value="{{ task.title }}" aria-label="Title">
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="input-description">Description</span>
</div>
<input type="text" name="description" class="form-control" placeholder="{{ task.description }}" value="{{ task.description }}" aria-label="Description">
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="input-description">Deadline</span>
</div>
<input type="text" name="deadline" class="form-control" placeholder="{{ task.deadline }}" value="{{ task.deadline }}" aria-label="Deadline">
</div>
<!--Checkbox-->
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="input-done">Done</span>
</div>
<input type="text" name="done" class="form-control" placeholder="{{ task.done }}" value="{{ task.done }}" aria-label="Deadline">
</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
<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">
<form action="/checklist/checklist.id/tasks/" method="POST">
<form action="/checklists/{{checklist.id}}/tasks/" method="POST">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add new task</h5>
......@@ -20,7 +20,7 @@
</div>
<div class="form-group">
<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>
......
......@@ -36,7 +36,7 @@
<div class="input-group-prepend">
<span class="input-group-text" id="input-done">Done</span>
</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>
......
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