/******/ (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 _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); 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/listings/:id', function () { var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(req, res, next) { var id, listings, listing; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.prev = 0; id = req.params.id; listings = db.collection('listings'); _context.next = 5; return listings.findOne({ _id: new ObjectID(id) }); case 5: listing = _context.sent; if (listing) { res.json(listing); } else { res.status(404); } res.end(); _context.next = 14; break; case 10: _context.prev = 10; _context.t0 = _context['catch'](0); console.log('error:', _context.t0); next(_context.t0); case 14: case 'end': return _context.stop(); } } }, _callee, undefined, [[0, 10]]); })); return function (_x, _x2, _x3) { return _ref.apply(this, arguments); }; }()); router.get('/search/listings', function () { var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(req, res, next) { var bounds, minPrice, maxPrice, minSize, maxSize, rooms, adType, category, sort, page, pins, properties, query, _bounds$split$map, _bounds$split$map2, lat1, lng1, lat2, lng2, box, price, and, allRooms, or, size, allCategories, _or, cnt, all, isPins; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.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; page = req.query.page || 0; pins = req.query.pins || false; 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 }); } 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 }); } if (and.length > 0) { query = Object.assign(query, { "$and": and }); } console.log('QUERY: ', query); _context2.next = 25; return properties.find(query).count(); case 25: cnt = _context2.sent; res.header('X-Total-Count', cnt); all = properties.find(query, { //"sort": [['field1','asc'], ['field2','desc']] "sort": [['price', 'asc']] }); isPins = pins === "true"; if (isPins) { _context2.next = 35; break; } _context2.next = 32; return all.skip(20 * page).limit(20).toArray(); case 32: all = _context2.sent; _context2.next = 38; break; case 35: _context2.next = 37; return all.toArray(); case 37: all = _context2.sent; case 38: if (all.length > 0) { res.header('X-Last-Record-Id', [].concat(_toConsumableArray(all)).pop()._id); } if (isPins) { res.json(all.map(function (val) { return { _id: val._id, loc: val.loc }; })); } else { res.json(all.map(function (_ref3) { var _id = _ref3._id, address = _ref3.address, images = _ref3.images, price = _ref3.price, rooms = _ref3.rooms, size = _ref3.size, time = _ref3.time; return { _id: _id, address: address, images: [images[0]], price: price, rooms: rooms, size: size, time: time }; })); } res.end(); _context2.next = 47; break; case 43: _context2.prev = 43; _context2.t0 = _context2['catch'](0); console.log('error:', _context2.t0); next(_context2.t0); case 47: case 'end': return _context2.stop(); } } }, _callee2, undefined, [[0, 43]]); })); return function (_x4, _x5, _x6) { return _ref2.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, X-Last-Record-Id, X-Total-Count"); res.header("Access-Control-Expose-Headers", "X-Last-Record-Id, X-Total-Count"); 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"); /***/ } /******/ ]);