开发者

Having logic problems with a voting system

i am looking for help on an up down voting system.

at the moment i have a voting table that references the user that voted , the user who was voted for and the piece of information(a parking spot) that was voted for

CREATE TABLE parking_spots_votes(
vote_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,

parking_spot_id INTEGER DEFAULT NULL,
    key  parking_spot_id_fk (parking_spot_id),
    FOREIGN KEY (parking_spot_id) REFERENCES parking_spots(id),

uploaded_by_user_id INTEGER DEFAULT NULL,
    key  user_id_fk (uploaded_by_user_id),
    FOREIGN KEY (uploaded_by_user_id) REFERENCES parking_angel_users(id),

vote_casted_user_id INTEG开发者_StackOverflowER DEFAULT NULL,
    key  vote_cast_user_id_fk (vote_casted_user_id),
    FOREIGN KEY (vote_casted_user_id) REFERENCES parking_angel_users(id),

vote_type INTEGER NOT NULL
 )

vote type can be 0 for no vote, 1 for up vote, 2 for downvote

now i am having a little logical trouble.

for example

  1. what if the user has already voted on a parking_spot
  2. How do i check if a user has already voted and if he has not then insert but if he has then do not a return voted already.

  3. How do i update a user(uploaded_by_user_id) score. plus one for an up vote and minus one for a down vote.

so the general flow would be ,

A user presses up vote, the server checks if has been voted for already, if so then you cant vote again. if not then the vote_casted_user_id = current user , parking_spot_id = current info , uploaded_by_user_id = the person who uploaded the info , then the uploaded_by_user score would update depending on the vote type.

I am using java servlet with a JDBC connection to a MYSQL database.

Any ideas for me?


  1. what if the user has already voted on a parking_spot?
  2. How do i check if a user has already voted and if he has not then insert but if he has then do not a return voted already.

These are basically the same question: how do I test whether a user has already voted on a parking_spot? Well, just run a query that fetches the records where the parking_spot_id and the vote_casted_user_id match.

How do i update a user(uploaded_by_user_id) score. plus one for an up vote and minus one for a down vote?

If it's not too much trouble, I'd change the values for vote_type to +1 for upvote and -1 for downvote. Then it's just a matter of SELECT SUM(vote_type) FROM table WHERE uploaded_by_user_id=...

I hope this helps


You can consider setting your primary key with parking_spot_id,uploaded_user_id,casted_user_id

so when a vote comes from same user for same parking spot of the same uploaded user you can catch the exception and handle it that "you have already voted!"

If you don't like this You can handle process by writing your own control mechanism

before saving vote write a select that get count of the records with parking_spot_id,uploaded_user_id,casted_user_id if the count greater than 0 this means already have a vote then don't save and give your message.Otherwise save your vote and select user's score increase it by vote and update the record.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜