From aa91ad7cd7e73c40b16a8eaf899f228c41d6711a Mon Sep 17 00:00:00 2001 From: Rasik Date: Tue, 31 Jul 2018 12:25:47 +0530 Subject: [PATCH] added new fields --- app.js | 2 -- models/event.js | 36 +++++++++++++++++++------- models/profile.js | 20 ++++++--------- models/user.js | 9 ++----- routes/api/event_routes.js | 24 +++++++++++++++--- routes/api/profile_routes.js | 49 +++++++++++------------------------- routes/api/user_routes.js | 18 ++++++------- routes/organizer_routes.js | 2 +- 8 files changed, 80 insertions(+), 80 deletions(-) diff --git a/app.js b/app.js index 77ed48b..d47bed5 100644 --- a/app.js +++ b/app.js @@ -18,7 +18,6 @@ require('./config/passport-setup')(passport) /**************** TODO: -2. Image Storage 3. Keys protection -- heroku enviroment variables 5. Data Validations 7. Model Restructing // add fullpath @@ -33,7 +32,6 @@ const app = express(); const PORT = process.env.PORT || 3000; // mongoose connection -//mongoose.connect('mongodb://localhost:27017/univent', { useNewUrlParser: true }).then(() => console.log('mongoose running on DEV MACHINE')); mongoose.connect('mongodb://admin:admin123@ds231941.mlab.com:31941/univent' , { useNewUrlParser: true }).then(() => console.log('Database Connected')); mongoose.Promise = global.Promise; diff --git a/models/event.js b/models/event.js index 1836316..d0e1c9b 100644 --- a/models/event.js +++ b/models/event.js @@ -21,17 +21,23 @@ const eventSchema = mongoose.Schema({ type: String, required: true }, - society :{ + society : { type:String, required:true }, form_link:{ type: String, - require: true + required: true }, - cover_link:{ - type:String, - required:true + cover_photo:{ + link:{ + type: String, + required: true + }, + name:{ + type: String, + required: true + } }, number_of_participants :{ type:Number, @@ -40,14 +46,26 @@ const eventSchema = mongoose.Schema({ date : { type: Date, required: true, - //abhi k liye hai ye bs remember to remove - default : Date.now }, - prizes_worth: { + prize_description: { type: String, required: true + }, + tags:{ + type: [String], + required: true + }, + event_incharge:{ + name:{ + type:String, + required: true + }, + mobile_number:{ + type:Number, + required: true + } } - // contact org + contact mobile muber field + }); const Event = mongoose.model('event', eventSchema); diff --git a/models/profile.js b/models/profile.js index f2386e4..9f5d57b 100644 --- a/models/profile.js +++ b/models/profile.js @@ -7,25 +7,19 @@ const profileSchema = mongoose.Schema({ type: mongoose.Schema.Types.ObjectId, ref:'users' }, - username :{ + society_name : { type: String, - required: true, - max: 30 - }, - mobile_number :{ - type: String, - required:true, - // max:10 ? + required: true }, college:{ - type:String, - required:true - }, - field_of_study:{ type: String, required: true }, - year:{ + president_name :{ + type:String, + required :true + }, + mobile_number:{ type: Number, required: true } diff --git a/models/user.js b/models/user.js index 1a02612..013b96f 100644 --- a/models/user.js +++ b/models/user.js @@ -2,18 +2,14 @@ const mongoose = require('mongoose'); // rename to organizer const userSchema = mongoose.Schema({ - name :{ - type: String, - required: true - }, - email:{ + society_email:{ type: String, required: true }, password:{ type: String, required: true - }, + }, acc_type:{// organizer or participant type: String, required: true @@ -22,7 +18,6 @@ const userSchema = mongoose.Schema({ type:Date, default: Date.now } - }); const User = mongoose.model('user', userSchema); diff --git a/routes/api/event_routes.js b/routes/api/event_routes.js index e733f69..652c3e0 100644 --- a/routes/api/event_routes.js +++ b/routes/api/event_routes.js @@ -51,10 +51,18 @@ router.post('/', passport.authenticate('jwt', {session : true}) , acc_type_autho description : req.body.description, society : req.body.society, form_link : req.body.form_link, - cover_link : req.body.cover_link, + cover_photo : { + name: req.body.cover_photo.name, + link: req.body.cover_photo.link + }, number_of_participants : req.body.number_of_participants, date : req.body.date, - prizes_worth : req.body.prizes_worth, + prize_description : req.body.prize_description, + tags: req.body.tags, + event_incharge : { + name : req.body.event_incharge.name, + mobile_number: req.body.event_incharge.mobile_number + } }) newEvent.save().then((result) => { res.json(result) @@ -79,10 +87,18 @@ router.put('/:id', passport.authenticate('jwt', {session : true}) , acc_type_aut description : req.body.description, society : req.body.society, form_link : req.body.form_link, - cover_link : req.body.cover_link, + cover_photo : { + name: req.body.cover_photo.name, + link: req.body.cover_photo.link + }, number_of_participants : req.body.number_of_participants, date : req.body.date, - prizes_worth : req.body.prizes_worth, + prize_description : req.body.prize_description, + tags: req.body.tags, + event_incharge : { + name : req.body.event_incharge.name, + mobile_number: req.body.event_incharge.mobile_number + } } Event.findByIdAndUpdate({_id : req.params.id}, updatedEvent).then(() => { Event.findOne({_id : req.params.id}).then((result) => { diff --git a/routes/api/profile_routes.js b/routes/api/profile_routes.js index 8d8e6e6..f707f12 100644 --- a/routes/api/profile_routes.js +++ b/routes/api/profile_routes.js @@ -25,47 +25,28 @@ router.get('/', passport.authenticate('jwt', {session:false}), (req, res) => { } }) -//to check of username Exists -router.get('/username/:username', passport.authenticate('jwt', {session: false}), (req, res) => { - if (req.user) { - Profile.findOne({username : req.params.username}).then((value) => { - if(value){ - res.json({message : 'Username Taken. Please Select a different Username'}) - }else{ - res.json({message : 'Username Available'}) - } - }).catch((err) => console.log(err)) - } -}) - // create a profile for the current user router.post('/', passport.authenticate('jwt', {session:false}), (req, res) => { const profileF = { user : req.user.id, - username : req.body.username, - mobile_number : req.body.mobile_number, + society_name : req.body.society_name, college : req.body.college, - field_of_study : req.body.field_of_study, - year : req.body.year + president_name : req.body.president_name, + mobile_number : req.body.mobile_number } Profile.findOne({user : req.user.id}).then((result) => { if(result){ - Profile.findOneAndUpdate({user : req.user.id}, {$set : profileF}, {new: true }).then((updated_profile) => { - res.json(updated_profile) - }).catch((err) => {console.log(err)}) + Profile.findOneAndUpdate({user : req.user.id}, {$set : profileF}, {new: true }) + .then((updated_profile) => res.json(updated_profile)) + .catch((err) => console.log(err)) } else{ - //check if mobile mobile_number is valid using 2FA ? - Profile.findOne({username : profileF.username}).then((value) => { - if(value){ - res.json({message : 'Username Taken. Please Select a different Username'}) - }else{ - const newProfile = new Profile(profileF); - newProfile.save().then((result) => {res.json(result)}).catch((err) => console.log(err)) - } - }) - } - }).catch((err) => {console.log(err)}) + const newProfile = new Profile(profileF); + newProfile.save() + .then((result) => res.json(result)) + .catch((err) => console.log(err)) + } + }).catch((err) => console.log(err)) }) @@ -74,10 +55,10 @@ router.post('/', passport.authenticate('jwt', {session:false}), (req, res) => { router.patch('/', passport.authenticate('jwt', {session: false}), (req, res) => { if(req.user){ const ProfileF = { - mobile_number : req.body.mobile_number, + society_name : req.body.society_name, college : req.body.college, - field_of_study : req.body.field_of_study, - year : req.body.year + president_name : req.body.president_name, + mobile_number : req.body.mobile_number, } Profile.findOne({user : req.user.id}).then((result) => { if(result){ diff --git a/routes/api/user_routes.js b/routes/api/user_routes.js index 7aaed6c..f3d765a 100644 --- a/routes/api/user_routes.js +++ b/routes/api/user_routes.js @@ -14,18 +14,17 @@ const router = express.Router(); // to register a new user router.post('/register', (req, res) => { - User.findOne({email: req.body.email}).then((result) => { + User.findOne({society_email: req.body.society_email}).then((result) => { if (!result){ const new_User = new User({ - name: req.body.name, - email : req.body.email, + society_email : req.body.society_email, password: req.body.password, acc_type : req.body.acc_type }) bcrypt.genSalt(10, (err, salt) => { bcrypt.hash(new_User.password, salt, (err, hash) => { - if (err) throw err; + if (err) throw err;3 new_User.password = hash; new_User.save().then((result) => { res.json(result) @@ -41,9 +40,9 @@ router.post('/register', (req, res) => { // to login a resgistered User router.post('/login', (req, res) => { - const email = req.body.email; + const society_email = req.body.society_email; const password = req.body.password; - User.findOne({email}).then((result) => { + User.findOne({society_email}).then((result) => { if(!result){ return res.status(404).json({message : 'email not found'}) } @@ -51,8 +50,7 @@ router.post('/login', (req, res) => { if(match){ const payload = { id : result.id, - name : result.name, - email : result.email, + society_email : result.society_email, acc_type : result.acc_type } //jwt payload jwt.sign(payload, 'abcssss', { expiresIn: 3600}, (err, token) => { @@ -69,7 +67,7 @@ router.post('/login', (req, res) => { // to get the current user router.get('/current', passport.authenticate('jwt', {session : false}),(req, res) => { - //User.findOne({email : req.user.email}).then((result) => {console.log(result);}) + //User.findOne({society_email : req.user.society_email}).then((result) => {console.log(result);}) res.json(req.user); }); @@ -100,7 +98,7 @@ router.delete('/current', passport.authenticate('jwt', {session : false}), (req, } }) - User.findOneAndRemove({email : req.user.email}).then((result) => { + User.findOneAndRemove({society_email : req.user.society_email}).then((result) => { if(result){ message.user = 'user deleted successfully'; res.json(message) diff --git a/routes/organizer_routes.js b/routes/organizer_routes.js index 2f04bdd..17f90f6 100644 --- a/routes/organizer_routes.js +++ b/routes/organizer_routes.js @@ -11,7 +11,7 @@ router.get('/register', (req, res) => { res.render('register'); }) -router.get('/dashboard' ,passport.authenticate('jwt', {session : false}), (req, res) => { +router.get('/dashboard' ,passport.authenticate('jwt', {session : false, failureRedirect:'/organizer/login'}), (req, res) => { res.render('dashboard'); })