1 | package mk.ukim.finki.eglas.services.Impl;
|
---|
2 |
|
---|
3 | import mk.ukim.finki.eglas.model.Citizen;
|
---|
4 | import mk.ukim.finki.eglas.model.ElectionRealization;
|
---|
5 | import mk.ukim.finki.eglas.model.VotingCode;
|
---|
6 | import mk.ukim.finki.eglas.repository.VotingCodeRepository;
|
---|
7 | import mk.ukim.finki.eglas.services.CitizenService;
|
---|
8 | import mk.ukim.finki.eglas.services.ElectionRealizationService;
|
---|
9 | import mk.ukim.finki.eglas.services.TurnoutService;
|
---|
10 | import mk.ukim.finki.eglas.services.VotingCodeService;
|
---|
11 | import org.springframework.stereotype.Service;
|
---|
12 |
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.UUID;
|
---|
15 |
|
---|
16 | @Service
|
---|
17 | public 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 | }
|
---|