Skip to content

Commit cb8c43a

Browse files
author
Lovepreet Singh
committed
added upto comments and reviews
1 parent 8c26ff2 commit cb8c43a

File tree

6,392 files changed

+2635837
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

6,392 files changed

+2635837
-0
lines changed

api/api.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var router = require('express').Router();
2+
var async = require('async');
3+
var faker = require('faker');
4+
var Category = require('../models/category');
5+
var Product = require('../models/product');
6+
7+
// this url is not same as declareed in main.js . It will be like '/api/search'
8+
router.post('/search',function(req,res,next){
9+
console.log(req.body.search_term);
10+
Product.search({
11+
query_string: {query:req.body.search_term}
12+
},function(err,results){
13+
if(err) return next(err);
14+
res.json(results);
15+
});
16+
});
17+
18+
router.get('/:name',function(req,res,next){
19+
20+
async.waterfall([
21+
function(callback){
22+
Category.findOne({name:req.params.name},function(err,category){
23+
if(err) return next(err);
24+
callback(null,category);
25+
});
26+
},
27+
function(category,callback){
28+
for(var i=0; i<30 ;i++){
29+
var product =new Product();
30+
product.category = category._id;
31+
product.name = faker.commerce.productName();
32+
product.price =faker.commerce.price();
33+
product.image = faker.image.image();
34+
35+
product.save();
36+
}
37+
}
38+
]);
39+
res.json({message:'Success'});
40+
});
41+
42+
module.exports = router;

config/passport.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var passport = require('passport');
2+
var LocalStrategy = require('passport-local').Strategy; // this library is for local login system
3+
var User = require('../models/user');
4+
//serialize and deserialize
5+
passport.serializeUser(function(user,done){
6+
done(null,user._id);
7+
});
8+
9+
passport.deserializeUser(function(id,done){
10+
User.findById(id,function(err,user){
11+
done(err,user);
12+
});
13+
});
14+
15+
//middleware
16+
// 'local-login' is just a name given to middleware
17+
passport.use('local-login',new LocalStrategy({
18+
usernameField:'email',
19+
passwordField:'password',
20+
passReqToCallback:true
21+
},function(req,email,password,done){
22+
User.findOne({email:email},function(err,user){
23+
if(err) return done(err);
24+
25+
if(!user){
26+
return done(null,false,req.flash('loginMessage','No user has been found'));
27+
}
28+
29+
//comparePassword is the method which we declared in UserSchema database
30+
if(!user.comparePassword(password)){
31+
return done(null,false,req.flash('loginMessage','Oops ! Wrong Password pal'));
32+
}
33+
return done(null,user);
34+
});
35+
}));
36+
37+
//custom function to validate
38+
exports.isAuthenticated = function(req,res,next){
39+
if(req.isAuthenticated()){
40+
//if user is authenticated allow him whatever route he want to take
41+
return next();
42+
}
43+
res.redirect('/login');
44+
}

config/secret.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
database : 'mongodb://lovepreet:[email protected]:41561/ecommerce',
3+
port : 3000,
4+
secretKey : "lovepreet@#!%"
5+
}

middlewares/middlewares.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var Cart = require('../models/cart');
2+
3+
4+
module.exports = function(req, res, next) {
5+
6+
if (req.user) {
7+
var total = 0;
8+
Cart.findOne({ owner: req.user._id }, function(err, cart) {
9+
if (cart) {
10+
for (var i = 0; i < cart.items.length; i++) {
11+
total += cart.items[i].quantity;
12+
}
13+
res.locals.cart = total;
14+
} else {
15+
res.locals.cart = 0;
16+
}
17+
next();
18+
})
19+
} else {
20+
next();
21+
}
22+
}

models/cart.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var mongoose = require('mongoose');
2+
var Schema = mongoose.Schema;
3+
4+
var CartSchema = new Schema({
5+
owner: { type: Schema.Types.ObjectId, ref: 'User'},
6+
total: { type: Number, default: 0},
7+
items: [{
8+
item: { type: Schema.Types.ObjectId, ref: 'Product'},
9+
quantity: { type: Number, default: 1},
10+
price: { type: Number, default: 0},
11+
}]
12+
});
13+
14+
15+
module.exports = mongoose.model('Cart', CartSchema);

models/category.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var mongoose = require('mongoose');
2+
var Schema = mongoose.Schema;
3+
4+
var CategorySchema = new Schema({
5+
name:{type:String,unique:true,lowercase:true}
6+
});
7+
8+
module.exports = mongoose.model('Category',CategorySchema);

models/product.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var mongoose = require('mongoose');
2+
var mongoosastic = require('mongoosastic');
3+
var Schema = mongoose.Schema;
4+
5+
var ProductSchema = new Schema({
6+
category: {type:Schema.Types.ObjectId,ref:'Category'},
7+
name:String,
8+
price:Number,
9+
image:String
10+
});
11+
12+
ProductSchema.plugin(mongoosastic,{
13+
hosts:[
14+
'localhost:9200'
15+
]
16+
});
17+
18+
module.exports = mongoose.model('Product',ProductSchema);

models/user.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var mongoose = require('mongoose'); // it connects nodejs and mongodb
2+
var bcrypt = require('bcrypt-nodejs'); // it is library that hashes passwords etc
3+
var crypto = require('crypto'); // no need to install this library it is built-in nodejs
4+
var Schema = mongoose.Schema;
5+
// The user schema attributes / characteristics / fields
6+
var UserSchema = new Schema({
7+
email:{type:String ,unique:true,lowercase:true},
8+
password: String,
9+
profile: {
10+
name:{type:String,default:''},
11+
picture:{type:String,default:''}
12+
},
13+
address:String,
14+
history:[{
15+
paid:{type:Number, default:0},
16+
item: { type: Schema.Types.ObjectId, ref: 'Product'}
17+
}]
18+
});
19+
20+
//Hash the password before we even save it to the database
21+
//pre is mongoose method which is available for all schemas. It contains actions for what is to be done prior to storing it in db
22+
UserSchema.pre('save',function(next){
23+
var user = this;
24+
if(!user.isModified('password')) return next();
25+
//10 is the no of rounds of salt
26+
//salt is the output of genSalt
27+
//genSalt is methos of bcrypt library to generate salt
28+
bcrypt.genSalt(10,function(err,salt){
29+
if(err) return next(err);
30+
//call method to generate hash
31+
bcrypt.hash(user.password,salt,null,function(err,hash){
32+
if(err) return next(err);
33+
user.password = hash;
34+
next();
35+
});
36+
});
37+
});
38+
39+
//compare password in the database and the one entered by user
40+
// comparePassword is a user defined method on schema . To declare it use 'methods' first
41+
UserSchema.methods.comparePassword = function(password){
42+
//here function parameter 'password' is the passowrd that user types in
43+
return bcrypt.compareSync(password,this.password);
44+
}
45+
46+
UserSchema.methods.gravatar = function(size){
47+
if(!this.size) size=200;
48+
if(!this.email) return 'https://gravatar.com/avatar/?s='+size+'&d=retro';
49+
var md5 = crypto.createHash('md5').update(this.email).digest('hex');
50+
return 'https://gravatar.com/avatar/'+md5+'?s='+size+'&d=retro';
51+
}
52+
53+
//this is done so that other files like server.js etc can use this schema
54+
module.exports = mongoose.model('User',UserSchema); // first parameter is user second is schema name

0 commit comments

Comments
 (0)