source: src/main/java/mk/ukim/finki/eglas/services/Impl/CommitteeMemberServiceImpl.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: 2.7 KB
Line 
1package mk.ukim.finki.eglas.services.Impl;
2
3import mk.ukim.finki.eglas.model.Citizen;
4import mk.ukim.finki.eglas.model.CommitteeMember;
5import mk.ukim.finki.eglas.model.UserProfile;
6import mk.ukim.finki.eglas.repository.CommitteeMemberRepository;
7import mk.ukim.finki.eglas.repository.UserProfileRepository;
8import mk.ukim.finki.eglas.services.CitizenService;
9import mk.ukim.finki.eglas.services.CommitteeMemberService;
10import mk.ukim.finki.eglas.services.UtilService;
11import org.springframework.security.crypto.password.PasswordEncoder;
12import org.springframework.stereotype.Service;
13
14import java.util.List;
15@Service
16public 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}
Note: See TracBrowser for help on using the repository browser.