I need to create Reddit-like voting system.
Users can vote up (+1 point) and down (-1 point). When they clicked upvote, the button is active, so when they click active button it backs to 0 and they. When upvote is active and they click on downvote, it is -1 point.
My goal is to display list of items and sort them from highest score. The problem is - I need to keep scoring as separate table in items as number type to be able to sort list and I can’t do good function for that.
- UPVOTE (inactive)
- UPVOTE (active - when is upvoted)
- DOWNVOTE (inactive)
- DOWNVOTE (active - when is downvoted)
I have a problem with proper function to calculate SCORE.
UPVOTE post = SCORE + 1
DOWNVOTE post = SCORE -1
Uncheck UPVOTE post = SCORE -1
Uncheck DOWNVOTE post = SCORE +1
The problem is: user UPVOTE, so SCORE = 1 and then he DOWNVOTE directly and SCORE = 0, but it should be -1. It is not needed to uncheck UPVOTE before clicking DOWNVOTE. I can’t disable that because user needs to have opportunity to not vote on posts.
How to do that properly? UPVOTE and DOWNVOTE columns work properly - it just add/delete current user.