source: src/main/java/mk/ukim/finki/eglas/services/Impl/VotingCodeServiceImpl.java

main
Last change on this file was ac151d1, checked in by David <darsov2@…>, 4 months ago

initial

  • Property mode set to 100644
File size: 2.9 KB
Line 
1package mk.ukim.finki.eglas.services.Impl;
2
3import mk.ukim.finki.eglas.model.Citizen;
4import mk.ukim.finki.eglas.model.ElectionRealization;
5import mk.ukim.finki.eglas.model.VotingCode;
6import mk.ukim.finki.eglas.repository.VotingCodeRepository;
7import mk.ukim.finki.eglas.services.CitizenService;
8import mk.ukim.finki.eglas.services.ElectionRealizationService;
9import mk.ukim.finki.eglas.services.TurnoutService;
10import mk.ukim.finki.eglas.services.VotingCodeService;
11import org.springframework.stereotype.Service;
12
13import java.util.List;
14import java.util.UUID;
15
16@Service
17public class VotingCodeServiceImpl implements VotingCodeService {
18
19 private final VotingCodeRepository votingCodeRepository;
20 private final TurnoutService turnoutService;
21 private final CitizenService citizenService;
22 private final ElectionRealizationService electionRealizationService;
23
24 public VotingCodeServiceImpl(VotingCodeRepository votingCodeRepository, TurnoutService turnoutService, CitizenService citizenService, ElectionRealizationService electionRealizationService) {
25 this.votingCodeRepository = votingCodeRepository;
26 this.turnoutService = turnoutService;
27 this.citizenService = citizenService;
28 this.electionRealizationService = electionRealizationService;
29 }
30
31
32 @Override
33 public List<VotingCode> findAll() {
34 return votingCodeRepository.findAll();
35 }
36
37 @Override
38 public VotingCode findByCode(String code) {
39 return votingCodeRepository.findByCode(code);
40 }
41
42 @Override
43 public VotingCode generateCode(Long citizenId, Long realizationId) {
44 Boolean hasVoted = turnoutService.hasCitizenVotedOnRealization(citizenId, realizationId);
45 if(hasVoted)
46 {
47 return null;
48 }
49 try
50 {
51 return findByCitizenIdAndRealizationId(citizenId, realizationId);
52 }
53 catch (Exception e)
54 {
55 Citizen citizen = citizenService.findById(citizenId);
56 ElectionRealization electionRealization = electionRealizationService.findById(realizationId);
57 VotingCode votingCode = new VotingCode(citizen, electionRealization);
58 return votingCodeRepository.save(votingCode);
59 }
60
61 }
62
63 @Override
64 public VotingCode delete(Long id) {
65 return null;
66 }
67
68 @Override
69 public VotingCode findByCitizenIdAndRealizationId(Long citizenId, Long realizationId) {
70 return votingCodeRepository.findByCitizen_IdAndElectionRealization_Id(citizenId, realizationId)
71 .orElseThrow(() -> new RuntimeException("Voting code for the citizen not found!"));
72 }
73
74 @Override
75 public Citizen findCitizenByCode(UUID code) {
76 String codeString = code.toString();
77 return votingCodeRepository.findCitizenByCode(codeString)
78 .orElseThrow(() -> new RuntimeException("Citizen with given code not found or code expired!"));
79 }
80}
Note: See TracBrowser for help on using the repository browser.