Lastest objects for post, invite and add cron GNEWS
This commit is contained in:
@@ -2,6 +2,97 @@
|
||||
* post controller
|
||||
*/
|
||||
|
||||
import { factories } from '@strapi/strapi'
|
||||
import { factories } from "@strapi/strapi";
|
||||
|
||||
export default factories.createCoreController('api::post.post');
|
||||
export default factories.createCoreController(
|
||||
"api::post.post",
|
||||
({ strapi }) => ({
|
||||
async create(ctx) {
|
||||
const data = JSON.parse(ctx.request.body.data);
|
||||
|
||||
if (ctx.request.files.media) {
|
||||
const files = Array.isArray(ctx.request.files.media)
|
||||
? ctx.request.files.media[0]
|
||||
: ctx.request.files.media;
|
||||
const extension = files.originalFilename.match(/\.[0-9a-z]+$/i);
|
||||
const payload = {
|
||||
fileInfo: {
|
||||
caption: "undefined",
|
||||
alternativeText: data.name || "",
|
||||
name: `${data.name}_media${extension}`,
|
||||
},
|
||||
};
|
||||
const asset = await strapi.services["plugin::upload.upload"].upload({
|
||||
data: payload,
|
||||
files,
|
||||
});
|
||||
data.media = asset[0].id;
|
||||
}
|
||||
ctx.request.body = { data };
|
||||
const result = await super.create(ctx);
|
||||
return result;
|
||||
},
|
||||
|
||||
async savePost(ctx) {
|
||||
console.log("🎯 savePost called with params:", ctx.params);
|
||||
console.log("🎯 Request method:", ctx.request.method);
|
||||
console.log("🎯 Request URL:", ctx.request.url);
|
||||
|
||||
const user = ctx.state.user;
|
||||
if (!user) {
|
||||
console.log("❌ User not authenticated");
|
||||
return ctx.unauthorized("You must be logged in to save posts");
|
||||
}
|
||||
|
||||
console.log("✅ User authenticated:", user.id);
|
||||
const postId = parseInt(ctx.params.id);
|
||||
|
||||
if (isNaN(postId)) {
|
||||
return ctx.badRequest("Invalid post ID");
|
||||
}
|
||||
|
||||
// Verify the post exists
|
||||
const post = await strapi.entityService.findOne("api::post.post", postId);
|
||||
if (!post) {
|
||||
return ctx.notFound("Post not found");
|
||||
}
|
||||
|
||||
// Get current user with saved_posts
|
||||
const currentUser: any = await strapi.entityService.findOne(
|
||||
"plugin::users-permissions.user",
|
||||
user.id,
|
||||
{ populate: ["saved_posts"] }
|
||||
);
|
||||
|
||||
// Initialize saved_posts array if it doesn't exist
|
||||
const currentSavedPosts = currentUser.saved_posts || [];
|
||||
|
||||
// Check if post is already saved to avoid duplicates
|
||||
if (currentSavedPosts.some((savedPost: any) => savedPost.id === postId)) {
|
||||
console.log("⚠️ Post already saved");
|
||||
return ctx.badRequest("Post already saved");
|
||||
}
|
||||
|
||||
// Add the post to saved_posts
|
||||
const updatedSavedPosts = [...currentSavedPosts, postId];
|
||||
|
||||
console.log("🔄 Updating user with new saved posts:", updatedSavedPosts);
|
||||
|
||||
// Update the user
|
||||
const updatedUser = await strapi.entityService.update(
|
||||
"plugin::users-permissions.user",
|
||||
user.id,
|
||||
{
|
||||
data: {
|
||||
saved_posts: updatedSavedPosts as any,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
return ctx.send({
|
||||
message: "Post saved successfully",
|
||||
savedPostsCount: updatedSavedPosts.length,
|
||||
});
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user