StudentLoanManagement
UserService.java
Go to the documentation of this file.
1package com.student_loan.service;
2
3import org.springframework.beans.factory.annotation.Autowired;
4import org.springframework.stereotype.Service;
5import org.springframework.transaction.annotation.Transactional;
6import com.student_loan.model.User;
7import com.student_loan.model.User.DegreeType;
8import com.student_loan.dtos.CredentialsDTO;
9import com.student_loan.repository.UserRepository;
10import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
11import com.student_loan.security.JwtUtil;
12
13import java.util.HashMap;
14import java.util.List;
15import java.util.Map;
16import java.util.Optional;
17
22@Service
23@Transactional
24public class UserService {
25 @Autowired
26 private UserRepository userRepository;
27
28 @Autowired
29 private NotificationService notificationService;
30
31 @Autowired
32 private BCryptPasswordEncoder passwordEncoder;
33
34
35 @Autowired
36 private JwtUtil jwtUtil;
37
38 private Map<String, User> tokens = new HashMap<>();
39
45 public List<User> getAllUsers() {
46 return userRepository.findAll();
47 }
48
54 public Optional<User> getUserById(Long id) {
55 return userRepository.findById(id);
56 }
57
66 public User updateUser(Long id, User newData) {
67 User existing = userRepository.findById(id)
68 .orElseThrow(() -> new RuntimeException("User not found"));
69
70 if (newData.getName() != null) {
71 existing.setName(newData.getName());
72 }
73 if (newData.getEmail() != null) {
74 existing.setEmail(newData.getEmail());
75 }
76 if (newData.getTelephoneNumber() != null) {
77 existing.setTelephoneNumber(newData.getTelephoneNumber());
78 }
79 if (newData.getAddress() != null) {
80 existing.setAddress(newData.getAddress());
81 }
82 if (newData.getDegreeType() != null) {
83 existing.setDegreeType(newData.getDegreeType());
84 }
85 if (newData.getDegreeYear() != null) {
86 existing.setDegreeYear(newData.getDegreeYear());
87 }
88 if (newData.getAverageRating() != null) {
89 existing.setAverageRating(newData.getAverageRating());
90 }
91
92 String rawPass = newData.getPassword();
93 if (rawPass != null && !rawPass.isBlank()) {
94 existing.setPassword(passwordEncoder.encode(rawPass));
95 }
96
97 Integer previousPenalties = existing.getPenalties();
98 Integer newPenalties = newData.getPenalties();
99
100 if (newPenalties != null) {
101 if (newPenalties > previousPenalties) {
102 notificationService.enviarCorreo(
103 existing.getEmail(),
104 "NEW PENALTY!",
105 "Your penalty count increased to " + newPenalties
106 );
107 }
108 existing.setPenalties(newPenalties);
109 } else {
110 existing.setPenalties(previousPenalties);
111 }
112 existing.setAdmin(newData.getAdmin());
113
114 return userRepository.save(existing);
115 }
116
117
124 public boolean register(User user) {
125 if(userRepository.findByEmail(user.getEmail())!=null) {
126 return false;
127 }else {
128 user.setPassword(passwordEncoder.encode(user.getPassword())); // Encrypts the password
129 userRepository.save(user);
130 return true;
131 }
132 }
133
134
141 public String login(CredentialsDTO credentials) {
142 User user = userRepository.findByEmail(credentials.getEmail());
143 if (tokens.containsValue(user)) {
144 return "User already logged in";
145 } else if (user != null && passwordEncoder.matches(credentials.getPassword(), user.getPassword())) {
146 String token = jwtUtil.generateToken(credentials.getEmail());
147 tokens.put(token, user);
148 return token;
149 } else {
150 return "Invalid credentials";
151 }
152 }
153
161 public boolean logout(String token) {
162 if(tokens.containsKey(token)) {
163 tokens.remove(token);
164 return true;
165 }else{
166 return false;}
167 }
168
175 public void deleteUser(Long id) {
176 Optional<User> user = userRepository.findById(id);
177 if (user.isPresent()) {
178 userRepository.deleteById(id);
179 } else {
180 throw new RuntimeException("User not found");
181 }
182 }
189 public User getUserByToken(String token) {
190 return tokens.get(token);
191 }
192
199 public User getUserByEmail(String email) {
200 return userRepository.findByEmail(email);
201 }
202}
String login(CredentialsDTO credentials)
Optional< User > getUserById(Long id)
User updateUser(Long id, User newData)