Skip to content

Commit

Permalink
created and integrated delete image api
Browse files Browse the repository at this point in the history
  • Loading branch information
Swarga-codes committed May 24, 2024
1 parent 2902949 commit a95af5c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
26 changes: 26 additions & 0 deletions app/api/memories/deleteImages/[imageId]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NextRequest,NextResponse } from "next/server";
import connectDb from "@/app/util/connectDb";
import { checkValidityOfToken } from "@/app/util/checkValidityOfToken";
import USER from "@/app/models/userSchema";
import FILE from "@/app/models/fileSchema";
export async function DELETE(req:NextRequest){
try{
const decodedToken = checkValidityOfToken(req);
if (!decodedToken.success) return NextResponse.json(decodedToken, { status: 401 });
const email = decodedToken?.email;
await connectDb();
const isExistingUser=await USER.findOne({email:email})
if(!isExistingUser) return NextResponse.json({success:false,message:'User not found!'},{status:404})
const splitUrl=req.url.split('/')
const imageId=splitUrl[splitUrl.length-1];
const isValidImage=await FILE.findOne({_id:imageId}).populate('memoryId')
if(!isValidImage) return NextResponse.json({success:false,message:'Image not found!'},{status:404})
if((isValidImage.createdBy.toString()!==isExistingUser._id.toString()) && (isValidImage.memoryId.createdBy.toString()!==isExistingUser._id.toString())) return NextResponse.json({success:false,message:'User not authorized to perform this action!'},{status:403})
await FILE.findByIdAndDelete(imageId)
return NextResponse.json({success:true,message:'Picture deleted successfully!'},{status:200})
}
catch(err){
console.log(err)
return NextResponse.json({success:false,message:'Could not delete image, try again!'},{status:500})
}
}
27 changes: 25 additions & 2 deletions app/memories/[memoryId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ async function downloadFile(fileName:string,fileUrl:string){
toast.error('Failed to download image')
}
}
async function deleteImage(imageId:string) {
const response=await fetch(`/api/memories/deleteImages/${imageId}`,{
method:'DELETE',
headers:{
'Content-Type':'application/json'
}
})
const data=await response.json()
if(data.success){
toast.success(data.message)
fetchMemoryImages()
}
else{
toast.error(data.message)
}
}
useEffect(()=>{
fetchMemoryImages()
},[])
Expand Down Expand Up @@ -90,10 +106,17 @@ if(typeof localStorage!=undefined){
<p className='p-2'>{image?.fileName}</p>
<p className='p-2'>Uploaded on {image?.createdAt.substring(0,10)}</p>
</div>
<div className='ml-auto m-4 cursor-pointer' onClick={()=>downloadFile(image?.fileName,image?.fileUrl)}>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="size-6">
<div className='ml-auto m-4 cursor-pointer'>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="size-6" onClick={()=>downloadFile(image?.fileName,image?.fileUrl)}>
<path strokeLinecap="round" strokeLinejoin="round" d="M19.5 13.5 12 21m0 0-7.5-7.5M12 21V3" />
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="red" className="size-6 mt-2" onClick={()=>{
if(window.confirm('Do you really wanna delete this image?')){
deleteImage(image?._id)
}
}}>
<path strokeLinecap="round" strokeLinejoin="round" d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
</svg>

</div>
</div>
Expand Down

0 comments on commit a95af5c

Please sign in to comment.