Skip to content

Commit

Permalink
Merge pull request #494 from Meetpidev/main
Browse files Browse the repository at this point in the history
ADDED LIKE/DISLIKE FEATURE..
Soujanya2004 authored Nov 9, 2024
2 parents 6268500 + 247bf79 commit 49be7fd
Showing 5 changed files with 47 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ const User=require("./models/user.js");
const { isLoggedIn, isAdmin } = require("./middlewares/middleware.js");
const {saveRedirectUrl}=require("./middlewares/middleware.js");
const {isOwner,isAuthor}=require("./middlewares/middleware.js");
const {index, newpost, createpost, editpost, saveEditpost,search, deletepost, showPost, signup}=require("./controllers/listing.js");
const {index, newpost, createpost, editpost, saveEditpost,search, deletepost, showPost, signup, likeListing }=require("./controllers/listing.js");
const { dashboard, showuser, deleteUser, deleteListing, viewIndividualListing, viewListingReview, adminListEditRender, adminSaveEditList } = require("./controllers/admin.js")
const { signupRender, siggnedUp, logout, forgotPassword, passwordResetLink, resetPasswordTokenGet, resetPasswordTokenPatch, updatePasswordGet, updatePasswordPost } = require("./controllers/user.js")
const { viewProfile, profileGet, profilePost } = require("./controllers/profile.js");
@@ -361,6 +361,7 @@ app.get("/listing/:id/edit", isLoggedIn, isOwner, asyncwrap(editpost));
app.put('/listing/:id', isLoggedIn, isOwner, upload.array('listing[image]', 10), asyncwrap(saveEditpost));
app.delete("/listing/:id", isLoggedIn, isOwner, asyncwrap(deletepost));
app.get("/listing/:id", asyncwrap(showPost));
app.post('/listing/:id/like', isLoggedIn, asyncwrap(listingController.likeListing));

// Feedback
app.post("/feedback", isLoggedIn, asyncwrap(feedbackController.feedbackPost));
28 changes: 28 additions & 0 deletions controllers/listing.js
Original file line number Diff line number Diff line change
@@ -315,4 +315,32 @@ module.exports.deletepost = async (req, res) => {
res.redirect("/listing");
};

module.exports.likeListing = async (req, res) => {
const { id } = req.params;
try {
const foundListing = await listing.findById(id);
if (!foundListing) {
req.flash('error', 'Listing not found');
return res.redirect('/listing');
}
const userId = req.user._id;
const hasLiked = foundListing.likedBy.includes(userId);

if (hasLiked) {
foundListing.likes -= 1;
foundListing.likedBy.pull(userId);
} else {
foundListing.likes += 1;
foundListing.likedBy.push(userId);
}

await foundListing.save();
req.flash('success', hasLiked ? 'Like removed!' : 'Listing liked successfully!');
return res.redirect(`/listing/${id}`);
} catch (err) {
console.error("Error liking/disliking listing:", err);
req.flash('error', 'An error occurred while liking/disliking the listing.');
return res.redirect(`/listing/${id}`);
}
};

7 changes: 6 additions & 1 deletion models/listing.js
Original file line number Diff line number Diff line change
@@ -40,6 +40,11 @@ const listingSchema = new Schema({
required: true
}
},
likes: {
type: Number,
default: 0,
},
likedBy: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
tags: [String] // New tags field
});

@@ -50,4 +55,4 @@ listingSchema.post("findOneAndDelete", async (listing)=>{
});

const listing = mongoose.model("listing", listingSchema);
module.exports = listing;
module.exports = listing;
1 change: 1 addition & 0 deletions views/index.ejs
Original file line number Diff line number Diff line change
@@ -130,6 +130,7 @@
<span class="price" data-base-price="<%= listing.price %>"><%= listing.price.toLocaleString("en-IN") %></span> / night
<span class="gst-label">(excl. GST)</span>
</p>
<p>❤️: <%= listing.likes %></p>
<p class="card-text location"><i class="fa-solid fa-location-crosshairs"></i>&nbsp; <%= listing.location %>, <%= listing.country %></p>
<!-- ADDING TAGS -->
<a href="/listing/<%= listing._id %>" class="btn btn-dark show_btn">Show in Detail</a><% listing.tags.forEach(tag => { %>
10 changes: 10 additions & 0 deletions views/show.ejs
Original file line number Diff line number Diff line change
@@ -282,6 +282,16 @@
Owned by: <b>Unknown User</b>
<% } %>
</p>
<p>Likes: <%= list.likes %></p>
<form action="/listing/<%= list._id %>/like" method="POST">
<button type="submit" class="btn btn-light">
<% if (list.likedBy && list.likedBy.includes(currUser._id)) { %>
❤️ <!-- Filled heart for liked -->
<% } else { %>
🤍 <!-- Empty heart for not liked -->
<% } %>
</button>
</form>
</div>
<div>
<form method="get" action="/listing/<%= list._id %>/booking" class="d-inline">

0 comments on commit 49be7fd

Please sign in to comment.