Skip to content

Instantly share code, notes, and snippets.

@zorca
Created September 23, 2020 10:53
Show Gist options
  • Save zorca/434445e2bf026ae7443e854c229113d8 to your computer and use it in GitHub Desktop.
Save zorca/434445e2bf026ae7443e854c229113d8 to your computer and use it in GitHub Desktop.
'use strict';
/**
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/controllers.html#core-controllers)
* to customize this controller
*/
module.exports = {
likeOne: async (ctx, next) => {
let user = ctx.state.user;
if (!user) {
return ctx.badRequest(null, [{ messages: [{ id: 'No authorization header was found' }] }]);
}
const { id } = ctx.params;
const currentUsermedia = await strapi.query('usermedias').findOne({ id: id });
console.log(currentUsermedia.users);
let resultArray = [];
let updatedUsers = currentUsermedia.users.map(item => resultArray.push(item.id));
updatedUsers.push(user.id);
console.log(updatedUsers);
await strapi.query('usermedias').update({ id: id }, {
users: updatedUsers,
});
ctx.response.status = 200;
ctx.body = 'usermedia: ' + id + ' liked';
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment