-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 39c902b
Showing
13,483 changed files
with
1,752,028 additions
and
0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"git.ignoreLimitWarning": true | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const express=require('express') | ||
const mongoose=require('mongoose') | ||
const path=require('path') | ||
const session=require('express-session') | ||
const MongoServer=require('connect-mongodb-session')(session) | ||
const csrf=require('csurf') | ||
const flash=require('connect-flash') | ||
const multer=require('multer') | ||
|
||
const bodyparser=require('body-parser') | ||
const MONGODB_URL='put your MongoDB URl' | ||
|
||
const app=express() | ||
const store=new MongoServer({ | ||
uri:MONGODB_URL, | ||
collection:'sessions' | ||
}) | ||
const csrfProtection=csrf() | ||
|
||
const User=require('./models/user') | ||
|
||
const fileStorage=multer.diskStorage({ | ||
destination:(req,file,cb)=>{ | ||
cb(null,'images') | ||
}, | ||
filename:(req,file,cb)=>{ | ||
cb(null,new Date().toISOString().replace(/:/g, '-')+'-'+file.originalname) | ||
} | ||
}) | ||
const filter=(req,file,cb)=>{ | ||
if(file.mimetype==='image/jpg' || | ||
file.mimetype==='image/jpeg' || | ||
file.mimetype==='image/png') | ||
{ | ||
cb(null,true) | ||
}else{ | ||
cb(null,false) | ||
} | ||
} | ||
|
||
app.set('view engine','ejs') | ||
app.set('views','views') | ||
|
||
const adminRoutes=require('./routes/admin') | ||
const shopRoutes=require('./routes/shop') | ||
const authRoutes=require('./routes/auth') | ||
|
||
|
||
const errorController=require('./controller/error') | ||
|
||
|
||
app.use(bodyparser.urlencoded({ extended: true})) | ||
app.use(multer({storage:fileStorage,fileFilter:filter}).single('image')) | ||
|
||
app.use(express.static(path.join(__dirname,'public'))) | ||
app.use('/images',express.static(path.join(__dirname,'images'))) | ||
app.use(session({secret:'nishant patel',resave:false,saveUninitialized:false,store:store})) | ||
app.use(csrfProtection) | ||
app.use(flash()) | ||
app.use((req,res,next)=>{ | ||
if(!req.session.user){ | ||
return next() | ||
} | ||
User.findOne(req.session.user._id).then((user)=>{ | ||
req.user=user | ||
next() | ||
|
||
}) | ||
}) | ||
app.use((req,res,next)=>{ | ||
res.locals.isAuth=req.session.isLoggedIn | ||
res.locals.csrfToken=req.csrfToken() | ||
next() | ||
}) | ||
app.use('/admin',adminRoutes) | ||
app.use(shopRoutes) | ||
app.use(authRoutes) | ||
app.get('/500',errorController.get500Page) | ||
app.use(errorController.get4040Page) | ||
mongoose.connect(MONGODB_URL,{useNewUrlParser:true,useUnifiedTopology:true}).then((result)=>{ | ||
|
||
app.listen(3000,()=>{ | ||
console.log('server started on port 3000!') | ||
}) | ||
}).catch((error)=>{ | ||
console.log(error) | ||
}) | ||
|
||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
const Product=require('../models/product') | ||
const {validationResult}=require('express-validator') | ||
exports.getAddProduct=(req,res,next)=>{ | ||
res.render('admin/edit-product', | ||
{pageTitle:'add-product', | ||
path:'/admin/add-product', | ||
edit:false, | ||
isAuth:req.session.isLoggedIn, | ||
|
||
}) | ||
} | ||
exports.getEditProduct=(req,res,next)=>{ | ||
const editMode=req.query.edit | ||
if(!editMode){ | ||
res.redirect('/') | ||
} | ||
const productId=req.params.productId | ||
Product.findById(productId).then((product)=>{ | ||
if(!product){ | ||
return res.redirect('/') | ||
} | ||
res.render('admin/edit-product', | ||
{pageTitle:'edit-product', | ||
path:'/admin/edit-product', | ||
edit:editMode, | ||
product:product, | ||
isAuth:req.session.isLoggedIn, | ||
|
||
|
||
}) | ||
}).catch((error)=>{ | ||
console.log(erro) | ||
}) | ||
|
||
} | ||
|
||
exports.postEditProduct=(req,res,next)=>{ | ||
const productId=req.body.productId | ||
const updatedtitle=req.body.title | ||
const image=req.file | ||
const updatedprice=req.body.price | ||
const updateddescription=req.body.description | ||
Product.findById(productId).then((product)=>{ | ||
if(product.userId.toString()!==req.user._id.toString()){ | ||
return res.redirect('/') | ||
} | ||
product.title=updatedtitle | ||
product.price=updatedprice | ||
product.description=updateddescription | ||
if(image){ | ||
updatedimageurl=image.path | ||
} | ||
product.imageUrl=updatedimageurl | ||
return product.save().then(()=>{ | ||
console.log('PRODUCT UPDATED!') | ||
res.redirect('/admin/products') | ||
}) | ||
}) | ||
.catch((error)=>{ | ||
console.log(error) | ||
}) | ||
|
||
|
||
} | ||
exports.productDelete=(req,res,next)=>{ | ||
const productId=req.params.productId | ||
|
||
Product.deleteOne({_id:productId,userId:req.user._id}) | ||
.then(()=>{ | ||
console.log('PRODUCT DELETED!') | ||
res.status(200).json({message:'product Deleted'}) | ||
}).catch((error)=>{ | ||
res.status(500).json({message:'Deleting product faild!'}) | ||
}) | ||
|
||
} | ||
exports.postAddProduct=(req,res,next)=>{ | ||
const title=req.body.title | ||
const image=req.file | ||
const price=req.body.price | ||
const description=req.body.description | ||
if(!image){ | ||
|
||
}else{ | ||
const imageUrl=image.path | ||
const product=new Product({ | ||
title:title, | ||
price:price, | ||
description:description, | ||
imageUrl:imageUrl, | ||
userId:req.user | ||
}) | ||
product.save() | ||
.then((result)=>{ | ||
console.log('PRODUCT INSERTED!') | ||
res.redirect('/admin/products') | ||
}).catch((error)=>{ | ||
console.log(error) | ||
}) | ||
|
||
} | ||
|
||
|
||
|
||
} | ||
|
||
exports.getProducts=(req,res,next)=>{ | ||
Product.find({userId:req.user._id}).then((products)=>{ | ||
res.render('admin/products', | ||
{pageTitle:'Admin Products', | ||
prods:products, | ||
path:'/admin/products', | ||
isAuth:req.session.isLoggedIn}) | ||
}).catch((error)=>{ | ||
console.log(error) | ||
}) | ||
|
||
|
||
} |
Oops, something went wrong.