Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Family_Travel_Tracker #563

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Family_Travel_Tracker/assests/familytravel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Family_Travel_Tracker/assests/travel.mp4
Binary file not shown.
102 changes: 102 additions & 0 deletions Family_Travel_Tracker/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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: "Travel_Data",
password: "deepika@11",
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 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();
res.render("index.ejs", {
countries: countries,
total: countries.length,
users: users,
color: currentUser.color,
});
});
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, currentUserId]
);
res.redirect("/");
} catch (err) {
console.log(err);
}
} catch (err) {
console.log(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) => {
const name = req.body.name;
const color = req.body.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 http://localhost:${port}`);
});
Loading