proper getting charity by id

This commit is contained in:
Arjun Patel
2019-04-04 01:45:53 -07:00
parent eed7d86c89
commit 27626a0fbf
2 changed files with 36 additions and 2 deletions
+33 -2
View File
@@ -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]);
};
+3
View File
@@ -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);