diff --git a/app/controllers/general/organization.controller.js b/app/controllers/general/organization.controller.js index 887553a..c55641d 100644 --- a/app/controllers/general/organization.controller.js +++ b/app/controllers/general/organization.controller.js @@ -14,3 +14,28 @@ exports.findAll = (req, res, next) => { }) .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)); +}; diff --git a/app/routes/general.route.js b/app/routes/general.route.js index 46fbd9a..8d8499d 100644 --- a/app/routes/general.route.js +++ b/app/routes/general.route.js @@ -12,4 +12,8 @@ router.get('/regions', controllers.region.findAll); // Retrieve all organizations 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;