Okay, so I reviewed again the docs, and this is the best solution I achieved:
add this to post.js inside controller folder of post
async like(ctx) {
// Get ID of post
const { post_id } = ctx.params;
// Get user liking the post
const user = ctx.state.user;
// Check if user is logged
if (!user) {
return ctx.badRequest(null, [{ messages: [{ id: 'No authorization header was found' }] }]);
}
// Find the liked posts from the user
let userQuery = await strapi.query('user', 'users-permissions');
let liked_posts = await userQuery.findOne({ id: user.id }).then(res => res["liked_posts"]);
// Check if the post is already liked
var index = liked_posts.map(el => el.id.toString()).indexOf(post_id.toString());
if (index > -1) {
// If post already liked by user
liked_posts.splice(index, 1);
} else {
// If post not liked by user
liked_posts.push(post_id.toString());
}
// Update liked post from user
userQuery.update({ id: user.id }, { liked_posts: liked_posts});
}
put this inside routes.json in config folder of post:
{
"method": "PUT",
"path": "/posts/like/:post_id",
"handler": "post.like",
"config": {
"policies": []
}
}