finished route based on search
This commit is contained in:
@@ -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));
|
||||||
|
};
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user