finished route based on search

This commit is contained in:
Arjun Patel
2019-01-22 21:13:56 -08:00
parent 6e6eba3de4
commit 8c6f439f61
2 changed files with 29 additions and 0 deletions
@@ -14,3 +14,28 @@ exports.findAll = (req, res, next) => {
}) })
.catch(error => next(error)); .catch(error => next(error));
}; };
// FETCH depending on the given search
exports.searchOrgs = (req, res, next) => {
let { search } = req.params;
search = '%' + search + '%';
const limit = 10;
const QUERY = `SELECT * from organizations
WHERE name LIKE :search or
primary_cause LIKE :search or
primary_region LIKE :search
LIMIT :limit`;
db.sequelize
.query(QUERY, {
replacements: { search, limit },
type: db.Sequelize.QueryTypes.SELECT
})
.then(organizations => {
res.status(200).json({
organizations,
number_items: organizations.length
});
})
.catch(error => next(error));
};
+4
View File
@@ -12,4 +12,8 @@ router.get('/regions', controllers.region.findAll);
// Retrieve all organizations // Retrieve all organizations
router.get('/organizations', controllers.organization.findAll); router.get('/organizations', controllers.organization.findAll);
// Retrieve based on inputted search
// limitted numer returned; empty input still returns orgs
router.get('/organizations/:search', controllers.organization.searchOrgs);
module.exports = router; module.exports = router;