Files
old-kivi/backend/server.js

173 lines
4.0 KiB
JavaScript
Raw Normal View History

2017-03-31 14:31:56 +02:00
import express from 'express'
import bodyParser from 'body-parser';
var MongoClient = require('mongodb').MongoClient;
2017-04-07 04:02:01 +02:00
var ObjectID = require('mongodb').ObjectID;
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';
let db;
2017-03-31 14:31:56 +02:00
2017-04-07 04:02:01 +02:00
router.get('/search/listings', async (req, res, next) => {
2017-03-31 14:31:56 +02:00
try {
2017-04-07 04:02:01 +02:00
const bounds = req.query.bounds || '';
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 category = req.query.category;
const sort = req.query.sort;
2017-04-07 04:50:46 +02:00
const page = req.query.page || 0;
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
}
}
});
}
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);
}
query = Object.assign(query, {
2017-04-04 04:36:52 +02:00
price
});
}
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 04:36:52 +02:00
return {
rooms: parseFloat(val)
};
});
2017-04-06 01:11:51 +02:00
and.push({ "$or": or });
}
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);
}
query = Object.assign(query, {
2017-04-04 04:36:52 +02:00
size
});
}
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-07 04:02:01 +02:00
//query = Object.assign(query, {
//"_id": 1
//});
2017-04-04 04:36:52 +02:00
console.log('QUERY: ', query);
2017-04-07 04:02:01 +02:00
const cnt = await properties.find(query).count();
res.header('X-Total-Count', cnt);
2017-04-07 04:50:46 +02:00
//if (lastRecordId) {
//query = Object.assign(query, {
//"_id": {
//"$gt": new ObjectID(lastRecordId)
//}
//});
//}
2017-04-07 04:02:01 +02:00
2017-04-05 00:14:30 +02:00
const all = await properties.find(query, {
//"sort": [['field1','asc'], ['field2','desc']]
"sort": [['price','asc']]
2017-04-07 04:50:46 +02:00
}).skip(20 * page).limit(20).toArray();
2017-04-07 04:02:01 +02:00
if (all.length > 0) {
res.header('X-Last-Record-Id', [...all].pop()._id);
}
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", "*");
2017-04-07 04:50:46 +02:00
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");
2017-03-31 14:31:56 +02:00
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((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