Skip to content

Commit

Permalink
added delete favourites route
Browse files Browse the repository at this point in the history
  • Loading branch information
Piyush-Bhor committed May 11, 2023
1 parent f77e5b0 commit 9e60c96
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
36 changes: 36 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,42 @@ app.get('/favourites', async (req,res) => {
res.render('favourites', pageData);
});

// delete favourite
app.get('/delete/:id',(req,res) => {
if(req.session.loggedIn) {
var username = req.session.username;
var recipe_id = req.params.id;

User.findOne({username: username}).then((user) => {
if(user){
for (i=0; i<user.favorites.length; i++) {
if (user.favorites[i] == recipe_id) {
user.favorites.splice(i,1);
user.save();
console.log(user.favorites);
break;
}
}
var pageData = {
delete_msg : "Deleted Successfully",
userInfo : user.username,
// dummy data
recipe_id : 36748,
recipe_title : "Ramen Noodle Coleslaw",
recipe_image : "https://spoonacular.com/recipeImages/Ramen-Noodle-Coleslaw-556177.jpg",
recipe_id : 12345
}
res.render('favourites', pageData);
}
}).catch((err) => {
res.send(err);
});
}
else {
res.redirect('/login');
}
});

// search page
app.get('/search', (req,res) => {
const recipeAPI = require('./api/recipe_random');
Expand Down
10 changes: 9 additions & 1 deletion views/favourites.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<!--grid of favourite recipes here-->
<!--use IDs in DB in the api request -->
<div class="recipe">
<a href="/add/<%=recipe_id%>">
<a href="/delete/<%=recipe_id%>">
<i class="fa-solid fa-star" id="<%=recipe_id%>" onclick="toggleFavourites(recipe_id)"></i>
</a>
<img src="<%= recipe_image %>" alt="food" />
Expand All @@ -70,6 +70,14 @@
</div>
</div>
</div>
<!-- delete message -->
<%
if(typeof(delete_msg) != 'undefined'){
%>
<br><br><div class="alert alert-success" role="alert"> <%= delete_msg %> </div>
<%
}
%>
</main>
<footer>
<div class="footer-container">
Expand Down

0 comments on commit 9e60c96

Please sign in to comment.