-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
79 lines (70 loc) · 1.79 KB
/
todo.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
var express = require("express");
const bodyParser = require("body-parser");
var todo = express.Router();
const Pool = require("pg").Pool;
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var config = {
user: "pxyelcidwntupx",
database: "dd9evum7thmpu0",
host: "ec2-54-225-76-201.compute-1.amazonaws.com",
port: "5432",
password: "ddb6afe9f49508b09c3e54ab8b48d553445cc13be65d6c4c5ea0b83ad29ce5d4"
};
var pool = new Pool(config);
todo.post("/list", (req, res) => {
var email = req.body.email;
pool.query("select * from list where email=($1)", [email], (err, result) => {
if (err) {
res.send("Select " + err.toString());
} else if (result.rows.length == 0) {
res.send("No list");
} else {
res.send(result.rows);
}
});
});
todo.post("/add", (req, res) => {
var { email, title, desc } = req.body;
pool.query(
"insert into list (email,title,description,upvote,downvote) values($1,$2,$3,$4,$5)",
[email, title, desc, 0, 0],
(err, result) => {
if (err) {
res.send("Insert " + err.toString());
} else {
res.send("Success");
}
}
);
});
todo.put("/edit", (req, res) => {
var { id, upvote, downvote } = req.body;
pool.query(
"update list set upvote=($2), downvote=($3) where id=($1)",
[id, upvote, downvote],
(err, result) => {
if (err) {
res.send("Updation err" + err);
} else {
res.send(result.toString());
}
}
);
});
todo.post("/delete", (req, res) => {
var { id } = req.body;
pool.query(
"delete from list where id=($1)",
[id],
(err, result) => {
if (err) {
res.send("delete " + err.toString());
} else {
res.send("success");
}
}
);
});
module.exports = todo;