Skip to content

Commit

Permalink
added api routes for collection route
Browse files Browse the repository at this point in the history
  • Loading branch information
chiefkarim committed Sep 9, 2023
1 parent dd2b170 commit 1bf4aee
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 69 deletions.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const session = require('express-session')
app.use(session({secret:"cats", resave:false, saveUninitialized:true}))
app.use(passport.initialize())
app.use(passport.session())
app.use(express.urlencoded({ extended: false }));
app.use(express.urlencoded({ extended: true }));

//optimizing response sent to the user
app.use(compression())
Expand Down
262 changes: 197 additions & 65 deletions controllers/collectionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@ exports.list = asyncHandler(async (req, res, next)=>{

})

// get all the items in that collection
// API get all collections
exports.api_list = asyncHandler(async(req,res,next)=>{
const collections = await collectionModel.find({})
res.send({title:'collections',collections:collections})
})

// get all the items in that collection
exports.list_items = asyncHandler(async(req,res,next)=>{

const items = await itemModel.find({category:req.params.id}).populate('category').exec()
res.render('collectionItems',{title:req.params.id,items:items,})
})

// API get all the items in that collection
exports.list_items_api = asyncHandler(async(req,res,next)=>{
const items = await itemModel.find({category:req.params.id}).populate('category').exec()
res.send({title:req.params.id,items:items})
})

//handling editing request GET
exports.edit_get = asyncHandler(async(req,res,next)=>{
Expand All @@ -31,49 +41,104 @@ exports.edit_get = asyncHandler(async(req,res,next)=>{
}
})

//API handling editing request GET
exports.edit_get_api = asyncHandler(async(req,res,next)=>{
if(typeof res.locals.currentUser == 'object' && res.locals.currentUser.username === 'admin'){
const collections =await collectionModel.find({})
res.send({title:'edit collection',collections:collections})

}else{
res.res({authorization:false})
}
})

//handling editing request POST
exports.edit_post =[
//sanitizing user input
//sanitizing user input

body('description','wrong description')
.trim()
.escape()
,asyncHandler(async(req,res,next)=>{
//initializing errors
if(typeof res.locals.currentUser == 'object' && res.locals.currentUser.username === 'admin'){

body('description','wrong description')
.trim()
.escape()
,asyncHandler(async(req,res,next)=>{
//initializing errors
if(typeof res.locals.currentUser == 'object' && res.locals.currentUser.username === 'admin'){

}else{
res.redirect('/log-in',)
const errors = validationResult(req)
//checking that the collection exists

const errors = validationResult(req)
//checking that the collection exists

const existingCollections =await collectionModel.find({})
let collection=false
for(let item of existingCollections){
if(item.name === req.body.category){collection=item.name}
}
if(collection === false) {errors.errors.push({path:'collection',msg:'please choose one of the available collections'})}
//checking for errors

if(!errors.isEmpty()){
res.render('collectionEdit',{title:'edit collection',errors:errors.array(),collections:existingCollections})
return
}
const existingCollections =await collectionModel.find({})
let collection=false
for(let item of existingCollections){
if(item.name === req.body.category){collection=item.name}
}
if(collection === false) {errors.errors.push({path:'collection',msg:'please choose one of the available collections'})}
//checking for errors

//updating collection

else{
//getting image

const src = req.file.filename
const updatedCollection = collectionModel.findByIdAndUpdate(req.params.id,{src:src},{new:true}).exec()
res.redirect('/collection')

}
if(!errors.isEmpty()){
res.render('collectionEdit',{title:'edit collection',errors:errors.array(),collections:existingCollections})
return
}

//updating collection

else{
//getting image

const src = req.file.filename
const updatedCollection = collectionModel.findByIdAndUpdate(req.params.id,{src:src},{new:true}).exec()
res.redirect('/collection')

}

}else{
res.redirect('/log-in',)


}})]

//handling editing request POST
exports.edit_post_api =[
//sanitizing user input

body('description','wrong description')
.trim()
.escape()
,asyncHandler(async(req,res,next)=>{
//initializing errors
if(typeof res.locals.currentUser == 'object' && res.locals.currentUser.username === 'admin'){

}})]
const errors = validationResult(req)
//checking that the collection exists

const existingCollections =await collectionModel.find({})
let collection=false
for(let item of existingCollections){
if(item.name === req.body.category){collection=item.name}
}
if(collection === false) {errors.errors.push({path:'collection',msg:'please choose one of the available collections'})}
//checking for errors

if(!errors.isEmpty()){
res.send({title:'edit collection',errors:errors.array(),collections:existingCollections})
return
}

//updating collection

else{
//getting image

const src = req.file.filename
const updatedCollection = collectionModel.findByIdAndUpdate(req.params.id,{src:src},{new:true}).exec()
res.send({authorization:true})

}

}else{
res.send({authorization:false})


}})]

//handling create item request GET
exports.create_get =asyncHandler(async(req,res,next)=>{
Expand All @@ -87,6 +152,17 @@ exports.create_get =asyncHandler(async(req,res,next)=>{
}
})

//handling create item request GET
exports.create_get_api =asyncHandler(async(req,res,next)=>{
if(typeof res.locals.currentUser == 'object' && res.locals.currentUser.username === 'admin'){
const collections = await collectionModel.find({})

res.send({title:"collections create",collections:collections})

}else{
res.send({authorization:false})
}
})

//handling create item request Post
exports.create_post =[
Expand All @@ -100,46 +176,102 @@ exports.create_post =[
.isLength({min:3})
.escape(),
asyncHandler(async(req,res,next)=>{
if(typeof res.locals.currentUser.currentUser.currentUser.currentUser == 'object' && res.locals.currentUser.currentUser.currentUser.currentUser.username === 'admin'){
if(typeof res.locals.currentUser == 'object' && res.locals.currentUser.username === 'admin'){

//initializing errors
const errors = validationResult(req)
//checking that the collection exists

const existingCollections =await collectionModel.find({})
let collection=false
for(let item of existingCollections){
if(item.name === req.body.name){collection=true}
}
if(collection === true) {errors.array().push({path:'collection',msg:'please write new collection name'})}
//checking for errors

if(!errors.isEmpty()){
res.render('collectionCreate',{title:'create collection',errors:errors})
return
}

//updating collection

else{
//creating collection
const src = req.file.filename
const updatedCollection =new collectionModel({name:req.body.name,description:req.body.description,src:src})
await updatedCollection.save()
res.redirect('/collection')
}
}else{
res.redirect('/log-in',)

}})
]

//API handling create item request Post
exports.create_post_api =[
//sanitizing user input

body('description','wrong description')
.trim()
.escape(),
body('name','name length must be between 3 and 100 character')
.trim()
.isLength({min:3})
.escape(),
asyncHandler(async(req,res,next)=>{
if(typeof res.locals.currentUser == 'object' && res.locals.currentUser.username === 'admin'){

//initializing errors
const errors = validationResult(req)
//checking that the collection exists

const existingCollections =await collectionModel.find({})
let collection=false
for(let item of existingCollections){
if(item.name === req.body.name){collection=true}
}
if(collection === true) {errors.array().push({path:'collection',msg:'please write new collection name'})}
//checking for errors

if(!errors.isEmpty()){
res.render('collectionCreate',{title:'create collection',errors:errors})
return
}

//updating collection

else{
//creating collection
const src = req.file.filename
const updatedCollection =new collectionModel({name:req.body.name,description:req.body.description,src:src})
await updatedCollection.save()
res.redirect('/collection')
}
}else{
res.redirect('/log-in',)

//initializing errors
const errors = validationResult(req)
//checking that the collection exists

const existingCollections =await collectionModel.find({})
let collection=false
for(let item of existingCollections){
if(item.name === req.body.name){collection=true}
}
if(collection === true) {errors.array().push({path:'collection',msg:'please write new collection name'})}
//checking for errors

if(!errors.isEmpty()){
res.render('collectionCreate',{title:'create collection',errors:errors})
return
}

//updating collection

else{
//creating collection
const src = req.file.filename
const updatedCollection =new collectionModel({name:req.body.name,description:req.body.description,src:src})
await updatedCollection.save()
res.redirect('/collection')
}
}})
]

//handling deleting a collection
exports.delete = asyncHandler(async(req,res,next)=>{
if(typeof res.locals.currentUser.currentUser.currentUser == 'object' && res.locals.currentUser.currentUser.currentUser.username === 'admin'){
if(typeof res.locals.currentUser == 'object' && res.locals.currentUser.username === 'admin'){
await collectionModel.findByIdAndDelete(req.params.id)
res.redirect('/collection')
}else{
res.redirect('/log-in',)
}
})

//API handling deleting a collection
exports.delete_api = asyncHandler(async(req,res,next)=>{
if(typeof res.locals.currentUser == 'object' && res.locals.currentUser.username === 'admin'){
await collectionModel.findByIdAndDelete(req.params.id)
res.send({authorization:true})
}else{
res.send({authorization:false})
}
})
5 changes: 3 additions & 2 deletions controllers/indexController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const collectionModel = require('../models/collection')
// Home page
exports.index = asyncHandler(async(req,res,next)=>{

collectionModel.find({})

res.render('index',{title:'E-commerce', user: req.user })


})
})

// API home page
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"cloudinary": "^1.40.0",
"compression": "^1.7.4",
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"csv-parse": "^5.4.1",
"csv-parser": "^3.0.0",
"debug": "~2.6.9",
Expand Down
Loading

0 comments on commit 1bf4aee

Please sign in to comment.