-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (91 loc) · 2.54 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
95
96
97
98
99
100
101
102
103
import express from "express";
import bodyParser from "body-parser";
import pg from "pg";
import dotenv from "dotenv";
dotenv.config();
const app = express();
const port = 3000;
const db = new pg.Client({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_DATABASE,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
});
db.connect();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
let currentUserId = 1;
async function getUsers(){
const result = await db.query("SELECT * FROM users");
return result.rows;
}
async function getColor(){
const result = await db.query("SELECT color FROM users WHERE id = $1", [currentUserId]);
return result.rows[0].color;
}
async function checkVisisted() {
const result = await db.query("SELECT * FROM visited_countries JOIN users ON visited_countries.user_id = users.id WHERE user_id = $1", [currentUserId]);
let countries = [];
result.rows.forEach((country) => {
countries.push(country.country_code);
});
return countries;
}
app.get("/", async (req, res) => {
const countries = await checkVisisted();
res.render("index.ejs", {
countries: countries,
total: countries.length,
users: await getUsers(),
color: await getColor(),
});
});
//add country
app.post("/add", async (req, res) => {
const input = req.body["country"];
try {
const result = await db.query(
"SELECT country_code FROM countries WHERE LOWER(country_name) LIKE '%' || $1 || '%';",
[input.toLowerCase()]
);
const data = result.rows[0];
const countryCode = data.country_code;
try {
await db.query(
"INSERT INTO visited_countries (country_code,user_id) VALUES ($1,$2)",
[countryCode,currentUserId]
);
res.redirect("/");
} catch (err) {
console.log(err);
}
} catch (err) {
console.log(err);
}
});
//change to other account
app.post("/user", async (req, res) => {
if(req.body.add === "new"){
res.render("new.ejs")
}
else{
currentUserId = req.body.user;
res.redirect('/');
}
});
//add user
app.post("/new", async (req, res) => {
//Hint: The RETURNING keyword can return the data that was inserted.
//https://www.postgresql.org/docs/current/dml-returning.html
const pickedColor = req.body.color;
const pickedName = req.body.name;
await db.query(
"INSERT INTO users (name,color) VALUES ($1,$2)",
[pickedName,pickedColor]
)
res.redirect('/');
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});