-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtask.controller.js
77 lines (67 loc) · 1.69 KB
/
task.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
69
70
71
72
73
74
75
76
77
import { Task } from "../models/Task.js";
export async function createTask(req, res) {
try {
const { name, done, projectId } = req.body;
const newTask = await Task.create({
projectId,
name,
done,
});
res.json(newTask);
} catch (error) {
return res.status(500).json({ message: error.message });
}
}
export async function getTasks(req, res) {
try {
const tasks = await Task.findAll({
attributes: ["id", "projectId", "name", "done"],
order: [["id", "DESC"]],
});
res.json(tasks);
} catch (error) {
return res.status(500).json({ message: error.message });
}
}
export async function updateTask(req, res) {
const { id } = req.params;
// const { projectid, name, done } = req.body;
try {
// const updatedTask = await Task.update(
// { name, done, projectid },
// { where: { id } }
// );
const task = await Task.findOne({
attributes: ["name", "projectId", "done", "id"],
where: { id },
});
task.set(req.body);
await task.save();
res.json(task);
} catch (error) {
return res.status(500).json({ message: error.message });
}
}
export async function deleteTask(req, res) {
const { id } = req.params;
try {
await Task.destroy({
where: { id },
});
return res.sendStatus(204);
} catch (error) {
return res.status(500).json({ message: error.message });
}
}
export async function getTask(req, res) {
const { id } = req.params;
try {
const task = await Task.findOne({
where: { id },
attributes: ["id", "projectId", "name", "done"],
});
res.json(task);
} catch (error) {
return res.status(500).json({ message: error.message });
}
}