source: src/main/java/mk/ukim/finki/eglas/services/Impl/CitizenServiceImpl.java

main
Last change on this file was ac151d1, checked in by David <darsov2@…>, 4 months ago

initial

  • Property mode set to 100644
File size: 3.2 KB
Line 
1package mk.ukim.finki.eglas.services.Impl;
2
3import jakarta.persistence.criteria.CriteriaBuilder;
4import mk.ukim.finki.eglas.model.Address;
5import mk.ukim.finki.eglas.model.Citizen;
6import mk.ukim.finki.eglas.repository.CitizenRepository;
7import mk.ukim.finki.eglas.services.AddressService;
8import mk.ukim.finki.eglas.services.CitizenService;
9import org.springframework.stereotype.Service;
10
11import java.time.LocalDate;
12import java.util.HashMap;
13import java.util.List;
14import java.util.Optional;
15import java.util.TreeMap;
16import java.util.stream.Collectors;
17
18@Service
19public 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}
Note: See TracBrowser for help on using the repository browser.