Load listing details from the server

This commit is contained in:
Edin Dazdarevic
2017-04-08 00:06:06 +02:00
parent a1e4a35d17
commit ffdf1d36b3
6 changed files with 188 additions and 42 deletions

View File

@@ -14,6 +14,25 @@ const AGENTURA_KEY = process.env.AGENTURA_KEY || '1somethingverysecret';
let db;
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);
}
});
router.get('/search/listings', async (req, res, next) => {
try {
const bounds = req.query.bounds || '';
@@ -126,7 +145,9 @@ router.get('/search/listings', async (req, res, next) => {
"sort": [['price','asc']]
});
if (pins !== "true") {
const isPins = pins === "true";
if (!isPins) {
all = await all.skip(20 * page).limit(20).toArray();
} else {
all = await all.toArray();
@@ -136,7 +157,32 @@ router.get('/search/listings', async (req, res, next) => {
res.header('X-Last-Record-Id', [...all].pop()._id);
}
res.json(all);
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,
time
})));
}
res.end();
} catch (e) {
console.log('error:', e);