forked from LukeMwila/multi-container-nginx-react-node-mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo_controller.js
62 lines (55 loc) · 1.19 KB
/
todo_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
const ToDo = require('../models/ToDo');
const create = async (req, res, next) => {
const toDoProps = req.body;
try {
const toDo = await ToDo.create(toDoProps);
res.status(201).send(toDo);
} catch (e) {
next();
}
};
const get = async (req, res, next) => {
try {
const toDo = await ToDo.find();
res.status(200).send(toDo);
} catch (e) {
next();
}
};
const getById = async (req, res, next) => {
const toDoId = req.params.id;
try {
const toDo = await ToDo.findById({ _id: toDoId });
res.status(200).send(toDo);
} catch (e) {
next();
}
};
const edit = async (req, res, next) => {
const toDoId = req.params.id;
const toDoProps = req.body;
try {
await ToDo.findByIdAndUpdate({ _id: toDoId }, toDoProps);
const toDo = await ToDo.findById({ _id: toDoId });
res.status(200).send(toDo);
} catch (e) {
next();
}
};
const deleteToDo = async (req, res, next) => {
const toDoId = req.params.id;
try {
const toDo = await ToDo.findByIdAndRemove({ _id: toDoId });
res.status(204).send(toDo);
} catch (e) {
next();
}
};
const ToDoController = {
create,
get,
getById,
edit,
deleteToDo
};
module.exports = ToDoController;