diff --git a/src/main/java/com/krylov/refound/controller/UserController.java b/src/main/java/com/krylov/refound/controller/UserController.java index a0d1b29..6414415 100644 --- a/src/main/java/com/krylov/refound/controller/UserController.java +++ b/src/main/java/com/krylov/refound/controller/UserController.java @@ -5,12 +5,16 @@ import com.krylov.refound.dto.UserUpdateDto; import com.krylov.refound.service.UserService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ModelAttribute; 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.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; @RestController @RequestMapping("/api/v1/users") @@ -20,11 +24,24 @@ public class UserController { private final UserService userService; - @PutMapping("/{id}") - public ResponseEntity updateUser(@PathVariable Long id, @ModelAttribute UserUpdateDto userUpdateDto) { - log.info("updateUser, id={}", id); - UserResponseDto userResponseDto = userService.updateUser(id, userUpdateDto); + @GetMapping("/{id}") + public ResponseEntity getUserById(@PathVariable Long id) { + log.info("getUserById, id={}", id); + UserResponseDto userResponseDto = userService.getUserById(id); return ResponseEntity.ok(userResponseDto); } -} + @PutMapping(value = "/{id}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity 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); + } + +} \ No newline at end of file diff --git a/src/main/java/com/krylov/refound/service/UserService.java b/src/main/java/com/krylov/refound/service/UserService.java index d41e288..56ad629 100644 --- a/src/main/java/com/krylov/refound/service/UserService.java +++ b/src/main/java/com/krylov/refound/service/UserService.java @@ -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"));