1 | package mk.ukim.finki.eglas.services.Impl;
|
---|
2 |
|
---|
3 | import mk.ukim.finki.eglas.model.Citizen;
|
---|
4 | import mk.ukim.finki.eglas.model.CommitteeMember;
|
---|
5 | import mk.ukim.finki.eglas.model.UserProfile;
|
---|
6 | import mk.ukim.finki.eglas.repository.CommitteeMemberRepository;
|
---|
7 | import mk.ukim.finki.eglas.repository.UserProfileRepository;
|
---|
8 | import mk.ukim.finki.eglas.services.CitizenService;
|
---|
9 | import mk.ukim.finki.eglas.services.CommitteeMemberService;
|
---|
10 | import mk.ukim.finki.eglas.services.UtilService;
|
---|
11 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
12 | import org.springframework.stereotype.Service;
|
---|
13 |
|
---|
14 | import java.util.List;
|
---|
15 | @Service
|
---|
16 | public class CommitteeMemberServiceImpl implements CommitteeMemberService {
|
---|
17 |
|
---|
18 |
|
---|
19 | private final CommitteeMemberRepository committeeMemberRepository;
|
---|
20 | private final CitizenService citizenService;
|
---|
21 | private final UtilService utilService;
|
---|
22 | private final UserProfileRepository userProfileRepository;
|
---|
23 | private final PasswordEncoder passwordEncoder;
|
---|
24 |
|
---|
25 | public CommitteeMemberServiceImpl(CommitteeMemberRepository committeeMemberRepository, CitizenService citizenService, UtilService utilService, UserProfileRepository userProfileRepository, PasswordEncoder passwordEncoder) {
|
---|
26 | this.committeeMemberRepository = committeeMemberRepository;
|
---|
27 | this.citizenService = citizenService;
|
---|
28 | this.utilService = utilService;
|
---|
29 | this.userProfileRepository = userProfileRepository;
|
---|
30 | this.passwordEncoder = passwordEncoder;
|
---|
31 | }
|
---|
32 |
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | public CommitteeMember findById(Long id) {
|
---|
36 | return committeeMemberRepository.findById(id).orElseThrow(() -> new RuntimeException("No committee member found"));
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public List<CommitteeMember> findAll() {
|
---|
41 | return committeeMemberRepository.findAll();
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public CommitteeMember update(Long id) {
|
---|
46 | committeeMemberRepository.insertCommitteeMember(id);
|
---|
47 | Citizen c = citizenService.findById(id);
|
---|
48 | String userName = utilService.cyrillicToLatinTransliteration(c.getName()) + '.' + utilService.cyrillicToLatinTransliteration(c.getSurname());
|
---|
49 | UserProfile userProfile = userProfileRepository.findByUserName(userName);
|
---|
50 | if(userProfile == null) {
|
---|
51 | userProfile = new UserProfile();
|
---|
52 | userProfile.setUserName(userName);
|
---|
53 | userProfile.setPassword(passwordEncoder.encode(c.getIdNum()));
|
---|
54 | userProfile.setIsCommittee(true);
|
---|
55 | userProfile.setCitizen(c);
|
---|
56 | }
|
---|
57 | else {
|
---|
58 | userProfile.setIsCommittee(false);
|
---|
59 | }
|
---|
60 | System.out.println(userProfileRepository.save(userProfile));
|
---|
61 | return findById(id);
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public void delete(Long id) {
|
---|
66 | committeeMemberRepository.delete(findById(id));
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 |
|
---|
71 | }
|
---|