kbo25

task-controller.js

Apr 9th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Task = require('../models/Task');
  2. let currentID = null; // Debug
  3.  
  4. module.exports = {
  5.   getIndex: function (req, res) {
  6.     // TODO: get all open, inProgress and finished tasks, pass them to the index view and render it
  7.     Task.find()
  8.       .then(tasks => {
  9.         res.render('index', {
  10.           'openTasks': tasks.filter(task => task.status === "Open"),
  11.           'inProgressTasks': tasks.filter(task => task.status === "In Progress"),
  12.           'finishedTasks': tasks.filter(task => task.status === "Finished")
  13.         });
  14.       });
  15.   },
  16.   getCreate: function (req, res) {
  17.     // TODO: render the create view
  18.     res.render('create');
  19.   },
  20.   postCreate: function (req, res) {
  21.     // TODO: create new task and redirect to the index view
  22.     Task.create(req.body)
  23.       .then(() => res.redirect('/'))
  24.       .catch(() => res.redirect('create'));
  25.   },
  26.   getEdit: function (req, res) {
  27.     // TODO: get the task for editing, pass it to the edit view and render it
  28.     Task.findById(req.params.id)
  29.         .then(tasks => {
  30.           // console.log(tasks);
  31.           currentID = req.params.id;
  32.           console.log(currentID, 'id GET');
  33.           return res.render('edit', {
  34.             task: {
  35.               title: tasks.title,
  36.               open: tasks.status === 'Open',
  37.               inProgress: tasks.status === 'In Progress',
  38.               finished: tasks.status === 'Finished'
  39.             }  
  40.         })
  41.     });
  42.   },
  43.   postEdit: function (req, res) {
  44.     // TODO: update the task and redirect to index view
  45.     Task.findByIdAndUpdate(currentID, { title: req.body.title, status: req.body.status }, (res) => {
  46.       // console.log(currentID, 'id POST'); // Debug
  47.       return res.redirect('/');
  48.     })
  49.       .catch((err) => console.log(err));
  50.     },
  51.   getDelete: function (req, res) {
  52.     // TODO: get the task for deleting, pass it to the detele view and render it
  53.   },
  54.   postDelete: function (req, res) {
  55.     // TODO: delete the task and redirect to the index view
  56.   }
  57. };
Add Comment
Please, Sign In to add comment