Skip to content

Instantly share code, notes, and snippets.

@vidomarkas
Created July 12, 2020 15:13
Show Gist options
  • Save vidomarkas/9f422a95b0289602b261881359f70d79 to your computer and use it in GitHub Desktop.
Save vidomarkas/9f422a95b0289602b261881359f70d79 to your computer and use it in GitHub Desktop.
const express = require("express");
const router = express.Router();
const Ad = require("../models/Ad");
// Route POST api/getgroup
// Description Search all ads with criteria
// Access Public
router.post("/", async (req, res) => {
const type = req.body.type;
console.log("type", type);
try {
let result;
switch (type) {
case "popular":
result = await Ad.find({})
.sort({
seenCount: 1,
})
.limit(15);
case "new":
result = await Ad.find({ mileage: { $lte: 500 } })
.sort({
mileage: -1,
})
.limit(15);
case "expensive":
result = await Ad.find({})
.sort({
price: -1,
})
.limit(15);
default:
result = await Ad.find({ featured: true })
.sort({
date: -1,
})
.limit(15);
}
res.json(result);
} catch (error) {
console.error(error.message);
res.status(500).json("Server error");
}
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment