Change vote status of voter if voter voted

Through that voters can only vote once
This commit is contained in:
2020-11-28 00:17:16 +01:00
parent 7b4f460445
commit 81befda3a9
3 changed files with 11 additions and 1 deletions

View File

@@ -95,6 +95,7 @@ public class VotingController {
List<Category> categories = categoryRepository.findAll(); List<Category> categories = categoryRepository.findAll();
model.addAttribute("candidates", candidates); model.addAttribute("candidates", candidates);
model.addAttribute("categories", categories); model.addAttribute("categories", categories);
model.addAttribute("name", name);
//sendSimpleMessage(name,"test", "test"); //sendSimpleMessage(name,"test", "test");
LOGGER.info(name + " is voting now"); LOGGER.info(name + " is voting now");
return "voting.html"; return "voting.html";
@@ -109,7 +110,7 @@ public class VotingController {
} }
@RequestMapping("/processVote") @RequestMapping("/processVote")
public String ProcessVote(@RequestParam String voteValues) { public String ProcessVote(@RequestParam String voteValues, @RequestParam String voterEmail) {
String[] partVoteValues = voteValues.split(","); String[] partVoteValues = voteValues.split(",");
for (String s: partVoteValues) { for (String s: partVoteValues) {
long candidateID = Long.valueOf(s); long candidateID = Long.valueOf(s);
@@ -117,6 +118,10 @@ public class VotingController {
candidate.votedFor(); candidate.votedFor();
candidateRepository.save(candidate); candidateRepository.save(candidate);
} }
Voter voter = voterRepository.findByEmail(voterEmail);
voter.vote();
voterRepository.save(voter);
LOGGER.info(voterEmail + " has voted!");
return "success.html"; return "success.html";
} }

View File

@@ -32,4 +32,8 @@ public class Voter {
public Boolean getVote_status() { public Boolean getVote_status() {
return vote_status; return vote_status;
} }
public void vote(){
vote_status = true;
}
} }

View File

@@ -54,6 +54,7 @@
</div> </div>
<form action="#" th:action="@{/processVote}" method="post"> <form action="#" th:action="@{/processVote}" method="post">
<input id="voteValues" type="hidden" name="voteValues" value="" /> <input id="voteValues" type="hidden" name="voteValues" value="" />
<input id="voterName" type="hidden" name="voterEmail" th:value="${name}" />
<button type="submit" id="submitButton" onclick="getVotes()">Auswahl bestätigen</button> <button type="submit" id="submitButton" onclick="getVotes()">Auswahl bestätigen</button>
</form> </form>