-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathUserService.java
More file actions
100 lines (78 loc) · 3.17 KB
/
UserService.java
File metadata and controls
100 lines (78 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.greencode.service;
import com.greencode.entity.User;
import com.greencode.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service
@Transactional
public class UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private PasswordEncoder passwordEncoder;
/**
* ✅ STEP 3.1 — Update password by email (for password reset flow)
*/
public void updatePasswordByEmail(String email, String encodedPassword) {
User user = userRepository.findByEmail(email)
.orElseThrow(() -> new IllegalArgumentException("User not found"));
user.setPassword(encodedPassword);
userRepository.save(user);
}
public List<User> getAllUsers() {
return userRepository.findAll();
}
public Optional<User> getUserById(Long id) {
return userRepository.findById(id);
}
public Optional<User> getUserByUsername(String username) {
return userRepository.findByUsername(username);
}
public Optional<User> getUserByEmail(String email) {
return userRepository.findByEmail(email);
}
public User createUser(User user) {
if (userRepository.existsByUsername(user.getUsername())) {
throw new RuntimeException("Username already exists");
}
if (userRepository.existsByEmail(user.getEmail())) {
throw new RuntimeException("Email already exists");
}
user.setPassword(passwordEncoder.encode(user.getPassword()));
return userRepository.save(user);
}
public User updateUser(Long id, User userDetails) {
User user = userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("User not found"));
if (userRepository.existsByUsernameOrEmailExcludingId(
userDetails.getUsername(), userDetails.getEmail(), id)) {
throw new RuntimeException("Username or email already exists");
}
user.setUsername(userDetails.getUsername());
user.setEmail(userDetails.getEmail());
user.setFirstName(userDetails.getFirstName());
user.setLastName(userDetails.getLastName());
user.setRole(userDetails.getRole());
user.setIsEnabled(userDetails.getIsEnabled());
if (userDetails.getPassword() != null && !userDetails.getPassword().isEmpty()) {
user.setPassword(passwordEncoder.encode(userDetails.getPassword()));
}
return userRepository.save(user);
}
public void deleteUser(Long id) {
User user = userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("User not found"));
user.setIsActive(false);
userRepository.save(user);
}
public boolean existsByUsername(String username) {
return userRepository.existsByUsername(username);
}
public boolean existsByEmail(String email) {
return userRepository.existsByEmail(email);
}
}