-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabelController.js
More file actions
executable file
·54 lines (53 loc) · 1.64 KB
/
labelController.js
File metadata and controls
executable file
·54 lines (53 loc) · 1.64 KB
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
const Label = require("../models/label");
module.exports = {
getAllLabels: async (req, res) => {
try {
const labels = await Label.find({});
res.status(200).json({ labels });
} catch (error) {
error = error.message || "Try again something went wrong!";
res.status(400).json({ error });
}
},
createLabel: async (req, res) => {
try {
let { name, description, color } = req.body;
if (!name || !color) {
throw new Error("Please enter all required fields.");
} else {
const label = await Label.create({ name, description, color });
res.status(201).json({ label });
}
} catch (error) {
error = error.message || "Try again something went wrong!";
res.status(400).json({ error });
}
},
updateLabel: async (req, res) => {
try {
let { name, description, color } = req.body;
if (!name || !color) {
throw new Error("Please enter all required fields.");
} else {
const updatedLabel = await Label.findOneAndUpdate(
{ _id: req.params.labelId },
{ name, description, color },
{ new: true }
);
res.status(200).json({ updatedLabel });
}
} catch (error) {
error = error.message || "Try again something went wrong!";
res.status(400).json({ error });
}
},
deleteLabel: async (req, res) => {
try {
const deletedLabel = await Label.deleteOne({ _id: req.params.labelId });
res.status(204).json({ deletedLabel });
} catch (error) {
error = error.message || "Try again something went wrong!";
res.status(400).json({ error });
}
}
};