Created
August 27, 2014 13:05
-
-
Save Epotignano/45b173cff7a5197bf500 to your computer and use it in GitHub Desktop.
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
/************** schemas/usersSchema ************* | |
var mongoose = require('mongoose'); | |
var Schema = require('mongoose').Schema; | |
var bcrypt = require('bcrypt-nodejs') | |
var util = require('util'); | |
var fashionTypes = require('../configs/referenceValues').fashionTypes; | |
function AbstractUserSchema() { | |
Schema.apply(this, arguments); | |
this.add({ | |
username : String, | |
email: String, | |
password : String, | |
profilePhoto : String, | |
country: String, | |
bio: String | |
}); | |
}; | |
util.inherits(AbstractUserSchema, Schema); | |
var userSchema = new AbstractUserSchema(); | |
/***Commons Methods ***/ | |
// generating a hash | |
userSchema.methods.generateHash = function(password) { | |
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); | |
}; | |
// checking if password is valid | |
userSchema.methods.validPassword = function(password) { | |
return bcrypt.compareSync(password, this.password); | |
}; | |
/***** ****/ | |
//Reference the model of a generic user in a variable | |
var User = mongoose.model('User', userSchema) | |
//Definition of Seller | |
var sellerSchema = new AbstractUserSchema ({ | |
stores : { | |
products : [] | |
}, | |
bio: String, | |
website: String | |
}); | |
// Definition of Buyer | |
var buyerSchema = new AbstractUserSchema ({ | |
cart : [], | |
mainlylookingFor : {type: String, enum : fashionTypes }, | |
//Followers must be populated with users | |
followers: [], | |
//Following must be populated with users | |
following : [], | |
//Following must be populated with products | |
likes : [], | |
ordersHistory : [] | |
}) | |
//Put it all together for prepare for export; | |
var usersType = { | |
User : User, | |
Seller : User.discriminator('Seller', sellerSchema), | |
Buyer : User.discriminator('Buyer', buyerSchema) | |
}; | |
//Export it | |
module.exports = usersType | |
/***************************** routes/users.js ******************************/ | |
var express = require('express'); | |
var router = express.Router(); | |
var AbstractUsersTypes = require('../schemas/usersSchema'); | |
var Users = AbstractUsersTypes.User | |
var Seller = AbstractUsersTypes.Seller; | |
var Buyer = AbstractUsersTypes.Buyer; | |
/* GET users listing. */ | |
router.post('/create/seller', function(req, res){ | |
var seller = new Seller(req.body); | |
seller.password = seller.generateHash(req.body.password) | |
seller.save(function(err, response){ | |
if(err) return err; | |
//res.redirect('/login'); | |
console.log('Saved with success'); | |
res.send(200); | |
}); | |
}); | |
router.post('/create/buyer', function(req, res){ | |
var buyer = new Buyer(req.body); | |
buyer.password = buyer.generateHash(req.body.password) | |
buyer.save(function(err, response){ | |
if(err) return err; | |
//res.redirect('/login'); | |
console.log('Saved with success'); | |
res.send(200); | |
}); | |
}); | |
router.get('/find/all-buyers', function(req, res){ | |
Buyer.find({}, function(err, response){ | |
if(err) return err; | |
//res.redirect('/login'); | |
console.log('Saved with success'); | |
res.send(response); | |
}); | |
}); | |
router.get('/find/all-sellers', function(req, res){ | |
Seller.find({},function(err, response){ | |
if(err) return err; | |
//res.redirect('/login'); | |
res.send(response); | |
}); | |
}); | |
router.get('/find/all', function(req, res){ | |
Users.find({},function(err, response){ | |
if(err) return err; | |
//res.redirect('/login'); | |
res.send(response); | |
}); | |
}); | |
module.exports = router; | |
/*************************** in the app.js ************************/ | |
var users = require('./routes/users'); | |
app.use('/user', users); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment