Added the voting system
This commit is contained in:
@@ -7,6 +7,7 @@ import com.github.cato447.AbizeitungVotingSystem.repositories.CandidateRepositor
|
||||
import com.github.cato447.AbizeitungVotingSystem.repositories.CategoryRepository;
|
||||
import com.github.cato447.AbizeitungVotingSystem.repositories.VoterRepository;
|
||||
import com.github.cato447.AbizeitungVotingSystem.table.TableAction;
|
||||
import org.apache.juli.logging.Log;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
@@ -71,7 +72,7 @@ public class VotingController {
|
||||
|
||||
return "start.html";
|
||||
}
|
||||
|
||||
|
||||
public void sendSimpleMessage(
|
||||
String to, String subject, String text) {
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
@@ -94,7 +95,7 @@ public class VotingController {
|
||||
List<Category> categories = categoryRepository.findAll();
|
||||
model.addAttribute("candidates", candidates);
|
||||
model.addAttribute("categories", categories);
|
||||
sendSimpleMessage(name,"test", "test");
|
||||
//sendSimpleMessage(name,"test", "test");
|
||||
LOGGER.info(name + " is voting now");
|
||||
return "voting.html";
|
||||
}
|
||||
@@ -108,8 +109,14 @@ public class VotingController {
|
||||
}
|
||||
|
||||
@RequestMapping("/processVote")
|
||||
public String ProcessVote(@RequestParam String name) {
|
||||
LOGGER.info(name + " has voted");
|
||||
public String ProcessVote(@RequestParam String voteValues) {
|
||||
String[] partVoteValues = voteValues.split(",");
|
||||
for (String s: partVoteValues) {
|
||||
long candidateID = Long.valueOf(s);
|
||||
Candidate candidate = candidateRepository.findById(candidateID).get();
|
||||
candidate.votedFor();
|
||||
candidateRepository.save(candidate);
|
||||
}
|
||||
return "success.html";
|
||||
}
|
||||
|
||||
@@ -118,7 +125,6 @@ public class VotingController {
|
||||
try {
|
||||
if (password.equals("admin")) {
|
||||
List<Voter> voters = voterRepository.findAll();
|
||||
List<Candidate> candidates = candidateRepository.findAll();
|
||||
List<Category> categories = categoryRepository.findAll();
|
||||
model.addAttribute("voters", voters);
|
||||
model.addAttribute("categories", categories);
|
||||
@@ -126,7 +132,6 @@ public class VotingController {
|
||||
} else {
|
||||
LOGGER.error("Wrong Password");
|
||||
}
|
||||
LOGGER.error("Wrong Username");
|
||||
} catch (Exception e) {
|
||||
LOGGER.fatal("voters table is not existing!");
|
||||
}
|
||||
|
||||
@@ -4,9 +4,14 @@ import com.github.cato447.AbizeitungVotingSystem.entities.Candidate;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface CandidateRepository extends JpaRepository<Candidate, Integer> {
|
||||
|
||||
public Candidate findByName(String name);
|
||||
|
||||
Optional<Candidate> findById(Long id);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -20,16 +20,33 @@ h2.categoryHeader {
|
||||
color: #FFF;
|
||||
}
|
||||
|
||||
a.candidate {
|
||||
color: #fff;
|
||||
button {
|
||||
background: transparent;
|
||||
border: none;
|
||||
color: #FFF;
|
||||
font-size: .875rem;
|
||||
font-weight: 300;
|
||||
font-weight: normal;
|
||||
letter-spacing: .125rem;
|
||||
text-transform: uppercase;
|
||||
text-align: right;
|
||||
text-align: center;
|
||||
transition: opacity .25s .5s;
|
||||
}
|
||||
|
||||
#submitButton {
|
||||
margin-top: 5%;
|
||||
padding: .25em 0;
|
||||
border: 0;
|
||||
outline: 0;
|
||||
background: #bb1515;
|
||||
color: rgba(255, 255, 255, 0.85);
|
||||
font-size: 2rem;
|
||||
width: 500px;
|
||||
letter-spacing: .0625rem;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 3px 5px 1px rgba(0, 0, 0, 0.25);
|
||||
text-shadow: 0 -2px 0 rgba(0, 0, 0, 0.25), 0 1px 0 rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
html,
|
||||
body {
|
||||
width: 100%;
|
||||
|
||||
@@ -7,14 +7,56 @@
|
||||
<link th:href="@{/styles/voting.css}" rel="stylesheet" />
|
||||
</head>
|
||||
|
||||
|
||||
<body class="center-screen">
|
||||
|
||||
<script>
|
||||
function setColor(button) {
|
||||
groupButtons = document.querySelectorAll('[id^= ' + button.id.split("_")[0] + ']');
|
||||
groupButtons.forEach(button => {
|
||||
button.style.background = 'transparent';
|
||||
button.style.fontWeight = 'normal';
|
||||
});
|
||||
button.style.background = "#bb1515";
|
||||
button.style.fontWeight = 'bold';
|
||||
}
|
||||
|
||||
function rgbToHex(rgb) {
|
||||
rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
|
||||
var color = (rgb && rgb.length === 4) ? "#" +
|
||||
("0" + parseInt(rgb[1], 10).toString(16)).slice(-2) +
|
||||
("0" + parseInt(rgb[2], 10).toString(16)).slice(-2) +
|
||||
("0" + parseInt(rgb[3], 10).toString(16)).slice(-2) : '';
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
function getVotes() {
|
||||
groupButtons = document.querySelectorAll(".inputButton");
|
||||
var voteIds = [];
|
||||
groupButtons.forEach(button => {
|
||||
if (button.style.fontWeight == 'bold') {
|
||||
var str = button.id.split("_");
|
||||
voteIds.push(str[1].replace(/\D/g, ""));
|
||||
}
|
||||
});
|
||||
input = document.getElementById("voteValues");
|
||||
input.value = voteIds;
|
||||
}
|
||||
</script>
|
||||
|
||||
<h1>Wähle deine Kandidaten:</h1>
|
||||
<div th:each="category,iter : ${categories}">
|
||||
<h2 class="categoryHeader" th:text="${category.name}"></h2>
|
||||
<div th:each="candidate : ${category.candidateList}">
|
||||
<a class="candidate" th:text="${candidate.name}"></a>
|
||||
<div th:each="candidate : ${category.candidateList}" class="voteDiv">
|
||||
<button class="inputButton" th:id="|category${category.id}_candidate${candidate.id}|" th:text="${candidate.name}" th:onclick="|setColor(category${category.id}_candidate${candidate.id})|"></button>
|
||||
</div>
|
||||
</div>
|
||||
<form action="#" th:action="@{/processVote}" method="post">
|
||||
<input id="voteValues" type="hidden" name="voteValues" value="" />
|
||||
<button type="submit" id="submitButton" onclick="getVotes()">Auswahl bestätigen</button>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user