Skip to content

Instantly share code, notes, and snippets.

@zorca
Created October 15, 2020 14:00
Show Gist options
  • Save zorca/9d71fae56e8c46b69aadb559a7039732 to your computer and use it in GitHub Desktop.
Save zorca/9d71fae56e8c46b69aadb559a7039732 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, town } = ctx.params;
const currentPoll = await strapi.query('polls').findOne({ id: id });
let updatedUsers = currentPoll.users.map(function(item) {
return item.id;
});
if (updatedUsers.indexOf( user.id ) != -1) {
return ctx.badRequest(null, [{ messages: [{ id: 'Like already exists' }] }]);
}
updatedUsers.push(user.id);
await strapi.query('polls').update({ id: id }, {
users: updatedUsers,
});
ctx.response.status = 200;
ctx.body = 'poll: ' + id + ' liked';
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment