-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (115 loc) · 3.03 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import express from "express";
import bodyParser from "body-parser";
import pg from "pg";
import dotenv from "dotenv";
const app = express();
const port = 3000;
dotenv.config();
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,
ssl: {
rejectUnauthorized: false,
},
});
db.connect();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
let currentUserId = 1;
async function checkVisisted() {
const result = await db.query(
"SELECT country_code FROM visited_countries WHERE user_id = $1",
[currentUserId]
);
let countries = [];
result.rows.forEach((country) => {
countries.push(country.country_code);
});
return countries;
}
async function checkUser() {
const result = await db.query('SELECT * FROM "users"');
return result.rows;
}
async function getCurrentUser() {
const result = await db.query('SELECT * FROM "users" WHERE id = $1', [
currentUserId,
]);
return result.rows[0];
}
async function userExists(name) {
const result = await db.query('SELECT * FROM "users" WHERE name = $1', [
name,
]);
return result.rows.length > 0;
}
app.get("/", async (req, res) => {
try {
const countries = await checkVisisted();
const users = await checkUser();
const currentUser = await getCurrentUser();
if (!currentUser) {
res.render("new.ejs");
} else {
res.render("index.ejs", {
countries: countries,
total: countries.length,
users: users,
color: currentUser.color,
});
}
} catch (error) {
console.error("Error fetching data: ", error);
res.status(500).send("Internal Server Error");
}
});
app.post("/add", async (req, res) => {
const input = req.body["country"];
const currentUser = await getCurrentUser();
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, currentUser.id]
);
res.redirect("/");
} catch (err) {
console.error(err);
}
} catch (err) {
console.error(err);
}
});
app.post("/user", async (req, res) => {
if (req.body.add === "new") {
res.render("new.ejs");
} else {
currentUserId = req.body.user;
res.redirect("/");
}
});
app.post("/new", async (req, res) => {
if (await userExists(req.body.name)) {
res.status(409).json({ error: "User already exists" });
return;
}
const { name, color } = req.body;
const result = await db.query(
'INSERT INTO "users" (name, color) VALUES ($1, $2) RETURNING id',
[name, color]
);
currentUserId = result.rows[0].id;
res.redirect("/");
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});