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

@@ -1,6 +1,8 @@
import express from 'express'
import bodyParser from 'body-parser';
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
var url = 'mongodb://localhost:27017/kivi';
require("babel-polyfill");
@@ -12,17 +14,18 @@ const AGENTURA_KEY = process.env.AGENTURA_KEY || '1somethingverysecret';
let db;
router.get('/search', async (req, res, next) => {
router.get('/search/listings', async (req, res, next) => {
try {
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;
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;
const lastRecordId = req.query.lastRecordId;
const properties = db.collection('listings');
let query = {};
@@ -111,11 +114,31 @@ router.get('/search', async (req, res, next) => {
});
}
//query = Object.assign(query, {
//"_id": 1
//});
console.log('QUERY: ', query);
const cnt = await properties.find(query).count();
res.header('X-Total-Count', cnt);
if (lastRecordId) {
query = Object.assign(query, {
"_id": {
"$gt": new ObjectID(lastRecordId)
}
});
}
const all = await properties.find(query, {
//"sort": [['field1','asc'], ['field2','desc']]
"sort": [['price','asc']]
}).toArray();
}).limit(20).toArray();
if (all.length > 0) {
res.header('X-Last-Record-Id', [...all].pop()._id);
}
res.json(all);
res.end();
@@ -131,7 +154,8 @@ 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-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();