Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

내 프로필 사진 변경 API 구현 #86

Merged
merged 15 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,7 +50,6 @@
public class UserController {

private final UserCommandService userCommandService;
private final UserProfileImageCommandService userProfileImageCommandService;
private final UserQueryService userQueryService;

@Operation(
Expand Down Expand Up @@ -134,7 +132,7 @@ public ResponseEntity<UserResponse> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
import com.ajou.hertz.domain.user.entity.UserProfileImage;

public interface UserProfileImageRepository extends JpaRepository<UserProfileImage, Long> {
Optional<UserProfileImage> findById(Long userId);
Optional<UserProfileImage> findByUser_Id(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -143,7 +141,10 @@ private String generateRandom16CharString() {
* @return 변경된 유저 정보
*/
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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,38 +19,55 @@
@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 userId 유저 id
* @param user 프로필 이미지를 업데이트할 유저
* @param newProfileImage 새로운 프로필 이미지
*
* @return 업데이트된 유저 정보
* @return 새로운 프로필 이미지 URL
*/
public UserDto updateProfileImage(Long userId, MultipartFile newProfileImage) {

User user = userQueryService.getById(userId);
Optional<UserProfileImage> optionalOldProfileImage = userProfileImageRepository.findById(userId);
public String updateProfileImage(User user, MultipartFile newProfileImage) {
deleteOldProfileImage(user.getId());
UserProfileImage newUserProfileImage = uploadNewProfileImage(user, newProfileImage);
Wo-ogie marked this conversation as resolved.
Show resolved Hide resolved
return newUserProfileImage.getUrl();
}

String uploadPath = "user-profile-images/";
FileDto uploadedFile = fileService.uploadFile(newProfileImage, uploadPath);
String newProfileImageUrl = uploadedFile.getUrl();
/**
* 유저의 프로필 이미지를 삭제한다.
*
* @param userId 프로필 이미지를 삭제할 유저의 id
*/
private void deleteOldProfileImage(Long userId) {
Optional<UserProfileImage> 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);
/**
* 새로운 프로필 이미지를 업로드한다.
* @param user 프로필 이미지를 업데이트할 유저
* @param newProfileImage 새로운 프로필 이미지
*
* @return 새로운 프로필 이미지 entity
*/
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;
}
}
Loading