diff --git a/app/controllers/organization.controller.js b/app/controllers/organization.controller.js index c2510d7..ec71770 100644 --- a/app/controllers/organization.controller.js +++ b/app/controllers/organization.controller.js @@ -186,8 +186,8 @@ exports.getAllOrganizations = async (req, res, next) => { select o.*, p.id as project_id, - p.title, - p.description, + p.title as project_title, + p.description as project_description, p.isComplete from organizations as o left join ( @@ -235,3 +235,34 @@ exports.searchOrganizations = (req, res, next) => { }) .catch(error => next(error)); }; + +exports.getOrganizationById = async (req, res, next) => { + const { charityId } = req.params; + + const QUERY = ` + select + o.*, + p.id as project_id, + p.title as project_title, + p.description as project_description, + p.isComplete + from organizations as o + left join ( + SELECT * from projects + where id in ( + select + max(id) + from projects + group by organization_id + ) + ) + as p on p.organization_id = o.id + WHERE o.id = :charityId + `; + const org = await db.sequelize.query(QUERY, { + replacements: { charityId }, + type: db.Sequelize.QueryTypes.SELECT + }); + + return res.status(200).json(org[0]); +}; diff --git a/app/routes/public.route.js b/app/routes/public.route.js index 31f88a0..81d21bf 100644 --- a/app/routes/public.route.js +++ b/app/routes/public.route.js @@ -18,6 +18,9 @@ router.get('/regions', region.getAllRegions); // Retrieve all organizations router.get('/organizations', organization.getAllOrganizations); +// Retrieve organization by id +router.get('/organization/:charityId', organization.getOrganizationById); + // Retrieve charities based on inputted search // TODO: fix sql injection attack router.get('/organizations/:search', organization.searchOrganizations);