-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.js
90 lines (63 loc) · 1.73 KB
/
tasks.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const Task = require('../models/Task')
const getAllTasks = async (req, res) => {
try{
const tasks = await Task.find({})
res.status(200).json({ tasks })
} catch(error) {
res.status(500).json({msg:error})
}
}
const createTask = async (req,res) => {
try{
const task = await Task.create(req.body)
res.status(201).json({ task })
} catch(error) {
res.status(500).json({msg:error})
}
}
const getTask = async (req,res) => {
try{
const {id:taskID} = req.params
const task = await Task.findOne({_id:taskID})
if(!task){
return res.status(404).json({msg: 'No task with id: $(taskID)'})
}
res.status(200).json({ tasks })
} catch(error){
res.status(500).json({msg:error})
}
}
const updateTask = async (req,res) => {
try{
const { id: taskID} = req.params
const task = await Task.findOneAndUpdate({_id:taskID},req.body,{
new:true,
runValidators:true,
})
if(!task){
return res.status(404).json({msg: 'No task with id: $(taskID)'})
}
res.status(200).json({task})
} catch(error) {
res.status(500).json({ msg:error})
}
}
const deleteTask = async (req, res) => {
try{
const {id:taskID}= req.params
const task = await Task.findOneAndDelete({_id:taskID})
if(!task){
return res.status(404).json({msg: 'No task with id: $(taskID)'})
}
res.status(200).json({ task })
}catch(error){
res.status(500).json({msg:error})
}
}
module.exports = {
getAllTasks,
createTask,
getTask,
updateTask,
deleteTask
}