This repository has been archived by the owner on Apr 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* modified event and attendee models to handle checkin/tardiness * wrote attendee endpoints * forgot yarn format again * fixing stuff
- Loading branch information
Showing
7 changed files
with
143 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
const express = require('express') | ||
const mongodb = require('mongodb') | ||
const router = express.Router() | ||
const { errorWrap } = require('../middleware') | ||
const { Attendee, Candidate } = require('../models') | ||
|
||
// get all attendees | ||
router.get( | ||
'/', | ||
errorWrap(async (req, res) => { | ||
const attendees = await Attendee.find({}) | ||
res.json({ | ||
code: 200, | ||
result: attendees, | ||
success: true | ||
}) | ||
}) | ||
) | ||
|
||
// get one attendee | ||
router.get( | ||
'/:attendeeId', | ||
errorWrap(async (req, res) => { | ||
const attendeeId = req.params.attendeeId | ||
const attendee = await Attendee.findById(attendeeId) | ||
res.json({ | ||
code: 200, | ||
result: attendee, | ||
success: true | ||
}) | ||
}) | ||
) | ||
|
||
// create a new attendee | ||
router.post( | ||
'/', | ||
errorWrap(async (req, res) => { | ||
const data = req.body | ||
const newAttendee = new Attendee({ | ||
name: data.name, | ||
email: data.email, | ||
year: data.year | ||
}) | ||
|
||
await newAttendee.save() | ||
res.json({ | ||
code: 200, | ||
message: 'Attendee Successfully Created', | ||
success: true | ||
}) | ||
}) | ||
) | ||
|
||
// update an attendee | ||
router.put( | ||
'/:attendeeId', | ||
errorWrap(async (req, res) => { | ||
const data = req.body | ||
const attendeeId = req.params.attendeeId | ||
let fieldsToUpdate = {} | ||
|
||
if (data.name !== undefined) { | ||
fieldsToUpdate['name'] = data.name | ||
} | ||
if (data.email !== undefined) { | ||
fieldsToUpdate['email'] = data.email | ||
} | ||
if (data.year !== undefined) { | ||
fieldsToUpdate['year'] = data.year | ||
} | ||
|
||
const attendee = await Attendee.findByIdAndUpdate( | ||
attendeeId, | ||
{ $set: fieldsToUpdate }, | ||
{ new: true } | ||
) | ||
const ret = attendee | ||
? { | ||
code: 200, | ||
message: 'Attendee Updated Successfully', | ||
success: true | ||
} | ||
: { | ||
code: 404, | ||
message: 'Attendee Not Found', | ||
success: false | ||
} | ||
res.json(ret) | ||
}) | ||
) | ||
|
||
// delete an attendee | ||
router.delete( | ||
'/:attendeeId', | ||
errorWrap(async (req, res) => { | ||
const attendeeId = req.params.attendeeId | ||
const attendee = await Attendee.findByIdAndRemove(attendeeId) | ||
const ret = attendee | ||
? { | ||
code: 200, | ||
message: 'Attendee Deleted Successfully', | ||
success: true | ||
} | ||
: { | ||
code: 404, | ||
message: 'Attendee Not Found', | ||
success: false | ||
} | ||
res.json(ret) | ||
}) | ||
) | ||
|
||
// delete all events | ||
router.delete( | ||
'/', | ||
errorWrap(async (req, res) => { | ||
await Attendee.deleteMany({}) | ||
res.json({ | ||
code: 200, | ||
message: 'Attendees Deleted Successfully', | ||
success: true | ||
}) | ||
}) | ||
) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters