90 lines
3.1 KiB
Java
90 lines
3.1 KiB
Java
package com.krylov.refound.service;
|
||
|
||
import com.krylov.refound.dto.UserResponseDto;
|
||
import com.krylov.refound.dto.UserUpdateDto;
|
||
import com.krylov.refound.entity.User;
|
||
import com.krylov.refound.enums.ErrorCode;
|
||
import com.krylov.refound.exception.ApiException;
|
||
import com.krylov.refound.repository.UserRepository;
|
||
import jakarta.persistence.EntityNotFoundException;
|
||
import lombok.RequiredArgsConstructor;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.http.HttpStatus;
|
||
import org.springframework.security.core.Authentication;
|
||
import org.springframework.security.core.context.SecurityContextHolder;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.web.multipart.MultipartFile;
|
||
|
||
@Service
|
||
@Slf4j
|
||
@RequiredArgsConstructor
|
||
public class UserService {
|
||
private final UserRepository repository;
|
||
private final FileStorageService fileStorageService;
|
||
|
||
public User getCurrentUser() {
|
||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||
|
||
if (authentication == null || authentication.getPrincipal() == null) {
|
||
throw new ApiException(ErrorCode.UNAUTHORIZED, "Пользователь не авторизован", HttpStatus.UNAUTHORIZED);
|
||
}
|
||
|
||
String login = (String) authentication.getPrincipal();
|
||
|
||
return repository.findByLogin(login)
|
||
.orElseThrow(() -> new ApiException(ErrorCode.NOT_FOUND, "User not found", HttpStatus.NOT_FOUND));
|
||
}
|
||
|
||
public UserResponseDto updateUser(Long id, UserUpdateDto dto) {
|
||
User user = repository.findById(id)
|
||
.orElseThrow(() -> new EntityNotFoundException("User not found"));
|
||
|
||
user.setLogin(dto.getLogin());
|
||
user.setName(dto.getFirstName());
|
||
user.setLastName(dto.getLastName());
|
||
user.setPhone(dto.getPhone());
|
||
user.setEmail(dto.getEmail());
|
||
|
||
MultipartFile avatar = dto.getAvatar();
|
||
if (avatar != null && !avatar.isEmpty()) {
|
||
String oldAvatarKey = user.getAvatarUrl();
|
||
|
||
String newAvatarKey = fileStorageService.uploadFile(avatar, "avatars");
|
||
user.setAvatarUrl(newAvatarKey);
|
||
|
||
// подчищаем старый файл, если он был и это реально ключ MinIO
|
||
if (oldAvatarKey != null && !oldAvatarKey.isBlank()) {
|
||
try {
|
||
fileStorageService.deleteFile(oldAvatarKey);
|
||
} catch (Exception e) {
|
||
log.warn("Не удалось удалить старый аватар из хранилища: {}", oldAvatarKey, e);
|
||
}
|
||
}
|
||
}
|
||
|
||
repository.save(user);
|
||
|
||
UserResponseDto userResponseDto = getUserResponseDto(user);
|
||
log.info("User updated: {}", userResponseDto.getLogin());
|
||
return userResponseDto;
|
||
}
|
||
|
||
public Long getUserIdByLogin(String login) {
|
||
return repository.findUserByLogin(login)
|
||
.orElseThrow(() -> new EntityNotFoundException("User not found"));
|
||
}
|
||
|
||
private static UserResponseDto getUserResponseDto(User user) {
|
||
return UserResponseDto.builder()
|
||
.id(user.getId())
|
||
.login(user.getLogin())
|
||
.name(user.getName())
|
||
.phone(user.getPhone())
|
||
.email(user.getEmail())
|
||
.lastName(user.getLastName())
|
||
.avatar(user.getAvatarUrl())
|
||
.build();
|
||
}
|
||
|
||
}
|