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

@@ -79,7 +79,7 @@
router.get('/search', function () {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(req, res, next) {
var bounds, minPrice, maxPrice, minSize, maxSize, rooms, adType, properties, query, _bounds$split$map, _bounds$split$map2, lat1, lng1, lat2, lng2, box, all;
var bounds, minPrice, maxPrice, minSize, maxSize, rooms, adType, properties, query, _bounds$split$map, _bounds$split$map2, lat1, lng1, lat2, lng2, box, price, allRooms, or, size, all;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
@@ -117,77 +117,85 @@
});
}
if (minPrice) {
if (minPrice || maxPrice) {
price = {};
if (minPrice) {
price["$gte"] = parseFloat(minPrice);
}
if (maxPrice) {
price["$lte"] = parseFloat(maxPrice);
}
query = Object.assign(query, {
price: {
"$gte": parseFloat(minPrice),
"$ne": -1
}
price: price
});
}
if (maxPrice) {
query = Object.assign(query, {
price: {
"$lte": parseFloat(maxPrice),
"$ne": -1
if (rooms) {
allRooms = rooms.split(',');
or = allRooms.map(function (val) {
if (val === '4+') {
return {
rooms: {
"$gte": 4
}
};
}
return {
rooms: parseFloat(val)
};
});
query = Object.assign(query, {
"$or": or
});
}
if (rooms === "4+") {
if (minSize || maxSize) {
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)
size: size
});
}
if (minSize) {
query = Object.assign(query, {
size: {
"$gte": parseFloat(minSize)
}
});
}
if (maxSize) {
query = Object.assign(query, {
size: {
"$lte": parseFloat(maxSize)
}
});
}
_context.next = 19;
console.log('QUERY: ', query);
_context.next = 18;
return properties.find(query).toArray();
case 19:
case 18:
all = _context.sent;
res.json(all);
res.end();
_context.next = 28;
_context.next = 27;
break;
case 24:
_context.prev = 24;
case 23:
_context.prev = 23;
_context.t0 = _context['catch'](0);
console.log('error:', _context.t0);
next(_context.t0);
case 28:
case 27:
case 'end':
return _context.stop();
}
}
}, _callee, undefined, [[0, 24]]);
}, _callee, undefined, [[0, 23]]);
}));
return function (_x, _x2, _x3) {

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);