Skip to content

Commit

Permalink
Created endpoint to get all volunteers and event count
Browse files Browse the repository at this point in the history
  • Loading branch information
brelieu05 committed Mar 16, 2024
1 parent a9476ba commit 36240fc
Showing 1 changed file with 36 additions and 17 deletions.
53 changes: 36 additions & 17 deletions routes/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,49 +129,50 @@ dataRouter.delete('/:id', async (req, res) => {
}
});

dataRouter.get('/volunteer/:volunteerId', async (req, res) => {
dataRouter.get('/volunteer/:volunteerId/event', async (req, res) => {
// retrive a list of event names associated with the volunteerID
try {
const { volunteerId } = req.params;
const volunteerData = await pool.query(
`SELECT *
`SELECT E.name
FROM event_data_new D
INNER JOIN events E ON D.event_id = E.id
WHERE D.volunteer_id = $1`,
[volunteerId],
);
res.status(200).json(volunteerData.rows);
const eventNames = volunteerData.rows.map((row) => row.name);
res.status(200).json(eventNames);
} catch (err) {
res.status(500).send(err.message);
}
});

dataRouter.get('/event/:eventId', async (req, res) => {
dataRouter.get('/volunteer/:volunteerId', async (req, res) => {
try {
const { eventId } = req.params;
const eventData = await pool.query(
const { volunteerId } = req.params;
const volunteerData = await pool.query(
`SELECT *
FROM event_data_new D
WHERE D.event_id = $1`,
[eventId],
WHERE D.volunteer_id = $1`,
[volunteerId],
);
res.status(200).json(eventData.rows);
res.status(200).json(volunteerData.rows);
} catch (err) {
res.status(500).send(err.message);
}
});

dataRouter.get('/volunteer/:volunteerId/event/:eventId', async (req, res) => {
dataRouter.get('/event/:eventId', async (req, res) => {
try {
const { volunteerId, eventId } = req.params;
const volAndEventData = await pool.query(
const { eventId } = req.params;
const eventData = await pool.query(
`SELECT *
FROM event_data_new D
WHERE D.event_id = $1 AND D.volunteer_id = $2
`,
[eventId, volunteerId],
WHERE D.event_id = $1`,
[eventId],
);
res.status(200).json(volAndEventData.rows);
res.status(200).json(eventData.rows);
} catch (err) {
// console.log(err);
res.status(500).send(err.message);
}
});
Expand Down Expand Up @@ -295,4 +296,22 @@ dataRouter.get('/image/:eventId', async (req, res) => {
}
});

// get all volunteers and how many events they are in
dataRouter.get('/allVolunteers/counts', async (req, res) => {
try {
const profileEventCount = await pool.query(
`SELECT DISTINCT Us.id, Us.image_url, Us.first_name, Us.last_name, U.total
FROM (
SELECT U.id, COUNT(E.volunteer_id) AS total
FROM event_data_new E, users U
WHERE E.volunteer_id = U.id
GROUP BY U.id
) as U, users as Us, event_data_new as E
WHERE U.id = Us.id AND U.id = E.volunteer_id
`);
res.status(200).json(profileEventCount.rows);
} catch (err) {
res.status(500).send(err.message);
}
});
module.exports = dataRouter;

0 comments on commit 36240fc

Please sign in to comment.