Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

endpoint to update an rsvp in the attendees table in the db #12

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions server/controllers/attendeeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,27 @@ attendeeController.addAttendee = (req, res, next) => {
}));
};

// on the frontend is where we would verify that an event already exists in the events table in the db
// we would check that the event object exists in the array of events that the user is rsvp-ed to before calling this endpoint to UPDATE vs CREATE
attendeeController.updateAttendee = (req, res, next) => {
const user_id = req.body.organizer.id;
const new_rsvp_level = Number(req.params.new_rsvp_level);
const event_id = req.body.id;

const queryStr = 'UPDATE attendees SET rsvp_level_id = $1 WHERE users_id = $2 AND events_id = $3 RETURNING *';
const args = [new_rsvp_level, user_id, event_id];
db.query(queryStr, args)
.then((data) => {
console.log('data: ', data);
const updatedAttendee = data.rows[0];
res.locals.updatedAttendee = updatedAttendee;
console.log('updatedAttendee: ', updatedAttendee);
return next();
})
.catch((error) => next({
log: 'Error in attendeeController.updateAttendee',
message: { err: error },
}));
};

module.exports = attendeeController;
9 changes: 8 additions & 1 deletion server/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ router.get(

// RSVP to an event - create an event if necessary, then add the user to attendees table and return the new row on the response object
router.post(
'/rsvp/:rsvp_level',
'/rsvp/:rsvp_level', // POST request to /api/rsvp/<rsvpID>
eventController.findEvent,
eventController.createEvent,
attendeeController.addAttendee,
Expand All @@ -76,6 +76,13 @@ router.post(
},
);

// update RSVP
router.put(
'/rsvp/:new_rsvp_level',
attendeeController.updateAttendee,
(req, res) => res.status(200).json(res.locals.updatedAttendee),
);

// Checks for active sessions
router.get(
'/sessions',
Expand Down