diff --git a/src/controllers/Likes.controller.ts b/src/controllers/Likes.controller.ts index 85bb88f..b20713c 100644 --- a/src/controllers/Likes.controller.ts +++ b/src/controllers/Likes.controller.ts @@ -76,6 +76,7 @@ const createBlogLike = async (req:Request, res:Response) => { } }; + const getBlogLikes = async (req:Request, res:Response) => { try { const blogId = req.params.id; @@ -86,23 +87,17 @@ const getBlogLikes = async (req:Request, res:Response) => { }); } + const blog= await blogSchem.findById(blogId); + - let allLike:string[]=[]; - const likes = await blogLikeScheme.find({ blogId: blogId }); - if(!likes){ + if(!blog){ return res.status(404).json({ message: 'Blog not found' }); - } -likes.forEach((item)=>{ - allLike.push(item.userId as string); -}) - - return res.status(200).json({ message: "Likes retrieved successfully", - Likes: allLike + Likes: blog.likesArray }); } catch (Error) { console.error(Error); @@ -110,4 +105,41 @@ likes.forEach((item)=>{ } }; +// const getBlogLikes = async (req:Request, res:Response) => { +// try { +// const likeId = req.params.id; + +// if (!mongoose.Types.ObjectId.isValid(likeId)){ +// return res.status(400).json({ +// message:"Invalid blog ID" +// }); +// } + + +// let allLike:string[]=[]; +// const likes = await blogLikeScheme.find({ blogId: likeId }); +// if(!likes){ +// return res.status(404).json({ +// message: 'Blog not found' +// }); + +// } +// likes.forEach((item)=>{ +// allLike.push(item.userId as string); +// }) + + +// return res.status(200).json({ +// message: "Likes retrieved successfully", +// Likes: allLike +// }); +// } catch (Error) { +// console.error(Error); +// return res.status(500).json({ message: "Internal server error", error: Error }); +// } +// }; + + + + export { createBlogLike, getBlogLikes };