2025-11-27 03:05:07 +07:00
|
|
|
package ru.soune.no_copy.service;
|
|
|
|
|
|
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
2025-11-27 12:48:08 +07:00
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
2025-11-27 03:05:07 +07:00
|
|
|
import ru.soune.no_copy.dto.ChangePasswordRequest;
|
2025-11-27 12:48:08 +07:00
|
|
|
import ru.soune.no_copy.dto.UserDTO;
|
|
|
|
|
import ru.soune.no_copy.dto.UserRequest;
|
2025-11-27 03:05:07 +07:00
|
|
|
import ru.soune.no_copy.entity.User;
|
2025-11-27 12:48:08 +07:00
|
|
|
import ru.soune.no_copy.exception.InvalidUserEmail;
|
2025-11-27 03:05:07 +07:00
|
|
|
import ru.soune.no_copy.exception.NotValidationPasswordException;
|
2025-11-27 12:48:08 +07:00
|
|
|
import ru.soune.no_copy.exception.UserNotFoundException;
|
2025-11-27 03:05:07 +07:00
|
|
|
import ru.soune.no_copy.repository.UserRepository;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
@AllArgsConstructor
|
|
|
|
|
public class UserService {
|
|
|
|
|
|
|
|
|
|
private final UserRepository userRepository;
|
|
|
|
|
|
|
|
|
|
private final PasswordEncoder passwordEncoder;
|
|
|
|
|
|
2025-11-27 12:48:08 +07:00
|
|
|
@Transactional
|
2025-11-27 03:05:07 +07:00
|
|
|
public User changePassword(User user, ChangePasswordRequest request) {
|
|
|
|
|
if (!passwordEncoder.matches(request.getCurrentPassword(), user.getPassword())) {
|
|
|
|
|
throw new NotValidationPasswordException("Current password is incorrect");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.setPassword(passwordEncoder.encode(request.getNewPassword()));
|
|
|
|
|
|
|
|
|
|
return userRepository.save(user);
|
|
|
|
|
}
|
2025-11-27 12:48:08 +07:00
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public UserDTO updateUser(UserRequest userRequest, User currentUser) {
|
|
|
|
|
User user = userRepository.findById(currentUser.getId())
|
|
|
|
|
.orElseThrow(() -> new UserNotFoundException("User not found with id: " + currentUser.getId()));
|
|
|
|
|
|
|
|
|
|
updateUserFromRequest(user, userRequest);
|
|
|
|
|
|
|
|
|
|
User updatedUser = userRepository.save(user);
|
|
|
|
|
|
|
|
|
|
return mapToDTO(updatedUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateUserFromRequest(User user, UserRequest request) {
|
|
|
|
|
if (request.getFullName() != null) {
|
|
|
|
|
user.setFullName(request.getFullName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.getCompany() != null) {
|
|
|
|
|
user.setCompany(request.getCompany());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.getEmail() != null && !request.getEmail().equals(user.getEmail())) {
|
|
|
|
|
if (userRepository.existsByEmail(request.getEmail())) {
|
|
|
|
|
throw new InvalidUserEmail("Email already exists: " + request.getEmail());
|
|
|
|
|
}
|
|
|
|
|
user.setEmail(request.getEmail());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.getPhone() != null) {
|
|
|
|
|
user.setPhone(request.getPhone());
|
|
|
|
|
}
|
|
|
|
|
if (request.getGenderType() != null) {
|
|
|
|
|
user.setGenderType(request.getGenderType());
|
|
|
|
|
}
|
|
|
|
|
if (request.getBirthday() != null) {
|
|
|
|
|
user.setBirthday(request.getBirthday());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private UserDTO mapToDTO(User user) {
|
|
|
|
|
return UserDTO.builder()
|
|
|
|
|
.fullName(user.getFullName())
|
|
|
|
|
.email(user.getEmail())
|
|
|
|
|
.company(user.getCompany())
|
|
|
|
|
.phone(user.getPhone())
|
|
|
|
|
.genderType(user.getGenderType())
|
|
|
|
|
.birthday(user.getBirthday())
|
|
|
|
|
.subscriptionType(user.getSubscriptionType())
|
|
|
|
|
.createdAt(user.getCreatedAt())
|
|
|
|
|
.build();
|
|
|
|
|
}
|
2025-11-27 03:05:07 +07:00
|
|
|
}
|