2017-03-31 14:31:56 +02:00
|
|
|
import express from 'express'
|
|
|
|
|
import bodyParser from 'body-parser';
|
|
|
|
|
var MongoClient = require('mongodb').MongoClient;
|
2017-04-04 02:16:22 +02:00
|
|
|
var url = 'mongodb://localhost:27017/kivi';
|
2017-03-31 14:31:56 +02:00
|
|
|
|
|
|
|
|
require("babel-polyfill");
|
|
|
|
|
|
|
|
|
|
const router = express.Router({mergeParams: true})
|
|
|
|
|
|
|
|
|
|
const PORT = process.env.PORT || 3001;
|
|
|
|
|
const AGENTURA_KEY = process.env.AGENTURA_KEY || '1somethingverysecret';
|
|
|
|
|
|
2017-04-04 02:16:22 +02:00
|
|
|
let db;
|
2017-03-31 14:31:56 +02:00
|
|
|
|
|
|
|
|
router.get('/search', async (req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
const bounds = req.query.bounds || '';
|
2017-04-04 02:16:22 +02:00
|
|
|
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;
|
2017-04-05 02:02:43 +02:00
|
|
|
const category = req.query.category;
|
|
|
|
|
const sort = req.query.sort;
|
2017-04-04 02:16:22 +02:00
|
|
|
const properties = db.collection('listings');
|
2017-03-31 14:31:56 +02:00
|
|
|
let query = {};
|
|
|
|
|
|
|
|
|
|
if (bounds) {
|
|
|
|
|
const [lat1, lng1, lat2, lng2] = bounds.split(',').map(parseFloat)
|
|
|
|
|
const box = [[lat1, lng1], [lat2, lng2]];
|
|
|
|
|
|
|
|
|
|
query = Object.assign(query, {
|
|
|
|
|
loc: {
|
|
|
|
|
"$geoWithin": {
|
|
|
|
|
"$box": box
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 02:16:22 +02:00
|
|
|
if (adType) {
|
|
|
|
|
query = Object.assign(query, {
|
|
|
|
|
adType: parseInt(adType)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 04:36:52 +02:00
|
|
|
if (minPrice || maxPrice) {
|
|
|
|
|
const price = {}
|
|
|
|
|
if (minPrice) {
|
|
|
|
|
price["$gte"] = parseFloat(minPrice);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (maxPrice) {
|
|
|
|
|
price["$lte"] = parseFloat(maxPrice);
|
|
|
|
|
}
|
2017-04-04 02:16:22 +02:00
|
|
|
|
|
|
|
|
query = Object.assign(query, {
|
2017-04-04 04:36:52 +02:00
|
|
|
price
|
2017-04-04 02:16:22 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-06 01:11:51 +02:00
|
|
|
const and = [];
|
2017-04-04 04:36:52 +02:00
|
|
|
if (rooms) {
|
|
|
|
|
const allRooms = rooms.split(',');
|
|
|
|
|
const or = allRooms.map(val => {
|
|
|
|
|
if (val === '4+') {
|
|
|
|
|
return {
|
|
|
|
|
rooms: {
|
|
|
|
|
"$gte": 4
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-04 02:16:22 +02:00
|
|
|
}
|
2017-04-04 04:36:52 +02:00
|
|
|
return {
|
|
|
|
|
rooms: parseFloat(val)
|
|
|
|
|
};
|
2017-04-04 02:16:22 +02:00
|
|
|
});
|
|
|
|
|
|
2017-04-06 01:11:51 +02:00
|
|
|
and.push({ "$or": or });
|
2017-04-04 02:16:22 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-04 04:36:52 +02:00
|
|
|
if (minSize || maxSize) {
|
|
|
|
|
const size = {}
|
|
|
|
|
if (minSize) {
|
|
|
|
|
size["$gte"] = parseFloat(minSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (maxSize) {
|
|
|
|
|
size["$lte"] = parseFloat(maxSize);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 02:16:22 +02:00
|
|
|
query = Object.assign(query, {
|
2017-04-04 04:36:52 +02:00
|
|
|
size
|
2017-04-04 02:16:22 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-05 02:02:43 +02:00
|
|
|
if (category) {
|
|
|
|
|
const allCategories = category.split(',');
|
|
|
|
|
const or = allCategories.map(val => {
|
|
|
|
|
return {
|
|
|
|
|
category: parseInt(val)
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2017-04-06 01:11:51 +02:00
|
|
|
and.push({ "$or": or });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (and.length > 0) {
|
2017-04-05 02:02:43 +02:00
|
|
|
query = Object.assign(query, {
|
2017-04-06 01:11:51 +02:00
|
|
|
"$and": and
|
2017-04-05 02:02:43 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 04:36:52 +02:00
|
|
|
console.log('QUERY: ', query);
|
2017-04-05 00:14:30 +02:00
|
|
|
const all = await properties.find(query, {
|
|
|
|
|
//"sort": [['field1','asc'], ['field2','desc']]
|
|
|
|
|
"sort": [['price','asc']]
|
|
|
|
|
}).toArray();
|
2017-03-31 14:31:56 +02:00
|
|
|
|
|
|
|
|
res.json(all);
|
|
|
|
|
res.end();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log('error:', e);
|
|
|
|
|
next(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const app = express()
|
|
|
|
|
app.use(bodyParser.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);
|
2017-04-04 02:16:22 +02:00
|
|
|
|
|
|
|
|
MongoClient.connect(url).then((database) => {
|
|
|
|
|
db = database;
|
|
|
|
|
db.collection('listings').createIndex({loc: "2d"});
|
|
|
|
|
app.listen(PORT, () => console.log('Express server running at localhost: ' + PORT));
|
|
|
|
|
});
|
2017-03-31 14:31:56 +02:00
|
|
|
|