Creating a like button on the app.

Hi everyone! So I am creating an app that somewhat similar like Reddit. As a user, I would like to give an upvote/downvote button to a certain thread and I would like to see the number of upvotes. Has anyone created something similar? If yes, would you guys mind to share your ideas/work on how to do it? Many thanks in advance!
1 answers

Theres different ways to doing this. Easiest is to make an object with attributes "Upvote” and “Downvote” (or something you like) as integers and associate that to a thread. Every time someone presses either one you can increment the counter by 1. You can show this on the page immediately if you want to. 

However, this may be the easiest but is rather error prone. You can still make the object as mentioned above, but I'd compute the numbers differently. Make 2 associations with a user object (one for downvote, one for upvote). When someone clicks you fill that association between the user and the counter object. When showing, you always recalculate the object by retrieving the amount of users that are associated to this object, counting the list and showing that on the counter. That way you are safe for e.g. when a computation goes wrong (since its always recalculating, so a potential mistake will reset), you can control for one user only being able to upvote once (rather than spamming the button 100 times), or when an account gets deleted the counter will pick up on that.


There's probably a better way of fixing this if you really start refining what you need. In all, I hope this gives you a general direction to work with!