trying out

This commit is contained in:
Edin Dazdarevic
2017-04-07 04:02:01 +02:00
parent 6716dd3076
commit 5515774d17
6 changed files with 209 additions and 42 deletions

View File

@@ -58,9 +58,13 @@
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
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 ObjectID = __webpack_require__(3).ObjectID;
var url = 'mongodb://localhost:27017/kivi';
__webpack_require__(4);
@@ -72,9 +76,9 @@
var db = void 0;
router.get('/search', function () {
router.get('/search/listings', function () {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(req, res, next) {
var bounds, minPrice, maxPrice, minSize, maxSize, rooms, adType, category, sort, properties, query, _bounds$split$map, _bounds$split$map2, lat1, lng1, lat2, lng2, box, price, and, allRooms, or, size, allCategories, _or, all;
var bounds, minPrice, maxPrice, minSize, maxSize, rooms, adType, category, sort, lastRecordId, properties, query, _bounds$split$map, _bounds$split$map2, lat1, lng1, lat2, lng2, box, price, and, allRooms, or, size, allCategories, _or, cnt, all;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
@@ -90,6 +94,7 @@
adType = req.query.adType;
category = req.query.category;
sort = req.query.sort;
lastRecordId = req.query.lastRecordId;
properties = db.collection('listings');
query = {};
@@ -149,9 +154,6 @@
and.push({ "$or": or });
//query = Object.assign(query, {
//rooms:
//});
}
if (minSize || maxSize) {
@@ -180,9 +182,6 @@
and.push({ "$or": _or });
//query = Object.assign(query, {
//{"$or" : or }
//});
}
if (and.length > 0) {
@@ -191,35 +190,60 @@
});
}
//query = Object.assign(query, {
//"_id": 1
//});
console.log('QUERY: ', query);
_context.next = 23;
_context.next = 24;
return properties.find(query).count();
case 24:
cnt = _context.sent;
res.header('X-Total-Count', cnt);
if (lastRecordId) {
query = Object.assign(query, {
"_id": {
"$gt": new ObjectID(lastRecordId)
}
});
}
_context.next = 29;
return properties.find(query, {
//"sort": [['field1','asc'], ['field2','desc']]
"sort": [['price', 'asc']]
}).toArray();
}).limit(20).toArray();
case 23:
case 29:
all = _context.sent;
if (all.length > 0) {
res.header('X-Last-Record-Id', [].concat(_toConsumableArray(all)).pop()._id);
}
res.json(all);
res.end();
_context.next = 32;
_context.next = 39;
break;
case 28:
_context.prev = 28;
case 35:
_context.prev = 35;
_context.t0 = _context['catch'](0);
console.log('error:', _context.t0);
next(_context.t0);
case 32:
case 39:
case 'end':
return _context.stop();
}
}
}, _callee, undefined, [[0, 28]]);
}, _callee, undefined, [[0, 35]]);
}));
return function (_x, _x2, _x3) {
@@ -232,7 +256,8 @@
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Last-Record-Id");
res.header("Access-Control-Expose-Headers", "X-Last-Record-Id");
res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
res.header('Access-Control-Allow-Credentials', 'true');
next();