/******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ // Flag the module as loaded /******/ module.loaded = true; /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); var _express = __webpack_require__(1); var _express2 = _interopRequireDefault(_express); var _bodyParser = __webpack_require__(2); var _bodyParser2 = _interopRequireDefault(_bodyParser); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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/kivi'; __webpack_require__(4); var router = _express2.default.Router({ mergeParams: true }); var PORT = process.env.PORT || 3001; var AGENTURA_KEY = process.env.AGENTURA_KEY || '1somethingverysecret'; var db = void 0; router.get('/search', 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; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.prev = 0; bounds = req.query.bounds || ''; minPrice = req.query.minPrice; maxPrice = req.query.maxPrice; minSize = req.query.minSize; maxSize = req.query.maxSize; rooms = req.query.rooms; adType = req.query.adType; category = req.query.category; sort = req.query.sort; properties = db.collection('listings'); query = {}; if (bounds) { _bounds$split$map = bounds.split(',').map(parseFloat), _bounds$split$map2 = _slicedToArray(_bounds$split$map, 4), lat1 = _bounds$split$map2[0], lng1 = _bounds$split$map2[1], lat2 = _bounds$split$map2[2], lng2 = _bounds$split$map2[3]; box = [[lat1, lng1], [lat2, lng2]]; query = Object.assign(query, { loc: { "$geoWithin": { "$box": box } } }); } if (adType) { query = Object.assign(query, { adType: parseInt(adType) }); } if (minPrice || maxPrice) { price = {}; if (minPrice) { price["$gte"] = parseFloat(minPrice); } if (maxPrice) { price["$lte"] = parseFloat(maxPrice); } query = Object.assign(query, { price: price }); } and = []; if (rooms) { allRooms = rooms.split(','); or = allRooms.map(function (val) { if (val === '4+') { return { rooms: { "$gte": 4 } }; } return { rooms: parseFloat(val) }; }); and.push({ "$or": or }); //query = Object.assign(query, { //rooms: //}); } if (minSize || maxSize) { size = {}; if (minSize) { size["$gte"] = parseFloat(minSize); } if (maxSize) { size["$lte"] = parseFloat(maxSize); } query = Object.assign(query, { size: size }); } if (category) { allCategories = category.split(','); _or = allCategories.map(function (val) { return { category: parseInt(val) }; }); and.push({ "$or": _or }); //query = Object.assign(query, { //{"$or" : or } //}); } if (and.length > 0) { query = Object.assign(query, { "$and": and }); } console.log('QUERY: ', query); _context.next = 23; return properties.find(query, { //"sort": [['field1','asc'], ['field2','desc']] "sort": [['price', 'asc']] }).toArray(); case 23: all = _context.sent; res.json(all); res.end(); _context.next = 32; break; case 28: _context.prev = 28; _context.t0 = _context['catch'](0); console.log('error:', _context.t0); next(_context.t0); case 32: case 'end': return _context.stop(); } } }, _callee, undefined, [[0, 28]]); })); return function (_x, _x2, _x3) { return _ref.apply(this, arguments); }; }()); var app = (0, _express2.default)(); app.use(_bodyParser2.default.json()); 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-Methods", "GET, POST, OPTIONS"); res.header('Access-Control-Allow-Credentials', 'true'); next(); }); app.use('/api', router); 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); }); }); /***/ }, /* 1 */ /***/ function(module, exports) { module.exports = require("express"); /***/ }, /* 2 */ /***/ function(module, exports) { module.exports = require("body-parser"); /***/ }, /* 3 */ /***/ function(module, exports) { module.exports = require("mongodb"); /***/ }, /* 4 */ /***/ function(module, exports) { module.exports = require("babel-polyfill"); /***/ } /******/ ]);