1 | package mk.ukim.finki.eglas.services.Impl;
|
---|
2 |
|
---|
3 | import jakarta.transaction.Transactional;
|
---|
4 | import mk.ukim.finki.eglas.model.Election;
|
---|
5 | import mk.ukim.finki.eglas.model.ElectionRealization;
|
---|
6 | import mk.ukim.finki.eglas.repository.CandidatesElectionRealizationRepository;
|
---|
7 | import mk.ukim.finki.eglas.repository.CandidatesListElectionRealizationRepository;
|
---|
8 | import mk.ukim.finki.eglas.repository.ElectionRealizationRepository;
|
---|
9 | import mk.ukim.finki.eglas.services.CitizenService;
|
---|
10 | import mk.ukim.finki.eglas.services.ElectionRealizationService;
|
---|
11 | import mk.ukim.finki.eglas.services.ElectionService;
|
---|
12 | import mk.ukim.finki.eglas.services.VoteIdentificationCodeService;
|
---|
13 | import org.springframework.stereotype.Service;
|
---|
14 |
|
---|
15 | import java.time.LocalDate;
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | @Service
|
---|
19 | public class ElectionRealizationServiceImpl implements ElectionRealizationService {
|
---|
20 | private final ElectionRealizationRepository electionRealizationRepository;
|
---|
21 | private final CandidatesListElectionRealizationRepository candidatesListElectionRealizationRepository;
|
---|
22 | private final CandidatesElectionRealizationRepository candidatesElectionRealizationRepository;
|
---|
23 | private final ElectionService electionService;
|
---|
24 | private final VoteIdentificationCodeService voteIdentificationCodeService;
|
---|
25 | private final CitizenService citizenService;
|
---|
26 |
|
---|
27 | public ElectionRealizationServiceImpl(ElectionRealizationRepository electionRealizationRepository, CandidatesListElectionRealizationRepository candidatesListElectionRealizationRepository, CandidatesElectionRealizationRepository candidatesElectionRealizationRepository, ElectionService electionService, VoteIdentificationCodeService voteIdentificationCodeService, CitizenService citizenService) {
|
---|
28 | this.electionRealizationRepository = electionRealizationRepository;
|
---|
29 | this.candidatesListElectionRealizationRepository = candidatesListElectionRealizationRepository;
|
---|
30 | this.candidatesElectionRealizationRepository = candidatesElectionRealizationRepository;
|
---|
31 | this.electionService = electionService;
|
---|
32 | this.voteIdentificationCodeService = voteIdentificationCodeService;
|
---|
33 | this.citizenService = citizenService;
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public List<ElectionRealization> findAll() {
|
---|
38 | return electionRealizationRepository.findAll();
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public ElectionRealization findById(Long id) {
|
---|
43 | return electionRealizationRepository.findById(id).orElseThrow(() -> new RuntimeException("Election Realization not found"));
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | @Transactional
|
---|
48 | public ElectionRealization update(Long id, LocalDate date, String name, Long electionId, String candidacyRealization, String candidatesListRealization) {
|
---|
49 | Election election = electionService.findById(electionId);
|
---|
50 | ElectionRealization electionRealization = new ElectionRealization();
|
---|
51 | if(id != null)
|
---|
52 | {
|
---|
53 | electionRealization = findById(id);
|
---|
54 | }
|
---|
55 | electionRealization.setElection(election);
|
---|
56 | electionRealization.setName(name);
|
---|
57 | electionRealization.setDate(date);
|
---|
58 | electionRealization = electionRealizationRepository.save(electionRealization);
|
---|
59 | if(candidacyRealization != null)
|
---|
60 | {
|
---|
61 | candidatesElectionRealizationRepository.insertCandidacyElections(electionRealization.getId());
|
---|
62 | }
|
---|
63 | if(candidatesListRealization != null)
|
---|
64 | {
|
---|
65 | candidatesListElectionRealizationRepository.insertCandidateListElections(electionRealization.getId());
|
---|
66 | }
|
---|
67 |
|
---|
68 | voteIdentificationCodeService.generateCodes(citizenService.availableVoters(), date.atTime(23, 59, 59));
|
---|
69 |
|
---|
70 | return electionRealization;
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public ElectionRealization delete(Long id) {
|
---|
75 | ElectionRealization electionRealization = findById(id);
|
---|
76 | electionRealizationRepository.delete(electionRealization);
|
---|
77 | return electionRealization;
|
---|
78 | }
|
---|
79 | }
|
---|