-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (86 loc) · 2.83 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
import express from "express";
import bodyParser from "body-parser";
import pg from "pg";
const app = express();
const port = 3000;
const db = new pg.Client({
user: "postgres",
host: "localhost",
database: "world",
password: "hola987",
port: 5432,
});
db.connect();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
let currentUserId = 1
let users = [
{ id: 1, name: "Angela", color: "teal" },
{ id: 2, name: "Jack", color: "powderblue" },
];
async function getCurrentUser(){
const data = await db.query(
"SELECT * FROM users"
)
users = data.rows
return users.find((user) => user.id == currentUserId)
}
async function visitedCountries(){
let dataArray = []
let data = await db.query(
"SELECT country_code FROM visited_countries JOIN users ON users.id = user_id WHERE user_id = $1",
[currentUserId]
)
dataArray = data.rows
let countryCodes = []
for(let i = 0; i < dataArray.length; i ++){
countryCodes.push(dataArray[i].country_code)
}
return countryCodes
}
app.get("/", async (req, res) => {
let user = await getCurrentUser()
let countries = await visitedCountries()
console.log("/ USER: ", user)
console.log("/ COUNTRIES: ", countries)
res.render("index.ejs", {users: users, countries: countries, total: countries.length, color: user.color})
})
/* ADD COUNTRY */
app.post("/add", async (req, res) => {
console.log("/ADD CURRENTUSERID: ", currentUserId)
let newCountry = req.body.country.slice(0, 1).toUpperCase() + req.body.country.slice(1, req.body.country.length).toLowerCase()
console.log("NEW COUNTRY: ", newCountry)
try {
let data= await db.query("SELECT country_code FROM countries WHERE country_name LIKE '%' || $1 || '%'", [newCountry])
let inputCountryCode = data.rows[0].country_code
try {
await db.query("INSERT INTO visited_countries (country_code, user_id) VALUES($1, $2)", [inputCountryCode, currentUserId])
res.redirect("/")
} catch (err) {
console.log("INSERT country ERROR: ", err)
}
} catch (err) {
console.log("SELECT country ERROR: ",err)
}
});
/* CHANGING USERS */
app.post("/user", async (req, res) => {
let memberId = req.body.user
// console.log(memberId)
if (req.body.add === "new") {// redirect to new.ejs for adding new member
res.render("new.ejs")
} else {
currentUserId = memberId
res.redirect("/")
}
});
/* ADD NEW MEMBER */
app.post("/new", async (req, res) => {
let newMemberName = req.body.name
let newMemberColor = req.body.color
await db.query("INSERT INTO users (name, color) VALUES ($1, $2) RETURNING *", [newMemberName, newMemberColor])// using RETURNING to get the id from the new added member
res.redirect("/")
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});