Skip to content

Commit

Permalink
push in likearray
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuyisenge2 committed Mar 30, 2024
1 parent 49bc837 commit 2aaac73
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions src/controllers/Likes.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const createBlogLike = async (req:Request, res:Response) => {
}
};


const getBlogLikes = async (req:Request, res:Response) => {
try {
const blogId = req.params.id;
Expand All @@ -86,28 +87,59 @@ 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);
return res.status(500).json({ message: "Internal server error", error: Error });
}
};

// 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 };

0 comments on commit 2aaac73

Please sign in to comment.