added get user by id

This commit is contained in:
SlimusMinus
2026-09-03 00:26:57 +03:00
parent 9a80de54b5
commit 63864011ef
2 changed files with 45 additions and 7 deletions

View File

@@ -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"));