-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7f191ce
Showing
6 changed files
with
1,616 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
node_modules | ||
build | ||
npm-debug.log | ||
.nyc | ||
.env | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import express from "express"; | ||
import bodyParser from "body-parser"; | ||
import pg from "pg"; | ||
import dotenv from "dotenv"; | ||
|
||
const app = express(); | ||
const port = process.env.PORT || 3000; | ||
dotenv.config(); | ||
|
||
const db = new pg.Client({ | ||
connectionString: process.env.DBConnLink, | ||
ssl: { | ||
rejectUnauthorized: false | ||
} | ||
}); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
app.use(express.static("public")); | ||
|
||
db.connect(); | ||
|
||
app.get("/", async (req, res) => { | ||
var visited_countries = await get_visited_countries(); | ||
res.render("index.ejs", { | ||
countries: visited_countries, | ||
total: visited_countries.length | ||
}); | ||
}); | ||
|
||
app.post("/submit", async (req, res) => { | ||
|
||
const country = req.body["country"].trim(); | ||
console.log(country); | ||
const action = req.body.action; | ||
if (country === "") { | ||
res.redirect("/"); | ||
} else { | ||
if(action === "add") { | ||
try { | ||
const country_code_query = await db.query("SELECT country_code FROM countries WHERE LOWER(country_name) LIKE '%' || $1 || '%'", [country.toLowerCase()]); | ||
const country_code = country_code_query.rows[0].country_code; | ||
const err = await insert_visited_country(country_code); | ||
if (err) { | ||
var visited_countries = await get_visited_countries(); | ||
res.render("index.ejs", { | ||
countries: visited_countries, | ||
total: visited_countries.length, | ||
error: "This country has already been added. Try again." | ||
}); | ||
} else { | ||
res.redirect("/"); | ||
} | ||
} catch (e) { | ||
console.log("Error with querying countries table", e.stack) | ||
var visited_countries = await get_visited_countries(); | ||
res.render("index.ejs", { | ||
countries: visited_countries, | ||
total: visited_countries.length, | ||
error: "This country does not exist. Try again." | ||
}); | ||
} | ||
} else { | ||
try { | ||
const country_code_query = await db.query("SELECT country_code FROM countries WHERE LOWER(country_name) LIKE '%' || $1 || '%'", [country.toLowerCase()]); | ||
const country_code = country_code_query.rows[0].country_code; | ||
const err = await remove_visited_country(country_code); | ||
|
||
if(err) { | ||
const visited_countries = get_visited_countries(); | ||
res.render("index.ejs", { | ||
countries: visited_countries, | ||
total: visited_countries.length, | ||
error: "You haven't been to this country. Please try again." | ||
}); | ||
} else { | ||
res.redirect("/"); | ||
} | ||
} catch(e) { | ||
console.log("Error with querying country_code", e.stack); | ||
var visited_countries = await get_visited_countries(); | ||
res.render("index.ejs", { | ||
countries: visited_countries, | ||
total: visited_countries.length, | ||
error: "This country does not exist. Try again." | ||
}); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
async function insert_visited_country(country_code) { | ||
try { | ||
const result = await db.query("INSERT INTO visited_countries (country_code) VALUES ($1)", [country_code]); | ||
return null; | ||
} catch (e){ | ||
console.log("Error with inserting to visited_countries table", e.stack) | ||
return e; | ||
} | ||
} | ||
|
||
async function remove_visited_country(country_code) { | ||
try { | ||
const result = await db.query("DELETE FROM visited_countries WHERE country_code=$1", [country_code]); | ||
return null; | ||
} catch(e) { | ||
console.log("Error with deleting a country code."); | ||
return e; | ||
} | ||
} | ||
|
||
function capitalize(str) { | ||
if(str){ | ||
const st_letter = str[0].toUpperCase(); | ||
const remaining = str.slice(1).toLowerCase(); | ||
|
||
return st_letter + remaining; | ||
|
||
} else { | ||
return "" | ||
} | ||
|
||
} | ||
|
||
async function get_visited_countries() { | ||
try { | ||
const result = await db.query("SELECT country_code FROM visited_countries"); | ||
return result.rows.map((obj) => obj.country_code); | ||
} catch (e) { | ||
console.log("Error with querying data", e.stack) | ||
return []; | ||
} | ||
} | ||
|
||
app.listen(port, () => { | ||
console.log(`Server running on http://localhost:${port}`); | ||
}); |
Oops, something went wrong.