Skip to content

Commit

Permalink
Add listing Bug Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dipexplorer committed Oct 15, 2024
1 parent d0f6cac commit 3440e22
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 33 deletions.
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

0 comments on commit 3440e22

Please sign in to comment.