1 | package mk.ukim.finki.eglas.services.Impl;
|
---|
2 |
|
---|
3 | import jakarta.persistence.criteria.CriteriaBuilder;
|
---|
4 | import mk.ukim.finki.eglas.model.Address;
|
---|
5 | import mk.ukim.finki.eglas.model.Citizen;
|
---|
6 | import mk.ukim.finki.eglas.repository.CitizenRepository;
|
---|
7 | import mk.ukim.finki.eglas.services.AddressService;
|
---|
8 | import mk.ukim.finki.eglas.services.CitizenService;
|
---|
9 | import org.springframework.stereotype.Service;
|
---|
10 |
|
---|
11 | import java.time.LocalDate;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.Optional;
|
---|
15 | import java.util.TreeMap;
|
---|
16 | import java.util.stream.Collectors;
|
---|
17 |
|
---|
18 | @Service
|
---|
19 | public class CitizenServiceImpl implements CitizenService {
|
---|
20 | private final CitizenRepository citizenRepository;
|
---|
21 | private final AddressService addressService;
|
---|
22 | public CitizenServiceImpl(CitizenRepository citizenRepository, AddressService addressService) {
|
---|
23 | this.citizenRepository = citizenRepository;
|
---|
24 | this.addressService = addressService;
|
---|
25 | }
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public List<Citizen> findAll() {
|
---|
29 | HashMap<String, Integer> mapa = new HashMap<>();
|
---|
30 | return citizenRepository.findAll();
|
---|
31 | }
|
---|
32 |
|
---|
33 | @Override
|
---|
34 | public Citizen findById(Long id) {
|
---|
35 | return citizenRepository.findById(id).orElseThrow(() -> new RuntimeException("Citizen not found"));
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | public Citizen update(Long id, String name, String surname, LocalDate dateOfBirth, String idNum, String street, Integer houseNumber, Integer entranceNumber, Integer apartmentNumber, Long municipality) {
|
---|
40 | Address address = addressService.findByStreetLikeAndHouseNumberAndEntranceNumberAndApartmentNumberAAndMunicipality(street, houseNumber, entranceNumber, apartmentNumber, municipality);
|
---|
41 | Citizen citizen = new Citizen();
|
---|
42 | if(id != null)
|
---|
43 | {
|
---|
44 | citizen = findById(id);
|
---|
45 | }
|
---|
46 | citizen.setAddress(address);
|
---|
47 | citizen.setName(name);
|
---|
48 | citizen.setSurname(surname);
|
---|
49 | citizen.setDateOfBirth(dateOfBirth);
|
---|
50 | citizen.setIdNum(idNum);
|
---|
51 | return citizenRepository.save(citizen);
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public Citizen delete(Long id) {
|
---|
56 | Citizen citizen = findById(id);
|
---|
57 | citizenRepository.save(citizen);
|
---|
58 | return citizen;
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public Citizen findByIdNum(String idNum) {
|
---|
63 | // Optional<Citizen> orel = citizenRepository.findByIdNum(idNum);
|
---|
64 | return citizenRepository.findByIdNum(idNum).orElseThrow(() -> new RuntimeException("Citizen with given id number not found"));
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public Citizen validateCitizen(String idNum, String documentNumber)
|
---|
69 | {
|
---|
70 | try {
|
---|
71 | Citizen citizen = findByIdNum(idNum);
|
---|
72 | String ds = citizen.getDocument().getDocumentNumber();
|
---|
73 | Boolean orel = citizen.getDocument().getDocumentNumber().equals(documentNumber);
|
---|
74 | return citizen.getDocument().getDocumentNumber().equals(documentNumber) ? citizen : null;
|
---|
75 | }
|
---|
76 | catch (Exception ex)
|
---|
77 | {
|
---|
78 | return null;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public Integer availableVoters() {
|
---|
84 | return citizenRepository.availableVoters();
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public List<Citizen> findAllByAddress_PollingStation_Id(Long pollingStationId) {
|
---|
89 | return citizenRepository.findAllByDocumentIsNotNullAndAddress_PollingStation_Id(pollingStationId);
|
---|
90 | }
|
---|
91 | }
|
---|