1 | package mk.ukim.finki.eglas.services.Impl;
|
---|
2 |
|
---|
3 | import mk.ukim.finki.eglas.model.CandidatesElectionRealization;
|
---|
4 | import mk.ukim.finki.eglas.model.Citizen;
|
---|
5 | import mk.ukim.finki.eglas.model.ElectionRealization;
|
---|
6 | import mk.ukim.finki.eglas.repository.CandidatesElectionRealizationRepository;
|
---|
7 | import mk.ukim.finki.eglas.services.CandidatesElectionRealizationService;
|
---|
8 | import mk.ukim.finki.eglas.services.CitizenService;
|
---|
9 | import mk.ukim.finki.eglas.services.ElectionRealizationService;
|
---|
10 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
11 | import org.springframework.stereotype.Service;
|
---|
12 |
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 | @Service
|
---|
16 | public class CandidatesElectionRealizationServiceImpl implements CandidatesElectionRealizationService {
|
---|
17 |
|
---|
18 | private final CandidatesElectionRealizationRepository repository;
|
---|
19 | private final ElectionRealizationService electionRealizationService;
|
---|
20 | @Autowired
|
---|
21 | private CitizenService citizenService;
|
---|
22 |
|
---|
23 | CandidatesElectionRealizationServiceImpl(CandidatesElectionRealizationRepository repository,
|
---|
24 | ElectionRealizationService electionRealizationService){
|
---|
25 | this.repository = repository;
|
---|
26 | this.electionRealizationService = electionRealizationService;
|
---|
27 | }
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | public CandidatesElectionRealization findById(Long id){
|
---|
31 | return repository.findById(id).orElseThrow(() -> new RuntimeException("No candidate election realization found"));
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | public List<CandidatesElectionRealization> findAll(){
|
---|
36 | return repository.findAll();
|
---|
37 | }
|
---|
38 | @Override
|
---|
39 | public CandidatesElectionRealization update(Long id){
|
---|
40 | CandidatesElectionRealization candidate = (CandidatesElectionRealization) electionRealizationService.findById(id);
|
---|
41 | return repository.save(candidate);
|
---|
42 | }
|
---|
43 | @Override
|
---|
44 | public void delete(Long id){
|
---|
45 | repository.delete(findById(id));
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public List<CandidatesElectionRealization> findAvailable(Long citizenId) {
|
---|
50 | Citizen citizen = citizenService.findById(citizenId);
|
---|
51 | return repository.availableElections(citizen, citizen.getAddress().getMunicipality());
|
---|
52 | }
|
---|
53 | }
|
---|