1 | package mk.ukim.finki.eglas.services.Impl;
|
---|
2 |
|
---|
3 | import mk.ukim.finki.eglas.model.Citizen;
|
---|
4 | import mk.ukim.finki.eglas.model.UserProfile;
|
---|
5 | import mk.ukim.finki.eglas.repository.UserProfileRepository;
|
---|
6 | import mk.ukim.finki.eglas.services.CitizenService;
|
---|
7 | import mk.ukim.finki.eglas.services.UserProfileService;
|
---|
8 | import org.springframework.stereotype.Service;
|
---|
9 |
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | // Jovan
|
---|
13 | @Service
|
---|
14 | public class UserProfileServiceImpl implements UserProfileService {
|
---|
15 | private final UserProfileRepository userProfileRepository;
|
---|
16 | private final CitizenService citizenService;
|
---|
17 |
|
---|
18 | UserProfileServiceImpl(UserProfileRepository userProfileRepository, CitizenService citizenService){
|
---|
19 | this.userProfileRepository = userProfileRepository;
|
---|
20 | this.citizenService = citizenService;
|
---|
21 | }
|
---|
22 |
|
---|
23 | @Override
|
---|
24 | public List<UserProfile> findAll() {
|
---|
25 | return userProfileRepository.findAll();
|
---|
26 | }
|
---|
27 |
|
---|
28 | @Override
|
---|
29 | public UserProfile findById(Long id) {
|
---|
30 | return userProfileRepository.findById(id).orElseThrow(() -> new RuntimeException("UserProfile not found"));
|
---|
31 | }
|
---|
32 |
|
---|
33 | @Override
|
---|
34 | public UserProfile update(Long id, String userName, String password, String role, Long citizenId) {
|
---|
35 | Citizen citizen = citizenService.findById(citizenId);
|
---|
36 | UserProfile userProfile = new UserProfile();
|
---|
37 | if(id != null)
|
---|
38 | {
|
---|
39 | userProfile = findById(id);
|
---|
40 | }
|
---|
41 | userProfile.setUserName(userName);
|
---|
42 | userProfile.setPassword(password);
|
---|
43 | userProfile.setRole(role);
|
---|
44 | userProfile.setCitizen(citizen);
|
---|
45 | return userProfileRepository.save(userProfile);
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public UserProfile delete(Long id) {
|
---|
50 | UserProfile userProfile = findById(id);
|
---|
51 | userProfileRepository.delete(userProfile);
|
---|
52 | return userProfile;
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public UserProfile loadUserByUsername(String username) {
|
---|
57 | return userProfileRepository.findByUserName(username);
|
---|
58 | }
|
---|
59 | }
|
---|