2017-03-31 14:31:56 +02:00
|
|
|
import express from 'express'
|
|
|
|
|
import bodyParser from 'body-parser';
|
2017-04-08 02:11:54 +02:00
|
|
|
import distanceInWordsToNow from 'date-fns/distance_in_words_to_now';
|
|
|
|
|
var hr = require('date-fns/locale/hr');
|
|
|
|
|
|
2017-03-31 14:31:56 +02:00
|
|
|
var MongoClient = require('mongodb').MongoClient;
|
2017-04-07 04:02:01 +02:00
|
|
|
var ObjectID = require('mongodb').ObjectID;
|
|
|
|
|
|
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
|
|
|
|
2017-04-13 17:23:46 +02:00
|
|
|
router.post('/contact/:listingId', async (req, res, next) => {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const listingId = req.params.listingId;
|
|
|
|
|
const body = req.body;
|
|
|
|
|
|
|
|
|
|
const contactRequests = db.collection('contact_requests');
|
|
|
|
|
|
|
|
|
|
if (!body.email) {
|
|
|
|
|
res.status(422);
|
|
|
|
|
res.end('Email is required');
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!body.name) {
|
|
|
|
|
res.status(422);
|
|
|
|
|
res.end('Name is required');
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await contactRequests.insertOne({
|
|
|
|
|
name : body.name,
|
|
|
|
|
email : body.email,
|
|
|
|
|
listingId,
|
|
|
|
|
message : body.message,
|
|
|
|
|
phone : body.phone,
|
|
|
|
|
alert : body.alert
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.status(200);
|
|
|
|
|
res.end();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log('error:', e);
|
|
|
|
|
next(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2017-04-08 00:06:06 +02:00
|
|
|
router.get('/search/listings/:id', async (req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
const id = req.params.id;
|
|
|
|
|
|
|
|
|
|
const listings = db.collection('listings');
|
|
|
|
|
const listing = await listings.findOne({_id: new ObjectID(id)});
|
|
|
|
|
if (listing) {
|
|
|
|
|
res.json(listing);
|
|
|
|
|
} else {
|
|
|
|
|
res.status(404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.end();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log('error:', e);
|
|
|
|
|
next(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
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;
|
2017-04-07 19:40:52 +02:00
|
|
|
const pins = req.query.pins || false;
|
2017-04-07 04:50:46 +02:00
|
|
|
|
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-07 04:02:01 +02:00
|
|
|
const cnt = await properties.find(query).count();
|
|
|
|
|
|
|
|
|
|
res.header('X-Total-Count', cnt);
|
|
|
|
|
|
2017-04-08 02:11:54 +02:00
|
|
|
const getSort = () => {
|
|
|
|
|
if (sort === 'price-min') {
|
|
|
|
|
return [['price', 'asc']];
|
|
|
|
|
} else if (sort === 'price-max') {
|
|
|
|
|
return [['price', 'desc']];
|
|
|
|
|
} else if (sort === 'newest') {
|
|
|
|
|
return [['_id', 'desc']];
|
|
|
|
|
} else if (sort === 'relevance') {
|
|
|
|
|
// TODO: figure out what the relevance is
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-04-07 19:40:52 +02:00
|
|
|
let all = properties.find(query, {
|
2017-04-05 00:14:30 +02:00
|
|
|
//"sort": [['field1','asc'], ['field2','desc']]
|
2017-04-08 02:11:54 +02:00
|
|
|
"sort": getSort()
|
2017-04-07 19:40:52 +02:00
|
|
|
});
|
|
|
|
|
|
2017-04-08 00:06:06 +02:00
|
|
|
const isPins = pins === "true";
|
|
|
|
|
|
|
|
|
|
if (!isPins) {
|
2017-04-07 19:40:52 +02:00
|
|
|
all = await all.skip(20 * page).limit(20).toArray();
|
|
|
|
|
} else {
|
|
|
|
|
all = await all.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
|
|
|
|
2017-04-08 00:06:06 +02:00
|
|
|
if (isPins) {
|
|
|
|
|
res.json(all.map(val => {
|
|
|
|
|
return {
|
|
|
|
|
_id: val._id,
|
|
|
|
|
loc: val.loc
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
} else {
|
|
|
|
|
res.json(all.map(({_id,
|
|
|
|
|
address,
|
|
|
|
|
images,
|
|
|
|
|
price,
|
|
|
|
|
rooms,
|
|
|
|
|
size,
|
|
|
|
|
time
|
|
|
|
|
}) => ({
|
|
|
|
|
_id,
|
|
|
|
|
address,
|
|
|
|
|
images: [images[0]],
|
|
|
|
|
price,
|
|
|
|
|
rooms,
|
|
|
|
|
size,
|
2017-04-08 02:11:54 +02:00
|
|
|
time: distanceInWordsToNow(
|
|
|
|
|
new Date(time),
|
|
|
|
|
{locale: hr}
|
|
|
|
|
)
|
2017-04-08 00:06:06 +02:00
|
|
|
})));
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-31 14:31:56 +02:00
|
|
|
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);
|
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
|
|
|
|