Make price filtering work

This commit is contained in:
Edin Dazdarevic
2017-04-04 04:36:52 +02:00
parent 0af6337132
commit 49e9e2fdfb
6 changed files with 179 additions and 112 deletions

View File

@@ -48,52 +48,57 @@ router.get('/search', async (req, res, next) => {
});
}
if (minPrice) {
if (minPrice || maxPrice) {
const price = {}
if (minPrice) {
price["$gte"] = parseFloat(minPrice);
}
if (maxPrice) {
price["$lte"] = parseFloat(maxPrice);
}
query = Object.assign(query, {
price: {
"$gte": parseFloat(minPrice),
"$ne": -1
}
price
});
}
if (maxPrice) {
query = Object.assign(query, {
price: {
"$lte": parseFloat(maxPrice),
"$ne": -1
if (rooms) {
const allRooms = rooms.split(',');
const or = allRooms.map(val => {
if (val === '4+') {
return {
rooms: {
"$gte": 4
}
}
}
return {
rooms: parseFloat(val)
};
});
query = Object.assign(query, {
"$or": or
});
}
if (rooms === "4+") {
if (minSize || maxSize) {
const size = {}
if (minSize) {
size["$gte"] = parseFloat(minSize);
}
if (maxSize) {
size["$lte"] = parseFloat(maxSize);
}
query = Object.assign(query, {
rooms: {
"$gte": 4
}
})
} else if (rooms) {
query = Object.assign(query, {
rooms: parseFloat(rooms)
});
}
if (minSize) {
query = Object.assign(query, {
size: {
"$gte": parseFloat(minSize)
}
});
}
if (maxSize) {
query = Object.assign(query, {
size: {
"$lte": parseFloat(maxSize)
}
size
});
}
console.log('QUERY: ', query);
const all = await properties.find(query).toArray();
res.json(all);