Skip to content

Commit

Permalink
safeguarded the new workout
Browse files Browse the repository at this point in the history
  • Loading branch information
pik-nik committed Mar 17, 2023
1 parent 9d5d756 commit 57d39ab
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
9 changes: 6 additions & 3 deletions controllers/user_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ router.get("/users/workouts", ensureLoggedIn, (req, res) => {
router.get("/users/:userid/workouts", ensureLoggedIn, (req, res) => {
const sql = "SELECT *, TO_CHAR(workout_date, 'FMMonth DD, YYYY') FROM workouts WHERE user_id = $1 ORDER BY workout_date DESC;"
// console.log(req.session.userId, "session userId");
db.query(sql, [req.session.userId], (err, dbRes) => {
const currentUserSessionId = req.session.userId
const workoutUserId = req.params.userid
db.query(sql, [workoutUserId], (err, dbRes) => {
const workouts = dbRes.rows
// console.log("workouts",workouts);
const sql2 = "SELECT * FROM workout_exercise_junction JOIN exercises ON workout_exercise_junction.exercise_id = exercises.exercise_id;"
Expand All @@ -55,7 +57,7 @@ router.get("/users/:userid/workouts", ensureLoggedIn, (req, res) => {
const sql3 = "SELECT * FROM users where user_id = $1"
db.query(sql3, [req.params.userid], (err, dbUsersRes) => {
const user = dbUsersRes.rows[0]
res.render("user_workouts", { workouts, exercisesInWorkouts, user })
res.render("user_workouts", { workouts, exercisesInWorkouts, user, currentUserSessionId })

})

Expand Down Expand Up @@ -122,7 +124,8 @@ router.post("/users", (req, res) => {
const sql2 = "INSERT INTO users (email, username, full_name, password_digest, profile_photo) VALUES ($1, $2, $3, $4, $5) RETURNING user_id;"
db.query(sql2, [email, username, req.body.full_name, digestedPassword, default_profile_picture_url], (err, insertRes) => {
req.session.userId = insertRes.rows[0].user_id
res.redirect("/")

res.redirect(`/users/${insertRes.rows[0].user_id}`)
})
})
})
Expand Down
Binary file modified latest.dump
Binary file not shown.
13 changes: 8 additions & 5 deletions views/user_workouts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
</head>
<body>
<h2><span><%= user.username%></span>'s Workouts</h2>
<section class="nav">
<h3><a href="/workouts/new">Start New Workout</a></h3>
<!-- add search function to look for old workout -->
<h3>Find a Workout</h3>
</section>

<% if (Number(currentUserSessionId) === user.user_id) { %>
<section class="nav">
<h3><a href="/workouts/new">Start New Workout</a></h3>
<!-- add search function to look for old workout -->
<h3>Find a Workout</h3>
</section>
<% } %>
<section class="log">
<h4>Workout Log</h4>
Expand Down

0 comments on commit 57d39ab

Please sign in to comment.