-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.controller.js
68 lines (60 loc) · 1.28 KB
/
tasks.controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const express = require('express');
const app = express();
const tasksRoute = express.Router();
// Task model
let Task = require('./Models/task');
// Add Task
tasksRoute.route('/todos').post((req, res, next) => {
Task.create(req.body, (error, data) => {
if (error) {
return next(error)
} else {
res.json(data)
}
})
});
// Get All Tasks
tasksRoute.route('/todos').get((req, res) => {
Task.find((error, data) => {
if (error) {
return next(error)
} else {
res.json(data)
}
})
})
// Get All Open Tasks
tasksRoute.route('/todos/open').get((req, res) => {
Task.find({Done:false}, (error, data) => {
if (error) {
return next(error)
} else {
res.json(data)
}
})
})
// Get single task
tasksRoute.route('/todos/:id').get((req, res) => {
Task.findById(req.params.id, (error, data) => {
if (error) {
return next(error)
} else {
res.json(data)
}
})
})
// Update task
tasksRoute.route('/todos/:id').put((req, res, next) => {
Task.findByIdAndUpdate(req.params.id, {
$set: req.body
}, (error, data) => {
if (error) {
return next(error);
console.log(error)
} else {
res.json(data)
console.log('Data updated successfully')
}
})
})
module.exports = tasksRoute;