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

Add listing Bug Fix #99

Merged
merged 1 commit into from
Oct 15, 2024
Merged
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
14 changes: 12 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,20 @@ app.route("/login")
res.redirect("/listing");

})
})
});

//define listing conroller
//BUG FIX
const listingController = require('./controllers/listing.js');

// Create new listing form route
// app.get("/new",isLoggedIn, asyncwrap(newpost));
app.get("/listing/new", isLoggedIn, asyncwrap(listingController.newpost));

//create new listing
app.get("/new",isLoggedIn, asyncwrap(newpost));
// Create new listing form route
app.get("/listing/new", isLoggedIn, asyncwrap(listingController.newpost));


//index route
app.get("/listing",asyncwrap(index));
Expand Down
30 changes: 23 additions & 7 deletions controllers/listing.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ module.exports.index = async (req, res) => {
}
};

//bug fixes
module.exports.newpost = async (req, res) => {
console.log(req.user);
res.render("new.ejs");
};
try {
console.log("Rendering new listing form...");
res.render("new.ejs");
} catch (err) {
console.error("Error loading new listing form:", err);
req.flash("error", "Error loading form.");
return res.redirect("/listing");
}
};


module.exports.search = async (req, res) => {
const { query } = req.body;
Expand Down Expand Up @@ -118,9 +126,18 @@ module.exports.editpost = async (req, res) => {
}
};

const mongoose = require('mongoose');

module.exports.showPost = async (req, res) => {
try {
const { id } = req.params;

// Validate if 'id' is a valid ObjectId
if (!mongoose.Types.ObjectId.isValid(id)) {
req.flash('error', 'Invalid listing ID');
return res.redirect('/listing');
}

const list = await listing.findById(id)
.populate({
path: 'reviews',
Expand All @@ -130,21 +147,20 @@ module.exports.showPost = async (req, res) => {
})
.populate('owner');

// console.log(list);

if (!list) {
req.flash('error', ERROR_LISTING_NOT_FOUND);
return res.redirect('/listing');
}

res.render('show.ejs', { list });
} catch (err) {
console.error("Error fetching listing:", err);
console.error("Error fetching listing:", err.message);
console.error("Stack trace:", err.stack);
req.flash("error", ERROR_LOAD_LISTING_DETAILS);
return res.redirect("/listing");
}
};


module.exports.saveEditpost = async (req, res) => {
const { id } = req.params;
console.log("saveditpost");
Expand Down
48 changes: 25 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion views/new.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<input name="listing[location]" class="form-control" placeholder="Enter location" required>
</div>
<br><br>
<button class="btn offset-6" name="saveButton" type="submit">ADD</button>
<button class="btn btn-outline-danger offset-6" name="saveButton" type="submit">ADD</button>
<br><br>
</form>
</div>
Expand Down
Loading