Created
October 15, 2020 14:00
-
-
Save zorca/9d71fae56e8c46b69aadb559a7039732 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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