-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
75 lines (69 loc) · 2.29 KB
/
server.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
const express = require("express");
const pool = require("./db");
app.set('port', process.env.PORT || 3000);
app.listen(port, () => console.log(`Server has started on port: ${port}`));
const app = express();
app.use(express.json());
//routes
app.get("/", async (req, res) => {
try {
const data = await pool.query("SELECT * FROM shipments");
res.status(200).send(data.rows);
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});
app.post("/", async (req, res) => {
const { id, destination, origin, status } = req.body;
//You put the columns of your table here and then you declare it again at the end of the pool query
try {
await pool.query(
"INSERT INTO shipments (id, destination, origin, status) VALUES ($1, $2, $3, $4)",
[id, destination, origin, status]
);
//The values are like $1, $2, $3, $4 because it's more of a mapping thing and not the actual values
//This is a short hand version of this kind of code => const id = req.body.id, const destination = req.body.destination etc so we dont have to type too much
res
.status(200)
.send({ message: "Successfully added shipment into database." });
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});
app.get("/setupdb", async (req, res) => {
try {
await pool.query(
"CREATE TABLE shipments(id INTEGER PRIMARY KEY, destination VARCHAR(50) NOT NULL, origin VARCHAR(50) NOT NULL, status VARCHAR(50) NOT NULL);"
);
res.status(200).send({ message: "Shipments table created." });
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});
app.delete("/:id", async (req, res) => {
const { id } = req.params;
try {
await pool.query("DELETE FROM shipments WHERE id = $1", [id]);
res.status(200).send({ message: "Successfully deleted shipment." });
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});
app.put("/:id", async (req, res) => {
const { id } = req.params;
const { origin, destination, status } = req.body;
try {
await pool.query(
"UPDATE shipments SET id = $1, origin = $2, destination = $3, status=$4",
[id, origin, destination, status]
);
res.status(200).send({ message: "Successfully updated shipment." });
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});