added get user by id
This commit is contained in:
@@ -35,7 +35,13 @@ public class UserService {
|
||||
.orElseThrow(() -> new ApiException(ErrorCode.NOT_FOUND, "User not found", HttpStatus.NOT_FOUND));
|
||||
}
|
||||
|
||||
public UserResponseDto updateUser(Long id, UserUpdateDto dto) {
|
||||
public UserResponseDto getUserById(Long id) {
|
||||
User user = repository.findById(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException("User not found"));
|
||||
return getUserResponseDto(user);
|
||||
}
|
||||
|
||||
public UserResponseDto updateUser(Long id, UserUpdateDto dto, Boolean removeAvatar) {
|
||||
User user = repository.findById(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException("User not found"));
|
||||
|
||||
@@ -53,13 +59,23 @@ public class UserService {
|
||||
user.setAvatarUrl(newAvatarKey);
|
||||
|
||||
// подчищаем старый файл, если он был и это реально ключ MinIO
|
||||
if (oldAvatarKey != null && !oldAvatarKey.isBlank()) {
|
||||
if (oldAvatarKey != null && !oldAvatarKey.isBlank() && isMinioKey(oldAvatarKey)) {
|
||||
try {
|
||||
fileStorageService.deleteFile(oldAvatarKey);
|
||||
} catch (Exception e) {
|
||||
log.warn("Не удалось удалить старый аватар из хранилища: {}", oldAvatarKey, e);
|
||||
}
|
||||
}
|
||||
} else if (Boolean.TRUE.equals(removeAvatar)) {
|
||||
String oldAvatarKey = user.getAvatarUrl();
|
||||
if (oldAvatarKey != null && !oldAvatarKey.isBlank() && isMinioKey(oldAvatarKey)) {
|
||||
try {
|
||||
fileStorageService.deleteFile(oldAvatarKey);
|
||||
} catch (Exception e) {
|
||||
log.warn("Не удалось удалить старый аватар из хранилища: {}", oldAvatarKey, e);
|
||||
}
|
||||
}
|
||||
user.setAvatarUrl(null);
|
||||
}
|
||||
|
||||
repository.save(user);
|
||||
@@ -69,6 +85,11 @@ public class UserService {
|
||||
return userResponseDto;
|
||||
}
|
||||
|
||||
/** Ключ MinIO — относительный (без хоста и без ведущего слеша). */
|
||||
private static boolean isMinioKey(String key) {
|
||||
return !key.startsWith("http://") && !key.startsWith("https://") && !key.startsWith("/") && !key.contains("\\");
|
||||
}
|
||||
|
||||
public Long getUserIdByLogin(String login) {
|
||||
return repository.findUserByLogin(login)
|
||||
.orElseThrow(() -> new EntityNotFoundException("User not found"));
|
||||
|
||||
Reference in New Issue
Block a user