1 | package mk.ukim.finki.eglas.services.Impl;
|
---|
2 |
|
---|
3 | import mk.ukim.finki.eglas.model.Candidacy;
|
---|
4 | import mk.ukim.finki.eglas.model.CandidatesElectionRealization;
|
---|
5 | import mk.ukim.finki.eglas.model.Citizen;
|
---|
6 | import mk.ukim.finki.eglas.repository.CandidacyRepository;
|
---|
7 | import mk.ukim.finki.eglas.services.*;
|
---|
8 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
9 | import org.springframework.stereotype.Service;
|
---|
10 |
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | @Service
|
---|
14 | public class CandidacyServiceImpl implements CandidacyService {
|
---|
15 |
|
---|
16 | private final CandidacyRepository candidacyRepository;
|
---|
17 | private final CandidateService candidateService;
|
---|
18 | private final PartyService partyService;
|
---|
19 | private final CandidatesElectionRealizationService candidatesElectionRealizationService;
|
---|
20 | private final MunicipalityService municipalityService;
|
---|
21 | @Autowired
|
---|
22 | private CitizenService citizenService;
|
---|
23 |
|
---|
24 | public CandidacyServiceImpl(CandidacyRepository candidacyRepository,
|
---|
25 | CandidateService candidateService,
|
---|
26 | PartyService partyService,
|
---|
27 | CandidatesElectionRealizationService candidatesElectionRealizationService,
|
---|
28 | MunicipalityService municipalityService) {
|
---|
29 | this.candidacyRepository = candidacyRepository;
|
---|
30 | this.candidateService = candidateService;
|
---|
31 | this.partyService = partyService;
|
---|
32 | this.candidatesElectionRealizationService = candidatesElectionRealizationService;
|
---|
33 | this.municipalityService = municipalityService;
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public Candidacy findById(Long id) {
|
---|
39 | return candidacyRepository.findById(id).orElseThrow(() -> new RuntimeException("No candidacy found"));
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public List<Candidacy> findAll() {
|
---|
44 | return candidacyRepository.findAll();
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public Candidacy update(Long id, String description, Long candidateId, Long partyId, Long candidatesElectionRealizationId, Long municipalityId) {
|
---|
49 | Candidacy candidacy = new Candidacy();
|
---|
50 | if (id != null){
|
---|
51 | candidacy = findById(id);
|
---|
52 | }
|
---|
53 | candidacy.setMunicipality(municipalityService.findById(municipalityId));
|
---|
54 | candidacy.setDescription(description);
|
---|
55 | candidacy.setCandidate(candidateService.findById(candidateId));
|
---|
56 | candidacy.setParty(partyService.findById(partyId));
|
---|
57 | candidacy.setCandidatesElectionRealization(candidatesElectionRealizationService.findById(candidatesElectionRealizationId));
|
---|
58 | return candidacyRepository.save(candidacy);
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public void delete(Long id) {
|
---|
63 | candidacyRepository.delete(findById(id));
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public List<Candidacy> findAllByElectionsRealiztion(Long realizationId, Long citizenId)
|
---|
68 | {
|
---|
69 | Citizen citizen = citizenService.findById(citizenId);
|
---|
70 | CandidatesElectionRealization candidatesElectionRealization = candidatesElectionRealizationService.findById(realizationId);
|
---|
71 | return candidacyRepository.findAllByCandidatesElectionRealizationAndMunicipality(candidatesElectionRealization, citizen.getAddress().getMunicipality());
|
---|
72 | }
|
---|
73 | }
|
---|