I have a project to make a site as stack overflow but current for reviews for products, I write on java spring boot, in my project you can leave a question and you can answer these questions and can put + or - a question how to make so that one user can current once plus put?

public Question postMarkToQuestion(Long questionId, String mark) { Question question = questionRep.findById(questionId).get(); if(mark.equals("+")){ question.setLikes(question.getLikes() + 1); } else if(mark.equals("-")){ question.setDislikes(question.getDislikes() + 1); } questionRep.save(question); return question; } 
  • one
    First thought - each user has a unique id. Each question and answer has lists of voters. If the user puts a plus, then his id is added to this list. When a user puts a plus, his id is compared with all id's in the list (bd), and if there is the same id, then do not give a vote to the user - Anton Sorokin
  • super spasibo vam - Kanat

1 answer 1

I move the thought out of the comment in response. If you are not using lists, but a database, then the lists should be replaced with tables in the database.

  1. Each user has a unique id (for example, with each registration, +1 is added to the number of registered users).

  2. Each question or answer has lists storing the id users who voted for the answer / question.

  3. When a user wants to vote, his id compared with the list of those already voted for this question / answer. If his id already in the list - then you can not give a vote again.