V 0.11.5 : addComment and fields
This commit is contained in:
@@ -30,7 +30,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -623,5 +623,102 @@ export default factories.createCoreController(
|
||||
unhiddenPostId: postId,
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Add a comment to a post
|
||||
* @param {Object} ctx - The Koa context object
|
||||
* @returns {Promise<void>} - Sends the created comment as the response
|
||||
*/
|
||||
async addComment(ctx) {
|
||||
const user = ctx.state.user;
|
||||
if (!user) {
|
||||
return ctx.unauthorized("You must be logged in to add a comment");
|
||||
}
|
||||
|
||||
const postId = parseInt(String(ctx.params.id));
|
||||
const { content } = ctx.request.body;
|
||||
|
||||
// Validate inputs
|
||||
if (isNaN(postId)) {
|
||||
return ctx.badRequest("Invalid post ID");
|
||||
}
|
||||
|
||||
if (!content || typeof content !== "string" || content.trim() === "") {
|
||||
return ctx.badRequest("Comment content is required");
|
||||
}
|
||||
|
||||
try {
|
||||
// Step 1: Verify the post exists and get current comments
|
||||
const currentPost = await strapi.db.query("api::post.post").findOne({
|
||||
where: { id: postId },
|
||||
populate: {
|
||||
comments: {
|
||||
fields: ["id"],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!currentPost) {
|
||||
return ctx.notFound(`Post with id ${postId} not found`);
|
||||
}
|
||||
|
||||
console.log("=== Current Post Comments ===");
|
||||
console.log(
|
||||
"Existing comment IDs:",
|
||||
currentPost?.comments?.map((c: any) => c.id)
|
||||
);
|
||||
|
||||
// Step 2: Create the new comment in the comments collection
|
||||
const newComment = await strapi.db
|
||||
.query("api::comment.comment")
|
||||
.create({
|
||||
data: {
|
||||
content: content.trim(),
|
||||
owner: user.id,
|
||||
likes: [],
|
||||
replies: [],
|
||||
},
|
||||
populate: {
|
||||
owner: {
|
||||
populate: {
|
||||
avatar: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
console.log("=== Comment created successfully ===");
|
||||
console.log("New Comment:", JSON.stringify(newComment, null, 2));
|
||||
|
||||
// Step 3: Update the post to include the new comment ID
|
||||
const existingCommentIds =
|
||||
currentPost.comments?.map((c: any) => c.id) || [];
|
||||
|
||||
await strapi.db.query("api::post.post").update({
|
||||
where: { id: postId },
|
||||
data: {
|
||||
comments: [...existingCommentIds, newComment.id],
|
||||
},
|
||||
});
|
||||
|
||||
console.log("=== Post updated with new comment ===");
|
||||
|
||||
return ctx.send({
|
||||
message: "Comment added successfully",
|
||||
comment: newComment,
|
||||
});
|
||||
} catch (error: any) {
|
||||
// Log detailed error if creation fails
|
||||
console.error("=== STRAPI ERROR DETAILS ===");
|
||||
console.error("Full error:", JSON.stringify(error, null, 2));
|
||||
if (error.error?.details?.errors) {
|
||||
console.error(
|
||||
"Validation errors:",
|
||||
JSON.stringify(error.error.details.errors, null, 2)
|
||||
);
|
||||
}
|
||||
return ctx.internalServerError("Failed to add comment");
|
||||
}
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
@@ -34,5 +34,10 @@ export default {
|
||||
path: "/posts/:id/like",
|
||||
handler: "post.likePost",
|
||||
},
|
||||
{
|
||||
method: "POST",
|
||||
path: "/posts/:id/comment",
|
||||
handler: "post.addComment",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
"name": "Apache 2.0",
|
||||
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
|
||||
},
|
||||
"x-generation-date": "2025-10-05T19:46:56.298Z"
|
||||
"x-generation-date": "2025-10-14T20:21:29.388Z"
|
||||
},
|
||||
"x-strapi-config": {
|
||||
"plugins": [
|
||||
@@ -16550,7 +16550,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -19998,7 +19999,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -23888,7 +23890,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -27337,7 +27340,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -31234,7 +31238,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -35382,7 +35387,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -39936,7 +39942,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -43823,7 +43830,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -47612,7 +47620,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -51388,7 +51397,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -55200,7 +55210,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -59012,7 +59023,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -62785,7 +62797,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -66739,7 +66752,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -70182,7 +70196,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -75947,7 +75962,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -76120,7 +76136,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"locale": {
|
||||
@@ -78958,7 +78975,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -79796,7 +79814,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -82749,7 +82768,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -86647,7 +86667,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -89925,7 +89946,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -94395,7 +94417,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -98232,7 +98255,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
@@ -102029,7 +102053,8 @@
|
||||
"member",
|
||||
"admin",
|
||||
"owner",
|
||||
"follow"
|
||||
"follow",
|
||||
"pending"
|
||||
]
|
||||
},
|
||||
"createdAt": {
|
||||
|
||||
Reference in New Issue
Block a user