-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
91 lines (78 loc) · 2.33 KB
/
app.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
91
const express = require("express");
const db = require("./models/database");
const app = express();
app.use(express.static("public"));
app.use(express.json());
app.get("/todos", (req, res) => {
db.query("SELECT * FROM todos_table ORDER BY id asc", (err, result) => {
if (!err) {
res.status(200).json(result.rows);
} else {
console.error("Error retrieving todos", err);
res.status(500).json({ error: "Internal server error" });
}
});
});
app.post("/todos", (req, res) => {
const { item, priority, notes, duedate, completed } = req.body;
db.query(
"INSERT INTO todos_table(item,priority,notes,duedate,completed) VALUES ($1,$2,$3,$4,$5) RETURNING *",
[item, priority, notes, duedate, completed],
(err, result) => {
if (!err) {
res.status(201).json(result.rows[0]);
} else {
console.error("Error creating todo", err);
res.status(500).json({ error: "Internal server error" });
}
}
);
});
app.put("/todos", (req, res) => {
const { id, notes, priority, duedate } = req.body;
db.query(
"UPDATE todos_table SET priority=$2, notes=$3, duedate=$4 WHERE id=$1 RETURNING *",
[id, priority, notes, duedate],
(err, result) => {
if (!err) {
res.status(200).json(result.rows[0]);
} else {
console.error("Error updating todo data", err);
res.status(500).json({ error: "Internal server error" });
}
}
);
});
app.put("/todos/checked", (req, res) => {
const { id, completed } = req.body;
db.query(
"UPDATE todos_table SET completed=$2 WHERE id = $1 RETURNING *",
[id, completed],
(err, result) => {
if (!err) {
res.status(200).send(result.rows[0]);
} else {
console.error("Error updating checking todo-item", err);
res.status(500).json({ error: "Internal server error" });
}
}
);
});
app.delete("/todos/:id", (req, res) => {
const todoId = req.params.id;
db.query(
"DELETE FROM todos_table WHERE id = $1 RETURNING *",
[todoId],
(err, result) => {
if (!err) {
res.json({ message: "Todo item deleted" });
} else {
console.error("Error deleting todo", err);
res.status(500).json({ error: "Internal server error" });
}
}
);
});
app.listen(5000, () => {
console.log("Express listening at port 5000");
});