-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (83 loc) · 2.17 KB
/
index.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
92
93
94
const express = require("express");
const app = express();
const fs = require("fs");
const path = require("path");
const sanitize = require("./sanitize.js")
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
const { Client } = require("pg");
const pgClient = new Client({
user: "postgres",
host: "localhost",
database: "contacts",
password: "admin",
port: 5432
});
pgClient.connect();
app.get("/", async (req, res) => {
res.send(fs.readFileSync(`public/index.html`));
});
app.get("/contacts", async (req, res) => {
let queryResults = [];
try {
queryResults = await pgClient.query(
'SELECT "firstName", "lastName", "price" FROM contacts ORDER BY "price" DESC'
);
} catch (err) {
console.log(err);
}
res.json(queryResults.rows);
});
app.post("/contact", async (req, res) => {
const insertContactSQL =
'INSERT INTO contacts("firstName", "lastName", "price") VALUES($1, $2, $3)';
const newContact = sanitize.sanitizeNewContact(req.body);
try {
await pgClient.query(insertContactSQL, [
newContact.firstName,
newContact.lastName,
newContact.price
]);
} catch (err) {
console.log(err);
res.sendStatus(500);
return;
}
res.sendStatus(200);
});
app.delete("/contact", async (req, res) => {
const deleteContactSQL =
'DELETE FROM contacts WHERE "firstName" = $1 AND "lastName" = $2';
try {
await pgClient.query(deleteContactSQL, [
req.body.firstName,
req.body.lastName
]);
} catch (err) {
console.log(err);
res.sendStatus(500);
return;
}
res.sendStatus(200);
});
app.put("/contact", async (req, res) => {
const updateContactSQL =
'UPDATE contacts SET "price" = $1 WHERE "firstName" = $2 AND "lastName" = $3';
const newContact = sanitize.sanitizeNewContact(req.body);
try {
await pgClient.query(updateContactSQL, [
newContact.price,
newContact.firstName,
newContact.lastName
]);
} catch (err) {
console.log(err);
res.sendStatus(500);
return;
}
res.sendStatus(200);
});
app.listen(3000, function() {
console.log("Example app listening on port 3000! http://localhost:3000/");
});
module.exports = app;