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.model.dto.CitizenVote;
|
---|
6 | import mk.ukim.finki.eglas.repository.CitizenRepository;
|
---|
7 | import mk.ukim.finki.eglas.repository.CommitteeMemberRepository;
|
---|
8 | import mk.ukim.finki.eglas.repository.CommitteeRepository;
|
---|
9 | import mk.ukim.finki.eglas.services.*;
|
---|
10 | import org.springframework.stereotype.Service;
|
---|
11 |
|
---|
12 | import java.time.LocalDate;
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 | @Service
|
---|
16 | public class CommitteeServiceImpl implements CommitteeService {
|
---|
17 |
|
---|
18 | private final CommitteeMemberService committeeMemberService;
|
---|
19 | private final CommitteeRepository committeeRepository;
|
---|
20 | private final AddressService addressService;
|
---|
21 | private final ElectionRealizationService electionRealizationService;
|
---|
22 | private final UtilService utilService;
|
---|
23 | private final CitizenRepository citizenRepository;
|
---|
24 | private final CommitteeMemberRepository committeeMemberRepository;
|
---|
25 |
|
---|
26 | public CommitteeServiceImpl(CommitteeMemberService committeeMemberService,
|
---|
27 | CommitteeRepository committeeRepository,
|
---|
28 | AddressService addressService,
|
---|
29 | ElectionRealizationService electionRealizationService, UtilService utilService, CitizenRepository citizenRepository, CommitteeMemberRepository committeeMemberRepository) {
|
---|
30 | this.committeeMemberService = committeeMemberService;
|
---|
31 | this.committeeRepository = committeeRepository;
|
---|
32 | this.addressService = addressService;
|
---|
33 | this.electionRealizationService = electionRealizationService;
|
---|
34 | this.utilService = utilService;
|
---|
35 | this.citizenRepository = citizenRepository;
|
---|
36 | this.committeeMemberRepository = committeeMemberRepository;
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public Committee findById(Long id) {
|
---|
41 | return committeeRepository.findById(id).orElseThrow(() -> new RuntimeException("Committee not found"));
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public List<Committee> findAll() {
|
---|
46 | return committeeRepository.findAll();
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public void delete(Long id) {
|
---|
51 | committeeRepository.delete(findById(id));
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Transactional
|
---|
55 | @Override
|
---|
56 | public Committee update(Long id, Long pollingStationId, Long electionRealizationId, List<Long> membersId) {
|
---|
57 | Committee committee;
|
---|
58 | if (id != null){
|
---|
59 | committee = findById(id);
|
---|
60 | } else {
|
---|
61 | committee = new Committee();
|
---|
62 | }
|
---|
63 |
|
---|
64 | committee.setPollingStation(addressService.findPollingStationById(pollingStationId));
|
---|
65 | committee.setElectionRealization(electionRealizationService. findById(electionRealizationId));
|
---|
66 | committeeRepository.save(committee);
|
---|
67 | membersId.forEach(memberId -> addMemberToCommittee(committee.getId(), memberId));
|
---|
68 | return committeeRepository.save(committee);
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | @Transactional
|
---|
73 | public void addMemberToCommittee(Long committeeId, Long committeeMemberId) {
|
---|
74 | Committee committee = findById(committeeId);
|
---|
75 | committee.getMembers().add(committeeMemberService.findById(committeeMemberId));
|
---|
76 | committeeRepository.save(committee);
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public List<CitizenVote> getCitizens(Long committeeId) {
|
---|
81 | CommitteeMember committeeMember = committeeMemberRepository.findById(committeeId).orElseThrow(RuntimeException::new);
|
---|
82 | Committee committee = committeeRepository.findCommitteeByMembersContainsAndElectionRealization_DateIsAfter(committeeMember, LocalDate.now().minusDays(300));
|
---|
83 | PollingStation pollingStation = committee.getPollingStation();
|
---|
84 | return citizenRepository.findAllByAddress_PollingStation(pollingStation.getId()).stream().filter(x -> x instanceof CitizenVote).toList();
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public ElectionRealization getElectionRealization(Long committeeId){
|
---|
89 | Committee committee = findById(committeeId);
|
---|
90 | return null;
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public ElectionRealization findElectionRealizationByCitizen(Long citizenId){
|
---|
95 | CommitteeMember committeeMember = committeeMemberRepository.findById(citizenId).orElseThrow(RuntimeException::new);
|
---|
96 | return committeeRepository.findCommitteeByMembersContainsAndElectionRealization_DateIsAfter(committeeMember, LocalDate.now().minusDays(300)).getElectionRealization();
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public boolean getSamePollingStation(Long committeeMemberId, Long citizenId){
|
---|
101 | Committee committee = committeeRepository.findCommitteeByMembersContainsAndElectionRealization_DateIsAfter(committeeMemberService.findById(committeeMemberId), LocalDate.now().minusDays(300));
|
---|
102 | return addressService.findPollingStationByCitizenId(citizenId).getId() == committee.getPollingStation().getId();
|
---|
103 | }
|
---|
104 | }
|
---|