diff --git a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java index 32fd064..e677f9a 100644 --- a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java +++ b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java @@ -28,7 +28,6 @@ import com.ajou.hertz.domain.user.dto.response.UserResponse; import com.ajou.hertz.domain.user.dto.response.UserWithLinkedAccountInfoResponse; import com.ajou.hertz.domain.user.service.UserCommandService; -import com.ajou.hertz.domain.user.service.UserProfileImageCommandService; import com.ajou.hertz.domain.user.service.UserQueryService; import io.swagger.v3.oas.annotations.Operation; @@ -51,7 +50,6 @@ public class UserController { private final UserCommandService userCommandService; - private final UserProfileImageCommandService userProfileImageCommandService; private final UserQueryService userQueryService; @Operation( @@ -134,7 +132,7 @@ public ResponseEntity signUpV1( ) public UserResponse updateProfileImageV1( @AuthenticationPrincipal UserPrincipal userPrincipal, - @RequestPart("profileImage") MultipartFile profileImage + @RequestPart MultipartFile profileImage ) { UserDto userUpdated = userCommandService.updateUserProfileImage(userPrincipal.getUserId(), profileImage); return UserResponse.from(userUpdated); diff --git a/src/main/java/com/ajou/hertz/domain/user/repository/UserProfileImageRepository.java b/src/main/java/com/ajou/hertz/domain/user/repository/UserProfileImageRepository.java index 7cf665b..de1bfb0 100644 --- a/src/main/java/com/ajou/hertz/domain/user/repository/UserProfileImageRepository.java +++ b/src/main/java/com/ajou/hertz/domain/user/repository/UserProfileImageRepository.java @@ -7,5 +7,5 @@ import com.ajou.hertz.domain.user.entity.UserProfileImage; public interface UserProfileImageRepository extends JpaRepository { - Optional findById(Long userId); + Optional findByUser_Id(Long userId); } diff --git a/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java b/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java index 0d2d67b..9a687f9 100644 --- a/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java +++ b/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java @@ -17,7 +17,6 @@ import com.ajou.hertz.domain.user.exception.UserEmailDuplicationException; import com.ajou.hertz.domain.user.exception.UserKakaoUidDuplicationException; import com.ajou.hertz.domain.user.exception.UserPhoneDuplicationException; -import com.ajou.hertz.domain.user.repository.UserProfileImageRepository; import com.ajou.hertz.domain.user.repository.UserRepository; import lombok.RequiredArgsConstructor; @@ -31,7 +30,6 @@ public class UserCommandService { private final UserRepository userRepository; private final PasswordEncoder passwordEncoder; private final HertzProperties hertzProperties; - private final UserProfileImageRepository userProfileImageRepository; private final UserProfileImageCommandService userProfileImageCommandService; /** @@ -142,8 +140,16 @@ private String generateRandom16CharString() { * * @return 변경된 유저 정보 */ + // public UserDto updateUserProfileImage(Long userId, MultipartFile profileImage) { + // User user = userQueryService.getById(userId); + // userProfileImageCommandService.updateProfileImage(user, profileImage); + // return UserDto.from(user); + // } public UserDto updateUserProfileImage(Long userId, MultipartFile profileImage) { - return userProfileImageCommandService.updateProfileImage(userId, profileImage); + User user = userQueryService.getById(userId); + String newProfileImageUrl = userProfileImageCommandService.updateProfileImage(user, profileImage); + user.changeProfileImageUrl(newProfileImageUrl); + return UserDto.from(user); } /** diff --git a/src/main/java/com/ajou/hertz/domain/user/service/UserProfileImageCommandService.java b/src/main/java/com/ajou/hertz/domain/user/service/UserProfileImageCommandService.java index 3c10d50..1e7ca20 100644 --- a/src/main/java/com/ajou/hertz/domain/user/service/UserProfileImageCommandService.java +++ b/src/main/java/com/ajou/hertz/domain/user/service/UserProfileImageCommandService.java @@ -8,7 +8,6 @@ import com.ajou.hertz.common.file.dto.FileDto; import com.ajou.hertz.common.file.service.FileService; -import com.ajou.hertz.domain.user.dto.UserDto; import com.ajou.hertz.domain.user.entity.User; import com.ajou.hertz.domain.user.entity.UserProfileImage; import com.ajou.hertz.domain.user.repository.UserProfileImageRepository; @@ -20,38 +19,89 @@ @Service public class UserProfileImageCommandService { - private final UserQueryService userQueryService; private final FileService fileService; private final UserProfileImageRepository userProfileImageRepository; + private static final String USER_PROFILE_IMAGE_UPLOAD_PATH = "user-profile-images/"; + + // /** + // * 유저의 프로필 이미지를 업데이트한다. + // * + // * @param user 프로필 이미지를 업데이트할 유저 + // * @param newProfileImage 새로운 프로필 이미지 + // */ + // @Transactional + // public void updateProfileImage(User user, MultipartFile newProfileImage) { + // deleteOldProfileImage(user.getId()); + // UserProfileImage newUserProfileImage = uploadNewProfileImage(user, newProfileImage); + // user.changeProfileImageUrl(newUserProfileImage.getUrl()); + // } + // + // /** + // * 유저의 프로필 이미지를 삭제한다. + // * + // * @param userId 프로필 이미지를 삭제할 유저의 id + // */ + // private void deleteOldProfileImage(Long userId) { + // Optional optionalOldProfileImage = userProfileImageRepository.findByUser_Id(userId); + // if (optionalOldProfileImage.isPresent()) { + // UserProfileImage oldProfileImage = optionalOldProfileImage.get(); + // userProfileImageRepository.delete(oldProfileImage); + // userProfileImageRepository.flush(); + // fileService.deleteFile(oldProfileImage.getStoredName()); + // } + // } + // + // /** + // * 새로운 프로필 이미지를 업로드한다. + // * @param user 프로필 이미지를 업데이트할 유저 + // * @param newProfileImage 새로운 프로필 이미지 + // * + // * @return 새로 업로드된 프로필 이미지 + // */ + // private UserProfileImage uploadNewProfileImage(User user, MultipartFile newProfileImage) { + // FileDto uploadedFile = fileService.uploadFile(newProfileImage, USER_PROFILE_IMAGE_UPLOAD_PATH); + // UserProfileImage newUserProfileImage = UserProfileImage.create( + // user, + // uploadedFile.getOriginalName(), + // uploadedFile.getStoredName(), + // uploadedFile.getUrl()); + // userProfileImageRepository.save(newUserProfileImage); + // return newUserProfileImage; + // } + /** * 유저의 프로필 이미지를 업데이트한다. * - * @param userId 유저 id + * @param user 프로필 이미지를 업데이트할 유저 * @param newProfileImage 새로운 프로필 이미지 * - * @return 업데이트된 유저 정보 + * @return 새로운 프로필 이미지 URL */ - public UserDto updateProfileImage(Long userId, MultipartFile newProfileImage) { - - User user = userQueryService.getById(userId); - Optional optionalOldProfileImage = userProfileImageRepository.findById(userId); + public String updateProfileImage(User user, MultipartFile newProfileImage) { + deleteOldProfileImage(user.getId()); + UserProfileImage newUserProfileImage = uploadNewProfileImage(user, newProfileImage); + return newUserProfileImage.getUrl(); + } - String uploadPath = "user-profile-images/"; - FileDto uploadedFile = fileService.uploadFile(newProfileImage, uploadPath); - String newProfileImageUrl = uploadedFile.getUrl(); + private void deleteOldProfileImage(Long userId) { + Optional optionalOldProfileImage = userProfileImageRepository.findByUser_Id(userId); if (optionalOldProfileImage.isPresent()) { UserProfileImage oldProfileImage = optionalOldProfileImage.get(); userProfileImageRepository.delete(oldProfileImage); userProfileImageRepository.flush(); fileService.deleteFile(oldProfileImage.getStoredName()); } + } - UserProfileImage newUserProfileImage = UserProfileImage.create(user, uploadedFile.getOriginalName(), - uploadedFile.getStoredName(), newProfileImageUrl); + private UserProfileImage uploadNewProfileImage(User user, MultipartFile newProfileImage) { + FileDto uploadedFile = fileService.uploadFile(newProfileImage, USER_PROFILE_IMAGE_UPLOAD_PATH); + UserProfileImage newUserProfileImage = UserProfileImage.create( + user, + uploadedFile.getOriginalName(), + uploadedFile.getStoredName(), + uploadedFile.getUrl()); userProfileImageRepository.save(newUserProfileImage); - - user.changeProfileImageUrl(newProfileImageUrl); - return UserDto.from(user); + return newUserProfileImage; } }