Skip to content

Commit

Permalink
added new fields
Browse files Browse the repository at this point in the history
  • Loading branch information
rasikraj01 committed Jul 31, 2018
1 parent a45d205 commit aa91ad7
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 80 deletions.
2 changes: 0 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:[email protected]:31941/univent' , { useNewUrlParser: true }).then(() => console.log('Database Connected'));
mongoose.Promise = global.Promise;

Expand Down
36 changes: 27 additions & 9 deletions models/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
20 changes: 7 additions & 13 deletions models/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 2 additions & 7 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +18,6 @@ const userSchema = mongoose.Schema({
type:Date,
default: Date.now
}

});

const User = mongoose.model('user', userSchema);
Expand Down
24 changes: 20 additions & 4 deletions routes/api/event_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) => {
Expand Down
49 changes: 15 additions & 34 deletions routes/api/profile_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})


Expand All @@ -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){
Expand Down
18 changes: 8 additions & 10 deletions routes/api/user_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -41,18 +40,17 @@ 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'})
}
bcrypt.compare(password, result.password).then((match) => {
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) => {
Expand All @@ -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);
});

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion routes/organizer_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
})

Expand Down

0 comments on commit aa91ad7

Please sign in to comment.