routeSearch
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
.env
|
.env
|
||||||
images/
|
images/
|
||||||
|
node_modules/
|
||||||
@@ -6,32 +6,69 @@ import restaurant from "../models/restaurant.js";
|
|||||||
|
|
||||||
var router = express.Router();
|
var router = express.Router();
|
||||||
|
|
||||||
|
// SEARCH RESTAURANTS BY NAME OR CITY
|
||||||
|
|
||||||
|
router.get("/", (req, res) => {
|
||||||
|
if (req.query.string.length > 0) {
|
||||||
|
const query = sanitizer.sanitize.keepUnicode(decodeURI(req.query.string));
|
||||||
|
const regex = new RegExp(query, "i");
|
||||||
|
|
||||||
|
Restaurant.find(
|
||||||
|
{
|
||||||
|
$and: [
|
||||||
|
{ $or: [{ city: { $regex: regex } }, { name: { $regex: regex } }] },
|
||||||
|
{ hidden: false },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
"_id name city imgUrl workingHours",
|
||||||
|
(err, results) => {
|
||||||
|
if (err) {
|
||||||
|
res.sendStatus(500);
|
||||||
|
} else {
|
||||||
|
res.send(results);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
res.send({
|
||||||
|
results: [],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// AUTOCOMPLETE
|
// AUTOCOMPLETE
|
||||||
|
|
||||||
router.get("/autocomplete/", (req, res) => {
|
router.get("/autocomplete/", (req, res) => {
|
||||||
var query = sanitizer.sanitize.keepUnicode(decodeURI(req.query.string));
|
if (req.query.string.length > 0) {
|
||||||
const regex = new RegExp(query, "i");
|
var query = sanitizer.sanitize.keepUnicode(decodeURI(req.query.string));
|
||||||
let cities = new Set();
|
const regex = new RegExp(query, "i");
|
||||||
let restaurants = new Set();
|
let cities = new Set();
|
||||||
|
let restaurants = new Set();
|
||||||
|
|
||||||
Restaurant.find(
|
Restaurant.find(
|
||||||
{ $or: [{ city: { $regex: regex } }, { name: { $regex: regex } }] },
|
{ $or: [{ city: { $regex: regex } }, { name: { $regex: regex } }] },
|
||||||
"name city",
|
"name city",
|
||||||
(err, doc) => {
|
(err, doc) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.send(404);
|
res.send(404);
|
||||||
} else {
|
} else {
|
||||||
doc.forEach((value) => {
|
doc.forEach((value) => {
|
||||||
cities.add(value.city);
|
cities.add(value.city);
|
||||||
restaurants.add(value.name);
|
restaurants.add(value.name);
|
||||||
});
|
});
|
||||||
res.send({
|
res.send({
|
||||||
cities: Array.from(cities),
|
cities: Array.from(cities),
|
||||||
restaurants: Array.from(restaurants),
|
restaurants: Array.from(restaurants),
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
).limit(5);
|
||||||
).limit(10);
|
} else {
|
||||||
|
res.send({
|
||||||
|
cities: [],
|
||||||
|
restaurants: [],
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user