From 36405327b96f8ea1169150b5d70dbd55a8440194 Mon Sep 17 00:00:00 2001 From: Michael Tran Date: Mon, 19 Aug 2019 14:12:39 -0700 Subject: [PATCH 1/2] feat(index.js, RSVPModel.js, rsvp.js): add rsvp model and routes add rsvp model and routes to create and get an authenticated user's rsvp --- src/index.js | 2 ++ src/models/RSVPModel.js | 57 +++++++++++++++++++++++++++++++++++++++++ src/routers/rsvp.js | 33 ++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 src/models/RSVPModel.js create mode 100644 src/routers/rsvp.js diff --git a/src/index.js b/src/index.js index 3a2823f..a7da2c7 100644 --- a/src/index.js +++ b/src/index.js @@ -4,6 +4,7 @@ const userRouter = require('./routers/user') const applicationRouter = require('./routers/application') const emailRequestRouter = require('./routers/emailRequest') const resumeRouter = require('./routers/resume') +const rsvpRouter = require('./routers/rsvp') const forgotPasswordRequestRouter = require('./routers/forgotPasswordRequest') const morgan = require('morgan') const app = express() @@ -16,6 +17,7 @@ app.use(applicationRouter) app.use(emailRequestRouter) app.use(forgotPasswordRequestRouter) app.use(resumeRouter) +app.use(rsvpRouter) app.listen(port, () => { console.log('Server is up on port ' + port) diff --git a/src/models/RSVPModel.js b/src/models/RSVPModel.js new file mode 100644 index 0000000..d66ff99 --- /dev/null +++ b/src/models/RSVPModel.js @@ -0,0 +1,57 @@ +const mongoose = require('mongoose') + +const RSVP = mongoose.model('RSVP', { + owner: { + type: mongoose.Schema.Types.ObjectId, + required: true + }, + attending: { + type: Boolean, + required: true + }, + shirt: { + type: Number, + min: 0, + max: 4, + required: function() {return this.attending} + + }, + travelType: { + type: Number, + min: 0, + max: 2, + required: function() {return this.attending} + }, + bus: { + type: Number, + min: 0, + max: 2, + required: function() {return this.attending && this.travelType === 0} + }, + socal: { + type: Number, + min: 0, + max: 2, + required: function() {return this.attending && this.bus === 0} + }, + norcal: { + type: Number, + min: 0, + max: 2, + required: function() {return this.attending && this.bus === 1} + + }, + flight: { + type: String, + required: function() {return this.attending && this.travelType === 1} + }, + misc: { + type: String + }, + submittedAt: { + type: Date, + default: Date.now + } +}) + +module.exports = RSVP diff --git a/src/routers/rsvp.js b/src/routers/rsvp.js new file mode 100644 index 0000000..9c2819e --- /dev/null +++ b/src/routers/rsvp.js @@ -0,0 +1,33 @@ +const express = require('express') +const RSVP = require('../models/RSVPModel') +const router = new express.Router() +const authMiddleware = require('../middleware/authMiddleware') + +router.post('/rsvp', authMiddleware, async (req, res) => { + try { + const rsvp = new RSVP(req.body); + rsvp.owner = req.user._id + const existingRSVP = await RSVP.findOne({ owner: req.user._id }) + if (existingRSVP) { + throw new Error('This user has already RSVP\'d') + } + await rsvp.save() + res.status(201).send(rsvp) + } catch (err) { + res.status(400).send({ errorMessage: err.message }) + } +}) + +router.get('/rsvp/', authMiddleware, async (req, res) => { + try { + const rsvp = await RSVP.findOne({ owner: req.user._id }) + if (!rsvp) { + res.status(404).send({}) + } + res.status(200).send(rsvp) + } catch (err) { + res.status(500).send({ errorMessage: err.message }) + } +}) + +module.exports = router \ No newline at end of file From 409d6124cc0cbb2d91dd7bbc2c94185da083bf49 Mon Sep 17 00:00:00 2001 From: Michael Tran Date: Mon, 19 Aug 2019 14:31:26 -0700 Subject: [PATCH 2/2] style(rsvp.js, RSVPModel.js): fixed lint errors --- src/models/RSVPModel.js | 14 +++++++------- src/routers/rsvp.js | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/models/RSVPModel.js b/src/models/RSVPModel.js index d66ff99..37c6945 100644 --- a/src/models/RSVPModel.js +++ b/src/models/RSVPModel.js @@ -13,37 +13,37 @@ const RSVP = mongoose.model('RSVP', { type: Number, min: 0, max: 4, - required: function() {return this.attending} + required: function () { return this.attending } }, travelType: { type: Number, min: 0, max: 2, - required: function() {return this.attending} + required: function () { return this.attending } }, bus: { type: Number, min: 0, max: 2, - required: function() {return this.attending && this.travelType === 0} + required: function () { return this.attending && this.travelType === 0 } }, socal: { type: Number, min: 0, max: 2, - required: function() {return this.attending && this.bus === 0} + required: function () { return this.attending && this.bus === 0 } }, norcal: { type: Number, min: 0, max: 2, - required: function() {return this.attending && this.bus === 1} - + required: function () { return this.attending && this.bus === 1 } + }, flight: { type: String, - required: function() {return this.attending && this.travelType === 1} + required: function () { return this.attending && this.travelType === 1 } }, misc: { type: String diff --git a/src/routers/rsvp.js b/src/routers/rsvp.js index 9c2819e..94ef893 100644 --- a/src/routers/rsvp.js +++ b/src/routers/rsvp.js @@ -5,7 +5,7 @@ const authMiddleware = require('../middleware/authMiddleware') router.post('/rsvp', authMiddleware, async (req, res) => { try { - const rsvp = new RSVP(req.body); + const rsvp = new RSVP(req.body) rsvp.owner = req.user._id const existingRSVP = await RSVP.findOne({ owner: req.user._id }) if (existingRSVP) { @@ -30,4 +30,4 @@ router.get('/rsvp/', authMiddleware, async (req, res) => { } }) -module.exports = router \ No newline at end of file +module.exports = router