Merge pull request #31 from SlimusMinus/fix-get_user_profile

added get user by id
This commit is contained in:
SlimusMinus
2026-09-03 00:27:44 +03:00
committed by GitHub
2 changed files with 45 additions and 7 deletions

View File

@@ -5,12 +5,16 @@ import com.krylov.refound.dto.UserUpdateDto;
import com.krylov.refound.service.UserService; import com.krylov.refound.service.UserService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController @RestController
@RequestMapping("/api/v1/users") @RequestMapping("/api/v1/users")
@@ -20,11 +24,24 @@ public class UserController {
private final UserService userService; private final UserService userService;
@PutMapping("/{id}") @GetMapping("/{id}")
public ResponseEntity<UserResponseDto> updateUser(@PathVariable Long id, @ModelAttribute UserUpdateDto userUpdateDto) { public ResponseEntity<UserResponseDto> getUserById(@PathVariable Long id) {
log.info("updateUser, id={}", id); log.info("getUserById, id={}", id);
UserResponseDto userResponseDto = userService.updateUser(id, userUpdateDto); UserResponseDto userResponseDto = userService.getUserById(id);
return ResponseEntity.ok(userResponseDto); return ResponseEntity.ok(userResponseDto);
} }
} @PutMapping(value = "/{id}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<UserResponseDto> updateUser(
@PathVariable Long id,
@ModelAttribute UserUpdateDto userUpdateDto,
@RequestParam(value = "avatar", required = false) MultipartFile avatar,
@RequestParam(value = "removeAvatar", required = false) Boolean removeAvatar) {
log.info("updateUser, id={}", id);
log.info("updateUser avatar empty: {}", avatar == null || avatar.isEmpty());
userUpdateDto.setAvatar(avatar);
UserResponseDto userResponseDto = userService.updateUser(id, userUpdateDto, removeAvatar);
return ResponseEntity.ok(userResponseDto);
}
}

View File

@@ -35,7 +35,13 @@ public class UserService {
.orElseThrow(() -> new ApiException(ErrorCode.NOT_FOUND, "User not found", HttpStatus.NOT_FOUND)); .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) User user = repository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("User not found")); .orElseThrow(() -> new EntityNotFoundException("User not found"));
@@ -53,13 +59,23 @@ public class UserService {
user.setAvatarUrl(newAvatarKey); user.setAvatarUrl(newAvatarKey);
// подчищаем старый файл, если он был и это реально ключ MinIO // подчищаем старый файл, если он был и это реально ключ MinIO
if (oldAvatarKey != null && !oldAvatarKey.isBlank()) { if (oldAvatarKey != null && !oldAvatarKey.isBlank() && isMinioKey(oldAvatarKey)) {
try { try {
fileStorageService.deleteFile(oldAvatarKey); fileStorageService.deleteFile(oldAvatarKey);
} catch (Exception e) { } catch (Exception e) {
log.warn("Не удалось удалить старый аватар из хранилища: {}", oldAvatarKey, 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); repository.save(user);
@@ -69,6 +85,11 @@ public class UserService {
return userResponseDto; 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) { public Long getUserIdByLogin(String login) {
return repository.findUserByLogin(login) return repository.findUserByLogin(login)
.orElseThrow(() -> new EntityNotFoundException("User not found")); .orElseThrow(() -> new EntityNotFoundException("User not found"));