-
Notifications
You must be signed in to change notification settings - Fork 0
/
controllers.js
66 lines (66 loc) · 1.79 KB
/
controllers.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
const model = require('./task')
const allTask = async (req, res) => {
try{
let task = await model.find({})
if(!task[0]){
return res.json({success:"false"})
// return res.status(404).json(`No task found, create new`)
}
res.status(200).json({ task })
}
catch(err){
res.status(500).json({ msg: err })
}
}
const getTask = async (req, res) => {
try{
const { id: taskID } = req.params
const task = await model.findOne({ _id: taskID })
if(!task){
return res.status(404).json({msg: `${taskID} not found`})
}
res.status(200).json({ task })
}
catch(err){
res.status(500).json({ msg: err })
}
}
const createTask = async (req, res) => {
try{
let task = await model.create(req.body)
res.status(200).json({ task })
}
catch(err){
res.status(500).json({msg:err})
}
}
const updateTask = async (req, res) => {
try{
let { id: taskID } = req.params
let task = await model.findOneAndUpdate({ _id: taskID }, req.body, {
new: true,
runValidators: true
})
if(!task){
return res.status(404).json({ msg: `No task found`})
}
res.status(200).json({ task })
}
catch(err) {
res.status(500).json({msg:err})
}
}
const deleteTask = async (req, res) => {
try{
let { id: taskID } = req.params
let task = await model.findOneAndDelete({ _id: taskID })
if(!task){
return res.status(404).json({msg: `${taskID} not found`})
}
res.status(200).json(`Task Deleted..`)
}
catch(err){
res.status(500).json({msg:err})
}
}
module.exports = {allTask, getTask, createTask, updateTask, deleteTask}