Skip to content

Commit

Permalink
fixed edit collection api
Browse files Browse the repository at this point in the history
  • Loading branch information
chiefkarim committed Sep 18, 2023
1 parent dfc253a commit 34d4116
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
44 changes: 29 additions & 15 deletions controllers/collectionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ exports.list_items = asyncHandler(async(req,res,next)=>{
// 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})
const collectionName = await collectionModel.findOne({_id:req.params.id}).exec()
res.send({title:collectionName.name,items:items})
})

//handling editing request GET
Expand Down Expand Up @@ -59,9 +60,12 @@ exports.edit_post =[

const existingCollections =await collectionModel.find({})
let collection=false
for(let item of existingCollections){
if(item.name === req.body.category){collection=item.name}
for(let i=0; i<collections.length;i++){

if (collections[i].name === req.body.name){
collection=true
}
}
if(collection === false) {errors.errors.push({path:'collection',msg:'please choose one of the available collections'})}
//checking for errors

Expand Down Expand Up @@ -99,26 +103,36 @@ exports.edit_post_api =[
//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}
let collection=true

for(let i=0; i<existingCollections.length;i++){

if (existingCollections[i].name === req.body.name){
collection=false
}
}
if(collection === false) {errors.errors.push({path:'collection',msg:'please choose one of the available collections'})}
console.log(collection)
if(collection === false) {errors.errors.push({path:'collection',msg:'please choose new collection name'})}
//checking for errors

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

}


//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})
if(!req.file){

const updatedCollection =await collectionModel.findByIdAndUpdate(req.params.id,{name:req.body.name,description:req.body.description},{new:true}).exec()
return res.send({url:updatedCollection.url})

}
const updatedCollection =await collectionModel.findByIdAndUpdate(req.params.id,{name:req.body.name,description:req.body.description,src:req.file.filename},{new:true}).exec()
console.log(updatedCollection)
res.send({url:updatedCollection.url})

}

Expand Down
4 changes: 2 additions & 2 deletions routes/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ router.get("/", collection_controller.list);
// API get collections
router.get("/api",collection_controller.api_list)

//handling edit collection request GET
//handling edit collection request
router.get("/:id/edit/",authenticate,collection_controller.edit_get)
router.post("/:id/edit/",authenticate,upload.single('src'),collection_controller.edit_post)

//APIhandling edit collection request GET
//APIhandling edit collection request API
router.get("/:id/edit/api",authenticate,collection_controller.edit_get_api)
router.post("/:id/edit/api",authenticate,upload.single('src'),collection_controller.edit_post_api)

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 34d4116

Please sign in to comment.