From a95af5ceab1057507408af591bde1f3a1ce913e5 Mon Sep 17 00:00:00 2001 From: Swarga-codes Date: Sat, 25 May 2024 02:11:30 +0530 Subject: [PATCH] created and integrated delete image api --- .../memories/deleteImages/[imageId]/route.ts | 26 ++++++++++++++++++ app/memories/[memoryId]/page.tsx | 27 +++++++++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 app/api/memories/deleteImages/[imageId]/route.ts diff --git a/app/api/memories/deleteImages/[imageId]/route.ts b/app/api/memories/deleteImages/[imageId]/route.ts new file mode 100644 index 0000000..3446d73 --- /dev/null +++ b/app/api/memories/deleteImages/[imageId]/route.ts @@ -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}) +} +} \ No newline at end of file diff --git a/app/memories/[memoryId]/page.tsx b/app/memories/[memoryId]/page.tsx index 1c4febc..02f0073 100644 --- a/app/memories/[memoryId]/page.tsx +++ b/app/memories/[memoryId]/page.tsx @@ -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() },[]) @@ -90,10 +106,17 @@ if(typeof localStorage!=undefined){

{image?.fileName}

Uploaded on {image?.createdAt.substring(0,10)}

-
downloadFile(image?.fileName,image?.fileUrl)}> - +
+ downloadFile(image?.fileName,image?.fileUrl)}> +{ + if(window.confirm('Do you really wanna delete this image?')){ + deleteImage(image?._id) + } +}}> + +