Skip to content

Commit

Permalink
changing put to patch, comments deletion bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
amiparadis250 committed Mar 8, 2024
1 parent ed9bb69 commit 8dffafc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
25 changes: 10 additions & 15 deletions src/controllers/commentsCtl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -204,4 +199,4 @@ export const deleteComment = async (req: Request, res: Response) => {
message: err.message,
});
}
};
};
2 changes: 1 addition & 1 deletion src/routes/blogRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion src/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"post": {
"summary": "Create an account",
"tags": ["Authentication"],
"security": [],
"requestBody": {
"content": {
"application/json": {
Expand Down Expand Up @@ -125,6 +126,7 @@
"post": {
"summary": "User Login",
"tags": ["Authentication"],
"security": [],
"requestBody": {
"content": {
"application/json": {
Expand Down Expand Up @@ -160,6 +162,7 @@
"400": {
"description": "Bad Request"
}

}
}
},
Expand Down Expand Up @@ -285,6 +288,7 @@
"get": {
"summary": "Read blogs",
"tags": ["Blogs"],
"security": [],
"responses": {
"200": {
"description": "OK",
Expand Down Expand Up @@ -333,6 +337,7 @@
"get": {
"summary": "Read a single blog",
"tags": ["Blogs"],
"security": [],
"parameters": [
{
"name": "id",
Expand Down Expand Up @@ -396,7 +401,7 @@
}
}
},
"put": {
"patch": {
"summary": "Update a blog",
"tags": ["Blogs"],
"parameters": [
Expand Down Expand Up @@ -554,6 +559,7 @@
"post": {
"summary": "Add a comment to a blog",
"tags": ["Comments"],
"security": [],
"parameters": [
{
"name": "id",
Expand Down Expand Up @@ -644,6 +650,7 @@
"post": {
"summary": "Create a user query",
"tags": ["Queries"],
"security": [],
"consumes": ["application/json"],
"produces": ["application/json"],
"requestBody": {
Expand Down

0 comments on commit 8dffafc

Please sign in to comment.