rest endpoint for search/filtering
search/filter
by hamzeen hameem
March 01, 2017
HTML
REST API EndPoint for a Search Field
Action Bubbling: <br/>
https://medium.com/developer-paradise/ember-send-and-sendaction-5e6ac9c80841#.gds3dxmof<br/>
Actions: https://guides.emberjs.com/v2.3.0/components/handling-events/<br/>
links<pre>
{{#link-to 'about' class="button"}}
About Us
{{/link-to}}
</pre>
JavaScript
/*jshint node:true*/
module.exports = function(app) {
var express = require('express');
var dealsRouter = express.Router();
var rentals = [
{
type: 'rentals',
id: 1,
attributes: {
title: 'Grand Old Mansion',
owner: 'Veruca Salt',
city: 'San Francisco',
type: 'Estate',
bedrooms: 15,
image: 'https://upload.wikimedia.org/wikipedia/commons/c/cb/Crane_estate_(5).jpg'
}
}, {
type: 'rentals',
id: 2,
attributes: {
title: 'Urban Living',
owner: 'Mike Teavee',
city: 'Seattle',
type: 'Condo',
bedrooms: 1,
image: 'https://upload.wikimedia.org/wikipedia/commons/0/0e/Alfonso_13_Highrise_Tegucigalpa.jpg'
}
}, {
type: 'rentals',
id: 3,
attributes: {
title: 'Downtown Charm',
owner: 'Violet Beauregarde',
city: 'Portland',
type: 'Apartment',
bedrooms: 3,
image: 'https://upload.wikimedia.org/wikipedia/commons/f/f7/Wheeldon_Apartment_Building_-_Portland_Oregon.jpg'
}
}
];
dealsRouter.get('/', function(req, res) {
res.send({
rentals
});
});
dealsRouter.post('/', function(req, res) {
res.status(201).end();
});
/*dealsRouter.get('/:id', function(req, res) {
res.send({
'':req.params
});
});*/
dealsRouter.get('/:city', function(req, res) {
let val = req.params.city.split("=");
let filteredRentals = rentals.filter(function(i) {
return i.attributes.city.toLowerCase().indexOf(val[1].toLowerCase()) !== -1;
});
if(filteredRentals.length>0) {
res.send({
'data': filteredRentals
});
} else {
res.send({
'data': 'no records found'
});
}
});
dealsRouter.put('/:id', function(req, res) {
res.send({
'deals': {
id: req.params.id
}
});
...