Correctly parse rooms, size, price etc.

This commit is contained in:
Edin Dazdarevic
2017-04-04 02:16:22 +02:00
parent 80aded2fc3
commit a6c5ee80e8
7 changed files with 177 additions and 39 deletions

View File

@@ -61,7 +61,7 @@
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
var MongoClient = __webpack_require__(3).MongoClient;
var url = 'mongodb://localhost:27017/example';
var url = 'mongodb://localhost:27017/kivi';
__webpack_require__(4);
@@ -70,13 +70,16 @@
var PORT = process.env.PORT || 3001;
var AGENTURA_KEY = process.env.AGENTURA_KEY || '1somethingverysecret';
var db = void 0;
//Monogo = await MongoClient.connect(url);
// TODO:
// db.results.ensureIndex({loc:"2d"})
//collection.ensureIndex("username",callback)
router.get('/search', function () {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(req, res, next) {
var bounds, db, 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, all;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
@@ -84,12 +87,13 @@
case 0:
_context.prev = 0;
bounds = req.query.bounds || '';
_context.next = 4;
return MongoClient.connect(url);
case 4:
db = _context.sent;
properties = db.collection('results');
minPrice = req.query.minPrice;
maxPrice = req.query.maxPrice;
minSize = req.query.minSize;
maxSize = req.query.maxSize;
rooms = req.query.rooms;
adType = req.query.adType;
properties = db.collection('listings');
query = {};
@@ -107,35 +111,83 @@
});
}
_context.next = 10;
if (adType) {
query = Object.assign(query, {
adType: parseInt(adType)
});
}
if (minPrice) {
query = Object.assign(query, {
price: {
"$gte": parseFloat(minPrice),
"$ne": -1
}
});
}
if (maxPrice) {
query = Object.assign(query, {
price: {
"$lte": parseFloat(maxPrice),
"$ne": -1
}
});
}
if (rooms === "4+") {
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)
}
});
}
_context.next = 19;
return properties.find(query).toArray();
case 10:
case 19:
all = _context.sent;
res.json(all);
res.end();
_context.next = 15;
return db.close();
case 15:
_context.next = 21;
_context.next = 28;
break;
case 17:
_context.prev = 17;
case 24:
_context.prev = 24;
_context.t0 = _context['catch'](0);
console.log('error:', _context.t0);
next(_context.t0);
case 21:
case 28:
case 'end':
return _context.stop();
}
}
}, _callee, undefined, [[0, 17]]);
}, _callee, undefined, [[0, 24]]);
}));
return function (_x, _x2, _x3) {
@@ -155,8 +207,13 @@
});
app.use('/api', router);
app.listen(PORT, function () {
return console.log('Express server running at localhost: ' + PORT);
MongoClient.connect(url).then(function (database) {
db = database;
db.collection('listings').createIndex({ loc: "2d" });
app.listen(PORT, function () {
return console.log('Express server running at localhost: ' + PORT);
});
});
/***/ },

View File

@@ -1,7 +1,7 @@
import express from 'express'
import bodyParser from 'body-parser';
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/example';
var url = 'mongodb://localhost:27017/kivi';
require("babel-polyfill");
@@ -10,6 +10,9 @@ const router = express.Router({mergeParams: true})
const PORT = process.env.PORT || 3001;
const AGENTURA_KEY = process.env.AGENTURA_KEY || '1somethingverysecret';
let db;
//Monogo = await MongoClient.connect(url);
// TODO:
// db.results.ensureIndex({loc:"2d"})
//collection.ensureIndex("username",callback)
@@ -17,8 +20,13 @@ const AGENTURA_KEY = process.env.AGENTURA_KEY || '1somethingverysecret';
router.get('/search', async (req, res, next) => {
try {
const bounds = req.query.bounds || '';
const db = await MongoClient.connect(url);
const properties = db.collection('results');
const minPrice = req.query.minPrice;
const maxPrice = req.query.maxPrice;
const minSize = req.query.minSize;
const maxSize = req.query.maxSize;
const rooms = req.query.rooms;
const adType = req.query.adType;
const properties = db.collection('listings');
let query = {};
if (bounds) {
@@ -34,11 +42,62 @@ router.get('/search', async (req, res, next) => {
});
}
if (adType) {
query = Object.assign(query, {
adType: parseInt(adType)
});
}
if (minPrice) {
query = Object.assign(query, {
price: {
"$gte": parseFloat(minPrice),
"$ne": -1
}
});
}
if (maxPrice) {
query = Object.assign(query, {
price: {
"$lte": parseFloat(maxPrice),
"$ne": -1
}
});
}
if (rooms === "4+") {
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)
}
});
}
const all = await properties.find(query).toArray();
res.json(all);
res.end();
await db.close();
} catch (e) {
console.log('error:', e);
next(e);
@@ -58,5 +117,10 @@ app.use(function(req, res, next) {
});
app.use('/api', router);
app.listen(PORT, () => console.log('Express server running at localhost: ' + PORT));
MongoClient.connect(url).then((database) => {
db = database;
db.collection('listings').createIndex({loc: "2d"});
app.listen(PORT, () => console.log('Express server running at localhost: ' + PORT));
});