-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3d8041
commit fff0f42
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const wrapAsync = require("../utils/wrapAsync.js"); | ||
const Listing = require("../models/listing.js"); | ||
const {isLoggedIn, isOwner, validateListing} = require("../middleware.js"); | ||
|
||
const listingController = require("../controllers/listings.js"); | ||
|
||
const multer = require('multer'); | ||
const {storage} = require("../cloudConfig.js"); | ||
const upload = multer({ storage }); | ||
|
||
router | ||
.route("/") | ||
.get(wrapAsync(listingController.index)) | ||
.post( | ||
isLoggedIn, | ||
|
||
upload.single("listing[image]"), | ||
validateListing, | ||
wrapAsync(listingController.createListing) | ||
); | ||
|
||
|
||
router.get('/search', async (req, res) => { | ||
try { | ||
const query = req.query.query; | ||
const listings = await Listing.find({ | ||
$or: [ | ||
{ title: new RegExp(query, 'i') }, // Case-insensitive search | ||
{ description: new RegExp(query, 'i') }, | ||
{ location: new RegExp(query, 'i') }, | ||
{ country: new RegExp(query, 'i') } | ||
] | ||
}); | ||
res.render('listings/index', { listings }); | ||
} catch (err) { | ||
req.flash('error', 'Something went wrong.'); | ||
res.redirect('/listings'); | ||
} | ||
}); | ||
|
||
|
||
//New Route | ||
router.get("/new", isLoggedIn, listingController.renderNewForm ); | ||
|
||
router.route("/:id") | ||
.get(wrapAsync(listingController.showListing)) | ||
.put( | ||
isLoggedIn, isOwner, | ||
upload.single("listing[image]"), | ||
validateListing, | ||
wrapAsync(listingController.updateListing)) | ||
.delete(isLoggedIn, isOwner, wrapAsync(listingController.destroyListing)); | ||
|
||
//Edit route | ||
router.get("/:id/edit", isLoggedIn, isOwner, wrapAsync(listingController.renderEditForm)); | ||
|
||
module.exports = router; |