Added candidate suggestion
- CHANGES: VotingControler:
-- added: switch to toogle between candidate suggestion and voting
-- added: suport for adding candidate names
-- added: method to save given candidate names
- ADDED: addingCandidates:
-- added: site to enter candidate suggestions
- ADDED: CandidateWrapper:
-- added: complete class to help with data transferation through th:object
- CHANGES: Category:
-- added: constructor with name parameter for logging purposes
- CHANGES: Candidate:
-- added: setters for class variables
- CHANGES: TableAction:
-- refactoring: moved method to vote and update vote status from VotingController to TableActions class
- CHANGES: Resources:
-- added: Files with data to feed to the tables
- ADDED: voteSuccessful:
-- added fallback html page
- REFACTORING: candidateAddingSuccessful:
-- renamed file
This commit is contained in:
+46
-24
@@ -3,16 +3,17 @@ package com.github.cato447.AbizeitungVotingSystem.controller;
|
||||
import com.github.cato447.AbizeitungVotingSystem.entities.Candidate;
|
||||
import com.github.cato447.AbizeitungVotingSystem.entities.Category;
|
||||
import com.github.cato447.AbizeitungVotingSystem.entities.Voter;
|
||||
import com.github.cato447.AbizeitungVotingSystem.helper.CandidateWrapper;
|
||||
import com.github.cato447.AbizeitungVotingSystem.repositories.CandidateRepository;
|
||||
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;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@@ -21,12 +22,15 @@ import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.*;
|
||||
|
||||
@Controller
|
||||
public class VotingController {
|
||||
|
||||
private Boolean candidatesAdded = false;
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger(VotingController.class);
|
||||
|
||||
private TableAction tableAction = new TableAction();
|
||||
@@ -45,18 +49,28 @@ public class VotingController {
|
||||
@Autowired
|
||||
JavaMailSender emailSender;
|
||||
|
||||
@RequestMapping("/")
|
||||
public String WelcomeSite() {
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
LOGGER.info("setups");
|
||||
if (voterRepository.findAll().size() == 0) {
|
||||
tableAction.setUpVoters(voterRepository);
|
||||
LOGGER.info("Voters successfully set up");
|
||||
}
|
||||
|
||||
if (categoryRepository.findAll().size() == 0){
|
||||
tableAction.setUpCategories(categoryRepository);
|
||||
LOGGER.info("Categories successfully set up");
|
||||
}
|
||||
|
||||
if (candidateRepository.findAll().size() == 0) {
|
||||
tableAction.setUpCandidates(candidateRepository);
|
||||
LOGGER.info("Candidates successfully set up");
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping("/")
|
||||
public String WelcomeSite() {
|
||||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
|
||||
.getRequest();
|
||||
|
||||
@@ -65,11 +79,6 @@ public class VotingController {
|
||||
LOGGER.info("User IP: " + request.getRemoteAddr());
|
||||
ipAddresses.add(currentIpAddress);
|
||||
}
|
||||
|
||||
tableAction.logVoters(voterRepository);
|
||||
tableAction.logCandidates(candidateRepository);
|
||||
tableAction.logCategories(categoryRepository);
|
||||
|
||||
return "start.html";
|
||||
}
|
||||
|
||||
@@ -91,14 +100,26 @@ public class VotingController {
|
||||
LOGGER.warn(name + " has already voted");
|
||||
return "errors/alreadyVoted.html";
|
||||
} else {
|
||||
List<Candidate> candidates = candidateRepository.findAll();
|
||||
List<Category> categories = categoryRepository.findAll();
|
||||
model.addAttribute("candidates", candidates);
|
||||
model.addAttribute("categories", categories);
|
||||
model.addAttribute("name", name);
|
||||
//sendSimpleMessage(name,"test", "test");
|
||||
LOGGER.info(name + " is voting now");
|
||||
return "voting.html";
|
||||
if(candidatesAdded) {
|
||||
List<Category> categories = categoryRepository.findAll();
|
||||
model.addAttribute("categories", categories);
|
||||
model.addAttribute("name", name);
|
||||
//sendSimpleMessage(name,"test", "test");
|
||||
LOGGER.info(name + " is voting now");
|
||||
return "voting.html";
|
||||
} else {
|
||||
CandidateWrapper candidates = new CandidateWrapper();
|
||||
List<Category> categories = categoryRepository.findAll();
|
||||
|
||||
for (int i = 0; i < categories.size(); i++){
|
||||
candidates.addCandidate(new Candidate());
|
||||
}
|
||||
|
||||
model.addAttribute("categories", categories);
|
||||
model.addAttribute("form", candidates);
|
||||
LOGGER.info(name + " is submitting candidates");
|
||||
return "addingCandidates.html";
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(name + " is not allowed to vote");
|
||||
@@ -109,20 +130,21 @@ public class VotingController {
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping("/saveCandidates")
|
||||
public String candidateSaving(@ModelAttribute CandidateWrapper candidates){
|
||||
LOGGER.info(tableAction.logCandidates(candidates.getCandidates(), categoryRepository));
|
||||
return "candidateAddingSuccessful.html";
|
||||
}
|
||||
|
||||
@RequestMapping("/processVote")
|
||||
public String ProcessVote(@RequestParam String voteValues, @RequestParam String voterEmail) {
|
||||
String[] partVoteValues = voteValues.split(",");
|
||||
for (String s: partVoteValues) {
|
||||
long candidateID = Long.valueOf(s);
|
||||
Candidate candidate = candidateRepository.findById(candidateID).get();
|
||||
candidate.votedFor();
|
||||
candidateRepository.save(candidate);
|
||||
tableAction.voteFor(s, candidateRepository);
|
||||
}
|
||||
Voter voter = voterRepository.findByEmail(voterEmail);
|
||||
voter.vote();
|
||||
voterRepository.save(voter);
|
||||
tableAction.updateVotingStatus(voterEmail,voterRepository);
|
||||
LOGGER.info(voterEmail + " has voted!");
|
||||
return "success.html";
|
||||
return "voteSuccessful.html";
|
||||
}
|
||||
|
||||
@RequestMapping("/dashboard")
|
||||
|
||||
@@ -42,6 +42,18 @@ public class Candidate implements Comparable<Candidate>{
|
||||
|
||||
public Category getCategory() {return category;}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setVotes(Integer votes) {
|
||||
this.votes = votes;
|
||||
}
|
||||
|
||||
public void setCategory(Category category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public void votedFor() {
|
||||
this.votes += 1;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,11 @@ public class Category {
|
||||
this.candidateList = candidateList;
|
||||
}
|
||||
|
||||
public Category(String name){
|
||||
super();
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy= GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.github.cato447.AbizeitungVotingSystem.helper;
|
||||
|
||||
import com.github.cato447.AbizeitungVotingSystem.entities.Candidate;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
|
||||
public class CandidateWrapper {
|
||||
|
||||
private LinkedList<Candidate> candidates;
|
||||
|
||||
public CandidateWrapper(){
|
||||
candidates = new LinkedList<>();
|
||||
}
|
||||
|
||||
public void addCandidate(Candidate candidate){
|
||||
this.candidates.add(candidate);
|
||||
}
|
||||
|
||||
public LinkedList<Candidate> getCandidates() {
|
||||
return candidates;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.github.cato447.AbizeitungVotingSystem.table;
|
||||
|
||||
import com.github.cato447.AbizeitungVotingSystem.controller.VotingController;
|
||||
import com.github.cato447.AbizeitungVotingSystem.entities.Candidate;
|
||||
import com.github.cato447.AbizeitungVotingSystem.entities.Category;
|
||||
import com.github.cato447.AbizeitungVotingSystem.entities.Voter;
|
||||
@@ -22,6 +23,19 @@ public class TableAction {
|
||||
candidateRepository.save(candidate);
|
||||
}
|
||||
|
||||
public void updateVotingStatus(String email, VoterRepository voterRepository){
|
||||
Voter voter = voterRepository.findByEmail(email);
|
||||
voter.vote();
|
||||
voterRepository.save(voter);
|
||||
}
|
||||
|
||||
public void voteFor(String id, CandidateRepository candidateRepository){
|
||||
long candidateID = Long.valueOf(id);
|
||||
Candidate candidate = candidateRepository.findById(candidateID).get();
|
||||
candidate.votedFor();
|
||||
candidateRepository.save(candidate);
|
||||
}
|
||||
|
||||
public void setUpVoters(VoterRepository voterRepository){
|
||||
try {
|
||||
File emailFile = new File("src/main/resources/Q2_emails.txt");
|
||||
@@ -53,6 +67,25 @@ public class TableAction {
|
||||
candidateRepository.saveAll(candidates);
|
||||
}
|
||||
|
||||
public void setUpCategories(CategoryRepository categoryRepository){
|
||||
ArrayList<String> names = new ArrayList<>();
|
||||
try {
|
||||
File categoryFile = new File("src/main/resources/Categories.txt");
|
||||
Scanner myReader = new Scanner(categoryFile);
|
||||
ArrayList<Category> categories = new ArrayList<Category>();
|
||||
while (myReader.hasNextLine()) {
|
||||
String name = myReader.nextLine();
|
||||
Category category = new Category(name);
|
||||
categories.add(category);
|
||||
}
|
||||
categoryRepository.saveAll(categories);
|
||||
myReader.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("An error occurred.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public String logCategories(CategoryRepository categoryRepository){
|
||||
List<List<String>> rows = new ArrayList<>();
|
||||
List<String> headers = Arrays.asList("Id", "Name", "Candidates");
|
||||
@@ -80,7 +113,7 @@ public class TableAction {
|
||||
|
||||
}
|
||||
|
||||
public String logCandidates(CandidateRepository candidateRepository){
|
||||
public String logCandidatesRepository(CandidateRepository candidateRepository){
|
||||
List<List<String>> rows = new ArrayList<>();
|
||||
List<String> headers = Arrays.asList("Id", "Name", "Votes");
|
||||
rows.add(headers);
|
||||
@@ -91,6 +124,20 @@ public class TableAction {
|
||||
return formatAsTable(rows);
|
||||
}
|
||||
|
||||
public String logCandidates(LinkedList<Candidate> candidates, CategoryRepository categoryRepository){
|
||||
List<List<String>> rows = new ArrayList<>();
|
||||
List<String> headers = Arrays.asList("Id", "Name", "Votes", "Category_ID");
|
||||
rows.add(headers);
|
||||
long i = 1;
|
||||
for (Candidate candidate: candidates) {
|
||||
Category category = categoryRepository.findById(i).get();
|
||||
rows.add(Arrays.asList("" + i, candidate.getName(), "" + 0, "" + category.getId()));
|
||||
i++;
|
||||
}
|
||||
|
||||
return formatAsTable(rows);
|
||||
}
|
||||
|
||||
private String formatAsTable(List<List<String>> rows) {
|
||||
int[] maxLengths = new int[rows.get(0).size()];
|
||||
for (List<String> row : rows)
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
Wer wird nie von zuhause ausziehen?
|
||||
Wer bekommt die meisten Punkte in Flensburg (Wackel)
|
||||
Wer ist der/die kreativste Zuspätkommer/in
|
||||
Mit wem wird man am ehesten in der Zukunft angeben zur Schule gegangen zu sein?
|
||||
Wer wohnt im Fittnessstudio?
|
||||
Wer ist der Adonis/Afrodite der Stufe?
|
||||
Wer landet am ehesten im Bundestag (Wackel)
|
||||
Wer landet als erstes in der Ausnüchterungszelle?
|
||||
Wer ist der coolste Leherer?
|
||||
Wer ist die coolste Lehrerin?
|
||||
Wer macht den besten Matheunterricht?
|
||||
Welcher Lehrer hat den Beruf verfehlt?
|
||||
Welcher Lehrer ist nie da?
|
||||
Wer hat das Internet erfunden?
|
||||
Wer sponsort Schollin?
|
||||
Wer hat nur ein Outfit?
|
||||
Wer könnte Modeln?
|
||||
Wer ist am engagiertesten?
|
||||
Wer hat das größte Ego? (Wackel)
|
||||
Wer wird Bundeskanzler? (Lehrer und Schüler)
|
||||
Wer wird eine Sekte gründen?
|
||||
Wer saß am längsten im Klassenschrank?
|
||||
Wer hat die gößte Sauklaue (Leherer)?
|
||||
Wer hat die größte Sauklaue (Schüler)?
|
||||
Wer gründet das nächste Google?
|
||||
Wer kommt am schnellsten in den Knast?
|
||||
Top 3-Pärchen?
|
||||
Top 3 Lehrergespanne?
|
||||
Wer ist der motiviertester Lehrer?
|
||||
Wer ist Google auf zwei Beinen(Leherer)?
|
||||
Wer ist Google auf zwei Beinen(Schüler)?
|
||||
Wer hat den besten Style (Mädchen)?
|
||||
Wer hat den besten Style (Jungs)?
|
||||
Wer währe ein gutes Pärchen (Lehrer)?
|
||||
Wer währe ein gutes Pärchen (Schüler)?
|
||||
Wer trinkt am meisten Kaffe?
|
||||
Tischflip
|
||||
Wer wird Harzer?
|
||||
Wer hat Wiedererkennungswert?
|
||||
Wen erkennt man an der Lache?
|
||||
Wer bringt die dümmsten Witze?
|
||||
Wer hat den meisten Schwachsinn erzählt?
|
||||
Welche Lehrer machen nie das, was die Schüler wollten?
|
||||
Welcher Kurs hat am meisten Kuchen gegessen?
|
||||
Welcher Kurs hat am meisten Kaffee getrunken?
|
||||
Wer hat die meisten peinlichen Momente gebracht?
|
||||
@@ -0,0 +1,33 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Schlage für jede Kategorie eine/n Kandiadt/en vor:</h1>
|
||||
<form action="#" th:action="@{/saveCandidates}" th:object="${form}" method="post">
|
||||
<fieldset>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th> Kategorie</th>
|
||||
<th> Kandidat/in</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="category, itemStat : ${categories}">
|
||||
<td th:text="${category.name}"></td>
|
||||
<td><input th:field="*{candidates[__${itemStat.index}__].name}" /></td>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<input type="submit" id="submitButton" th:value="Confirm"></button>
|
||||
</fieldset>
|
||||
</form>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user