1 | package mk.ukim.finki.eglas.services.Impl;
|
---|
2 |
|
---|
3 | import jakarta.transaction.Transactional;
|
---|
4 | import mk.ukim.finki.eglas.model.*;
|
---|
5 | import mk.ukim.finki.eglas.repository.CandidatesListRepository;
|
---|
6 | import mk.ukim.finki.eglas.services.*;
|
---|
7 | import org.springframework.stereotype.Service;
|
---|
8 |
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Objects;
|
---|
13 | import java.util.stream.Collectors;
|
---|
14 |
|
---|
15 | @Service
|
---|
16 | public class CandidatesListServiceImpl implements CandidatesListService {
|
---|
17 | private final CandidatesListRepository repository;
|
---|
18 | private final CandidatesListElectionRealizationService candidatesListElectionRealizationService;
|
---|
19 | private final CandidateService candidateService;
|
---|
20 | private final ElectoralUnitService electoralUnitService;
|
---|
21 | private final MunicipalityService municipalityService;
|
---|
22 | private final PartyService partyService;
|
---|
23 |
|
---|
24 | public CandidatesListServiceImpl(CandidatesListRepository repository,
|
---|
25 | CandidatesListElectionRealizationService candidatesListElectionRealizationService,
|
---|
26 | CandidateService candidateService, ElectoralUnitService electoralUnitService, MunicipalityService municipalityService, PartyService partyService){
|
---|
27 | this.repository = repository;
|
---|
28 | this.candidatesListElectionRealizationService = candidatesListElectionRealizationService;
|
---|
29 | this.candidateService = candidateService;
|
---|
30 | this.electoralUnitService = electoralUnitService;
|
---|
31 | this.municipalityService = municipalityService;
|
---|
32 | this.partyService = partyService;
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public CandidatesList findById(Long id) {
|
---|
37 | return repository.findById(id).orElseThrow(() -> new RuntimeException("No candidates list found"));
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public List<CandidatesList> findAll() {
|
---|
42 | return repository.findAll();
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | @Transactional
|
---|
47 | public CandidatesList update(Long id, String description, Long partyId, Long candidatesListElectionRealizationId, Long municipalityId, Long electoralUnitId, List<Long> candidatesInList) {
|
---|
48 | if(candidatesInList.size() != 20)
|
---|
49 | {
|
---|
50 | throw new RuntimeException("Candidates list must have 20 candidates!");
|
---|
51 | }
|
---|
52 |
|
---|
53 | CandidatesList list;
|
---|
54 |
|
---|
55 | ElectoralUnit electoralUnit = electoralUnitId != null ? electoralUnitService.findById(electoralUnitId) : null;
|
---|
56 | Municipality municipality = municipalityId != null ? municipalityService.findById(municipalityId) : null;
|
---|
57 | CandidatesListElectionRealization candidatesListElectionRealization = candidatesListElectionRealizationService.findById(candidatesListElectionRealizationId);
|
---|
58 | Party party = partyService.findById(partyId);
|
---|
59 |
|
---|
60 | if (id != null) {
|
---|
61 | list = findById(id);
|
---|
62 | list.getCandidates().clear();
|
---|
63 | repository.save(list);
|
---|
64 | } else {
|
---|
65 | list = new CandidatesList();
|
---|
66 | }
|
---|
67 |
|
---|
68 | candidatesInList.stream()
|
---|
69 | .filter(Objects::nonNull)
|
---|
70 | .map(candidateService::findById)
|
---|
71 | .forEach(x -> list.candidates.add(x));
|
---|
72 | list.setDescription(description);
|
---|
73 | list.setParty(party);
|
---|
74 | list.setMunicipality(municipality);
|
---|
75 | list.setElectoralUnit(electoralUnit);
|
---|
76 | list.setCandidatesListElectionRealization(candidatesListElectionRealization);
|
---|
77 | return repository.save(list);
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public void delete(Long id) {
|
---|
82 | repository.delete(findById(id));
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public void addCandidateToCandidatesList(Long candidatesListId, Long candidateId) {
|
---|
87 | CandidatesList list = findById(candidatesListId);
|
---|
88 | list.getCandidates().add(candidateService.findById(candidateId));
|
---|
89 | }
|
---|
90 | }
|
---|