diff --git a/src/controllers/commentsCtl.ts b/src/controllers/commentsCtl.ts index 2908a8c..7163755 100644 --- a/src/controllers/commentsCtl.ts +++ b/src/controllers/commentsCtl.ts @@ -173,29 +173,24 @@ export const deleteComment = async (req: Request, res: Response) => { const commentId = req.params.commentId; try { - // Find the blog by ID - const blog = await Blog.findById(blogId); - - if (!blog) { + // Use findByIdAndUpdate to remove the comment directly + const result = await Blog.findByIdAndUpdate( + blogId, + { $pull: { comments: { _id: commentId } } }, + { new: true } + ).populate('comments'); + + if (!result) { return res.status(404).json({ status: 'error', message: 'Blog not found', }); } - // Remove the comment by ID from the comments array - blog.comments = blog.comments.filter(comment => comment._id.toString() !== commentId); - - // Save the updated blog - await blog.save(); - - // Populate comments before responding - await blog.populate('comments'); - // Respond with success and the updated comments array res.json({ status: 'success', - data: blog.comments, + data: result.comments, }); } catch (err) { console.error('Error in deleteComment:', err); @@ -204,4 +199,4 @@ export const deleteComment = async (req: Request, res: Response) => { message: err.message, }); } -}; +}; \ No newline at end of file diff --git a/src/routes/blogRoutes.ts b/src/routes/blogRoutes.ts index 192da98..73a6927 100644 --- a/src/routes/blogRoutes.ts +++ b/src/routes/blogRoutes.ts @@ -45,7 +45,7 @@ router.post('/', upload.single('file'),isLogin,isAdmin, async (req:any, res) => }); // Update a blog by ID -router.put('/:id', upload.single('file'),isLogin,isAdmin, async (req:any, res) => { +router.patch('/:id', upload.single('file'),isLogin,isAdmin, async (req:any, res) => { try { // Validate request body const { error } = updateBlogValidation.validate(req.body); diff --git a/src/swagger.json b/src/swagger.json index 2d348ea..b5ad26f 100644 --- a/src/swagger.json +++ b/src/swagger.json @@ -83,6 +83,7 @@ "post": { "summary": "Create an account", "tags": ["Authentication"], + "security": [], "requestBody": { "content": { "application/json": { @@ -125,6 +126,7 @@ "post": { "summary": "User Login", "tags": ["Authentication"], + "security": [], "requestBody": { "content": { "application/json": { @@ -160,6 +162,7 @@ "400": { "description": "Bad Request" } + } } }, @@ -285,6 +288,7 @@ "get": { "summary": "Read blogs", "tags": ["Blogs"], + "security": [], "responses": { "200": { "description": "OK", @@ -333,6 +337,7 @@ "get": { "summary": "Read a single blog", "tags": ["Blogs"], + "security": [], "parameters": [ { "name": "id", @@ -396,7 +401,7 @@ } } }, - "put": { + "patch": { "summary": "Update a blog", "tags": ["Blogs"], "parameters": [ @@ -554,6 +559,7 @@ "post": { "summary": "Add a comment to a blog", "tags": ["Comments"], + "security": [], "parameters": [ { "name": "id", @@ -644,6 +650,7 @@ "post": { "summary": "Create a user query", "tags": ["Queries"], + "security": [], "consumes": ["application/json"], "produces": ["application/json"], "requestBody": {