Devided Candidates in PossibleCandidates and Candidates

CHANGED: VotingController:
    - added implementation of PossibleCandidateRepository 'possibleCandidateRepository'
    - 'VerifyName' added checking for candidatesubmit_status
    - 'VerifyName' changed Candidate to PossibleCandidate
    - 'VerifyName' changed CandidateWrapper to PossibleCandidateWrapper
    - added method 'candidateSaving'

ADDED: PossibleCandidate:
    - added entity PossibleCandidate

CHANGED: Voter
    - removed 'vote_status' from parameterized constructor
    - added candidatesubmit_status
    - added getter for candidatesubmit_status

CHANGED: Candidate
    - added 'Category category' to parameterized constructor

ADDED: PossibleCandidateWrapper
    - added method 'addPossibleCandidate'
    - added getters/setters

REMOVED: CandidateWrapper
ADDED: PossibleCandidateRepository:
    - added method 'findByNameAndCategoryID' (!!! Not working)
    - added method 'findById'

CHANGED: TableAction:
    - added needed elements to parameter lists because of changes in the entitys voter, candidate and possibleCandidate
    - added method 'logPossibleCandidates'

CHANGED: addingCandidates.html:
    - changed the pointer to list according to the changes in 'VotingController.VerifyName'
This commit is contained in:
2020-11-30 21:40:44 +01:00
committed by Simon
parent 6a6ef2b96d
commit 361433bf22
10 changed files with 201 additions and 89 deletions

View File

@@ -1,11 +1,12 @@
package com.github.cato447.AbizeitungVotingSystem.controller; 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.Category;
import com.github.cato447.AbizeitungVotingSystem.entities.PossibleCandidate;
import com.github.cato447.AbizeitungVotingSystem.entities.Voter; import com.github.cato447.AbizeitungVotingSystem.entities.Voter;
import com.github.cato447.AbizeitungVotingSystem.helper.CandidateWrapper; import com.github.cato447.AbizeitungVotingSystem.helper.PossibleCandidateWrapper;
import com.github.cato447.AbizeitungVotingSystem.repositories.CandidateRepository; import com.github.cato447.AbizeitungVotingSystem.repositories.CandidateRepository;
import com.github.cato447.AbizeitungVotingSystem.repositories.CategoryRepository; import com.github.cato447.AbizeitungVotingSystem.repositories.CategoryRepository;
import com.github.cato447.AbizeitungVotingSystem.repositories.PossibleCandidateRepository;
import com.github.cato447.AbizeitungVotingSystem.repositories.VoterRepository; import com.github.cato447.AbizeitungVotingSystem.repositories.VoterRepository;
import com.github.cato447.AbizeitungVotingSystem.table.TableAction; import com.github.cato447.AbizeitungVotingSystem.table.TableAction;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -46,6 +47,9 @@ public class VotingController {
@Autowired @Autowired
CategoryRepository categoryRepository; CategoryRepository categoryRepository;
@Autowired
PossibleCandidateRepository possibleCandidateRepository;
@Autowired @Autowired
JavaMailSender emailSender; JavaMailSender emailSender;
@@ -64,7 +68,7 @@ public class VotingController {
} }
if (candidateRepository.findAll().size() == 0) { if (candidateRepository.findAll().size() == 0) {
tableAction.setUpCandidates(candidateRepository); tableAction.setUpCandidates(candidateRepository, categoryRepository);
LOGGER.info("Candidates successfully set up"); LOGGER.info("Candidates successfully set up");
} }
} }
@@ -96,9 +100,13 @@ public class VotingController {
if (name.strip().toLowerCase().matches("[a-z]+\\.[a-z]+@adolfinum+\\.de$")) { if (name.strip().toLowerCase().matches("[a-z]+\\.[a-z]+@adolfinum+\\.de$")) {
try { try {
Voter voter = voterRepository.findByEmail(name.toLowerCase().strip()); Voter voter = voterRepository.findByEmail(name.toLowerCase().strip());
LOGGER.warn(voter.getEmail());
if (voter.getVote_status()) { if (voter.getVote_status()) {
LOGGER.warn(name + " has already voted"); LOGGER.warn(name + " has already voted");
return "errors/alreadyVoted.html"; return "errors/alreadyVoted.html";
} else if (voter.getCandidatesubmit_status()) {
LOGGER.warn(name + " has already submitted its candidates");
return "errors/alreadysubmittedcandidates.html";
} else { } else {
if(candidatesAdded) { if(candidatesAdded) {
List<Category> categories = categoryRepository.findAll(); List<Category> categories = categoryRepository.findAll();
@@ -108,15 +116,13 @@ public class VotingController {
LOGGER.info(name + " is voting now"); LOGGER.info(name + " is voting now");
return "voting.html"; return "voting.html";
} else { } else {
CandidateWrapper candidates = new CandidateWrapper(); PossibleCandidateWrapper possibleCandidates = new PossibleCandidateWrapper();
List<Category> categories = categoryRepository.findAll(); List<Category> categories = categoryRepository.findAll();
for (int i = 0; i < categories.size(); i++){ for (int i = 0; i < categories.size(); i++){
candidates.addCandidate(new Candidate()); possibleCandidates.addPossibleCandidate(new PossibleCandidate());
} }
model.addAttribute("categories", categories); model.addAttribute("categories", categories);
model.addAttribute("form", candidates); model.addAttribute("form", possibleCandidates);
LOGGER.info(name + " is submitting candidates"); LOGGER.info(name + " is submitting candidates");
return "addingCandidates.html"; return "addingCandidates.html";
} }
@@ -131,8 +137,22 @@ public class VotingController {
} }
@RequestMapping("/saveCandidates") @RequestMapping("/saveCandidates")
public String candidateSaving(@ModelAttribute CandidateWrapper candidates){ public String candidateSaving(@ModelAttribute PossibleCandidateWrapper possibleCandidates){
LOGGER.info(tableAction.logCandidates(candidates.getCandidates(), categoryRepository)); LOGGER.info(tableAction.logPossibleCandidates(possibleCandidates.getPossibleCandidates(), categoryRepository));
LinkedList<PossibleCandidate> posCandidates = possibleCandidates.getPossibleCandidates();
long index = 1;
for (PossibleCandidate posCandidate : posCandidates){
if (posCandidate.getName() != "") {
if (possibleCandidateRepository.findByName(posCandidate.getName()) != null) {
PossibleCandidate p = possibleCandidateRepository.findByName(posCandidate.getName());
p.setVotes(p.getVotes() + 1);
} else {
PossibleCandidate possibleCandidate = new PossibleCandidate(posCandidate.getName(), categoryRepository.findById(index).get());
possibleCandidateRepository.save(possibleCandidate);
}
}
index++;
}
return "candidateAddingSuccessful.html"; return "candidateAddingSuccessful.html";
} }

View File

@@ -10,10 +10,11 @@ public class Candidate implements Comparable<Candidate>{
super(); super();
} }
public Candidate(String name) { public Candidate(String name, Category category) {
super(); super();
this.name = name; this.name = name;
this.votes = 0; this.votes = 0;
this.category = category;
} }
@Id @Id

View File

@@ -0,0 +1,55 @@
package com.github.cato447.AbizeitungVotingSystem.entities;
import javax.persistence.*;
@Entity
@Table(name = "possibleCandidates")
public class PossibleCandidate{
public PossibleCandidate() {
super();
}
public PossibleCandidate(String name, Category category) {
super();
this.name = name;
this.category = category;
this.votes = 1;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private int votes;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
private Category category;
public Long getId() {
return id;
}
public String getName() {
return name;
}
public Category getCategory() {return category;}
public int getVotes() {
return votes;
}
public void setName(String name) {
this.name = name;
}
public void setCategory(Category category) {
this.category = category;
}
public void setVotes(int votes) {
this.votes = votes;
}
}

View File

@@ -10,9 +10,10 @@ public class Voter {
super(); super();
} }
public Voter(String email, boolean vote_status) { public Voter(String email) {
this.email = email; this.email = email;
this.vote_status= vote_status; this.vote_status = false;
this.candidatesubmit_status = false;
} }
@Id @Id
@@ -20,6 +21,7 @@ public class Voter {
private Long id; private Long id;
private String email; private String email;
private Boolean vote_status; private Boolean vote_status;
private Boolean candidatesubmit_status;
public Long getId() { public Long getId() {
return id; return id;
@@ -33,6 +35,10 @@ public class Voter {
return vote_status; return vote_status;
} }
public Boolean getCandidatesubmit_status() {
return candidatesubmit_status;
}
public void vote(){ public void vote(){
vote_status = true; vote_status = true;
} }

View File

@@ -1,23 +0,0 @@
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;
}
}

View File

@@ -0,0 +1,27 @@
package com.github.cato447.AbizeitungVotingSystem.helper;
import com.github.cato447.AbizeitungVotingSystem.entities.PossibleCandidate;
import java.util.LinkedList;
public class PossibleCandidateWrapper {
private LinkedList<PossibleCandidate> possibleCandidates;
public PossibleCandidateWrapper(){
possibleCandidates = new LinkedList<>();
}
public void addPossibleCandidate(PossibleCandidate possibleCandidate){
this.possibleCandidates.add(possibleCandidate);
}
public LinkedList<PossibleCandidate> getPossibleCandidates() {
return possibleCandidates;
}
public void setPossibleCandidates(LinkedList<PossibleCandidate> possibleCandidates) {
this.possibleCandidates = possibleCandidates;
}
}

View File

@@ -0,0 +1,16 @@
package com.github.cato447.AbizeitungVotingSystem.repositories;
import com.github.cato447.AbizeitungVotingSystem.entities.PossibleCandidate;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface PossibleCandidateRepository extends JpaRepository<PossibleCandidate, Integer> {
public PossibleCandidate findByNameAndCategoryID(String name, Long category_id);
Optional<PossibleCandidate> findById(Long id);
}

View File

@@ -3,9 +3,11 @@ package com.github.cato447.AbizeitungVotingSystem.table;
import com.github.cato447.AbizeitungVotingSystem.controller.VotingController; import com.github.cato447.AbizeitungVotingSystem.controller.VotingController;
import com.github.cato447.AbizeitungVotingSystem.entities.Candidate; import com.github.cato447.AbizeitungVotingSystem.entities.Candidate;
import com.github.cato447.AbizeitungVotingSystem.entities.Category; import com.github.cato447.AbizeitungVotingSystem.entities.Category;
import com.github.cato447.AbizeitungVotingSystem.entities.PossibleCandidate;
import com.github.cato447.AbizeitungVotingSystem.entities.Voter; import com.github.cato447.AbizeitungVotingSystem.entities.Voter;
import com.github.cato447.AbizeitungVotingSystem.repositories.CandidateRepository; import com.github.cato447.AbizeitungVotingSystem.repositories.CandidateRepository;
import com.github.cato447.AbizeitungVotingSystem.repositories.CategoryRepository; import com.github.cato447.AbizeitungVotingSystem.repositories.CategoryRepository;
import com.github.cato447.AbizeitungVotingSystem.repositories.PossibleCandidateRepository;
import com.github.cato447.AbizeitungVotingSystem.repositories.VoterRepository; import com.github.cato447.AbizeitungVotingSystem.repositories.VoterRepository;
import java.io.File; import java.io.File;
@@ -18,11 +20,16 @@ public class TableAction {
} }
public void addCandidate(String name, CandidateRepository candidateRepository){ public void addCandidate(String name, long category_id, CategoryRepository categoryRepository,CandidateRepository candidateRepository){
Candidate candidate = new Candidate(name); Candidate candidate = new Candidate(name, categoryRepository.findById(category_id).get());
candidateRepository.save(candidate); candidateRepository.save(candidate);
} }
public void addPossibleCandidate(String name, long category_id, CategoryRepository categoryRepository, PossibleCandidateRepository possibleCandidateRepository){
PossibleCandidate possibleCandidate = new PossibleCandidate(name, categoryRepository.findById(category_id).get());
possibleCandidateRepository.save(possibleCandidate);
}
public void updateVotingStatus(String email, VoterRepository voterRepository){ public void updateVotingStatus(String email, VoterRepository voterRepository){
Voter voter = voterRepository.findByEmail(email); Voter voter = voterRepository.findByEmail(email);
voter.vote(); voter.vote();
@@ -43,7 +50,7 @@ public class TableAction {
ArrayList<Voter> voters = new ArrayList<Voter>(); ArrayList<Voter> voters = new ArrayList<Voter>();
while (myReader.hasNextLine()) { while (myReader.hasNextLine()) {
String email = myReader.nextLine(); String email = myReader.nextLine();
Voter voter = new Voter(email, false); Voter voter = new Voter(email);
voters.add(voter); voters.add(voter);
} }
voterRepository.saveAll(voters); voterRepository.saveAll(voters);
@@ -54,14 +61,14 @@ public class TableAction {
} }
} }
public void setUpCandidates(CandidateRepository candidateRepository){ public void setUpCandidates(CandidateRepository candidateRepository, CategoryRepository categoryRepository){
ArrayList<String> names = new ArrayList<>(); ArrayList<String> names = new ArrayList<>();
Collections.addAll(names, "Greta Bentgens", "Laura König", "Aaron Glos", "Lukas Boy", "Frau Meyering" Collections.addAll(names, "Greta Bentgens", "Laura König", "Aaron Glos", "Lukas Boy", "Frau Meyering"
, "Frau Adams", "Herr Petering", "Frau Milde", "Frau Meyer"); , "Frau Adams", "Herr Petering", "Frau Milde", "Frau Meyer");
ArrayList<Candidate> candidates = new ArrayList<>(); ArrayList<Candidate> candidates = new ArrayList<>();
for (String name: names) { for (String name: names) {
Candidate candidate = new Candidate(name); Candidate candidate = new Candidate(name, categoryRepository.findById(20l).get());
candidates.add(candidate); candidates.add(candidate);
} }
candidateRepository.saveAll(candidates); candidateRepository.saveAll(candidates);
@@ -138,6 +145,20 @@ public class TableAction {
return formatAsTable(rows); return formatAsTable(rows);
} }
public String logPossibleCandidates(LinkedList<PossibleCandidate> possibleCandidates, 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 (PossibleCandidate possibleCandidate: possibleCandidates) {
Category category = categoryRepository.findById(i).get();
rows.add(Arrays.asList("" + i, possibleCandidate.getName(), "" + 0, "" + category.getId()));
i++;
}
return formatAsTable(rows);
}
private String formatAsTable(List<List<String>> rows) { private String formatAsTable(List<List<String>> rows) {
int[] maxLengths = new int[rows.get(0).size()]; int[] maxLengths = new int[rows.get(0).size()];
for (List<String> row : rows) for (List<String> row : rows)

View File

@@ -1,46 +1,36 @@
Wer wird nie von zuhause ausziehen? 1. Wer wird nie von zuhause ausziehen? (Schüler)
Wer bekommt die meisten Punkte in Flensburg (Wackel) 1. Wer ist der/die kreativste Zuspätkommer/in (Schüler)
Wer ist der/die kreativste Zuspätkommer/in 1. Mit wem wird man am ehesten in der Zukunft angeben zur Schule gegangen zu sein? (Schüler)
Mit wem wird man am ehesten in der Zukunft angeben zur Schule gegangen zu sein? 1. Wer wohnt im Fittnessstudio? (Schüler)
Wer wohnt im Fittnessstudio? 1. Wer ist der coolste Leherer?
Wer ist der Adonis/Afrodite der Stufe? 1. Wer ist die coolste Lehrerin?
Wer landet am ehesten im Bundestag (Wackel) 1. Wer macht den besten Matheunterricht?
Wer landet als erstes in der Ausnüchterungszelle? 1. Welcher Lehrer hat den Beruf verfehlt?
Wer ist der coolste Leherer? 1. Welcher Lehrer ist nie da?
Wer ist die coolste Lehrerin? 1. Wer hat das Internet erfunden?
Wer macht den besten Matheunterricht? 1. Wer sponsort Schollin? (Schüler)
Welcher Lehrer hat den Beruf verfehlt? 1. Wer ist am engagiertesten? (Schüler)
Welcher Lehrer ist nie da? 1. Wer wird Bundeskanzler? (Schüler)
Wer hat das Internet erfunden? 1. Wer hat das Kimpeltum gegründet? (Schüler)
Wer sponsort Schollin? 1. Wer saß am längsten im Klassenschrank? (Schüler)
Wer hat nur ein Outfit? 1. Wer hat die gößte Sauklaue? (Leherer)
Wer könnte Modeln? 1. Wer gründet das nächste Google? (Schüler)
Wer ist am engagiertesten? 1. Wer kommt am schnellsten in den Knast? (Schüler)
Wer hat das größte Ego? (Wackel) 1. Top 3-Pärchen? (Schüler)
Wer wird Bundeskanzler? (Lehrer und Schüler) 1. Top 3 Lehrergespanne? (Schüler)
Wer wird eine Sekte gründen? 1. Wer ist der motiviertester Lehrer?
Wer saß am längsten im Klassenschrank? 1. Wer ist Google auf zwei Beinen(Schüler)?
Wer hat die gößte Sauklaue (Leherer)? 1. Wer hat den besten Style (Schüler)?
Wer hat die größte Sauklaue (Schüler)? 1. Wer hat den besten Style (Lehrer)?
Wer gründet das nächste Google? 1. Wer währe ein gutes Pärchen (Lehrer)?
Wer kommt am schnellsten in den Knast? 1. Wer trinkt am meisten Kaffe (Schüler)?
Top 3-Pärchen? 1. Wer trinkt am meisten Kaffe (Lehrer)?
Top 3 Lehrergespanne? 1. Tischflip?
Wer ist der motiviertester Lehrer? 1. Wer wird Harzer?
Wer ist Google auf zwei Beinen(Leherer)? 1. Wer hat Wiedererkennungswert (Schüler)?
Wer ist Google auf zwei Beinen(Schüler)? 1. Wen erkennt man an der Lache (Schüler)?
Wer hat den besten Style (Mädchen)? 1. Wer bringt die dümmsten Witze? (Lehrer)
Wer hat den besten Style (Jungs)? 1. Wer hat den meisten Schwachsinn erzählt? (Lehrer)
Wer währe ein gutes Pärchen (Lehrer)? 1. Welche Lehrer machen nie das, was die Schüler wollten? (Lehrer)
Wer währe ein gutes Pärchen (Schüler)? 1. Wer isst immer? (Lehrer)
Wer trinkt am meisten Kaffe? 1. Wer hat die meisten peinlichen Momente gebracht?
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?

View File

@@ -20,8 +20,7 @@
<tbody> <tbody>
<tr th:each="category, itemStat : ${categories}"> <tr th:each="category, itemStat : ${categories}">
<td th:text="${category.name}"></td> <td th:text="${category.name}"></td>
<td><input th:field="*{candidates[__${itemStat.index}__].name}" /></td> <td><input th:field="*{possibleCandidates[__${itemStat.index}__].name}" /></td>
</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>