-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (121 loc) · 3.5 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
134
135
136
137
138
139
140
141
142
import express from "express";
import bodyParser from "body-parser";
import dotenv from 'dotenv';
import pkg from 'pg';
const { Client } = pkg;
dotenv.config();
const app = express();
const port = 3000;
const connectionString = process.env.DATABASE_URL;
const db = new Client({
connectionString: connectionString,
ssl: {
rejectUnauthorized: false,
},
});
db.connect();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
let currentUserId = 1;
let users = [
{ id: 1, name: "Nikhil", color: "teal" },
{ id: 2, name: "Jack", color: "powderblue" },
];
async function checkVisisted() {
const result = await db.query(
"SELECT country_code FROM visited_countries JOIN users ON users.id = user_id WHERE user_id = $1; ",
[currentUserId]
);
let countries = [];
result.rows.forEach((country) => {
countries.push(country.country_code);
});
return countries;
}
async function getCurrentUser() {
const result = await db.query("SELECT * FROM users");
users = result.rows;
return users.find((user) => user.id == currentUserId);
}
app.get("/", async (req, res) => {
const countries = await checkVisisted();
const currentUser = await getCurrentUser();
let uniqueCountriesSet = new Set(countries);
res.render("index.ejs", {
countries: countries,
total: uniqueCountriesSet.size,
users: users,
color: currentUser.color,
});
});
app.post("/add", async (req, res) => {
const input = req.body["country"];
// console.log(input);
const currentUser = await getCurrentUser();
if (input !== "") {
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;
const existingResult = await db.query(
"SELECT * FROM visited_countries WHERE country_code = $1 AND user_id = $2",
[countryCode, currentUserId]
);
if (existingResult.rows.length > 0) {
throw new Error("Country has already been added, try again!");
}
await db.query(
"INSERT INTO visited_countries (country_code, user_id) VALUES ($1, $2)",
[countryCode, currentUserId]
);
res.redirect("/");
} catch (err) {
console.log(err);
const countries = await checkVisisted();
let uniqueCountriesSet = new Set(countries);
res.render("index.ejs", {
countries: countries,
total: uniqueCountriesSet.size,
users: users,
color: currentUser.color,
error: err.message,
});
}
} else {
const countries = await checkVisisted();
let uniqueCountriesSet = new Set(countries);
res.render("index.ejs", {
countries: countries,
total: uniqueCountriesSet.size,
users: users,
color: currentUser.color,
error: "Please enter a country name!",
});
}
});
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) => {
const name = req.body.name;
const color = req.body.color;
// console.log(color);
const result = await db.query(
"INSERT INTO users (name, color) VALUES($1, $2) RETURNING *;",
[name, color]
);
const id = result.rows[0].id;
currentUserId = id;
res.redirect("/");
});
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});