Skip to content

Commit

Permalink
refactor: #73 service 코드 분리 및 코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
tinon1004 committed Mar 25, 2024
1 parent d09adb5 commit f6c1b28
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 23 deletions.
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 @@ -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);
}

/**
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,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<UserProfileImage> 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<UserProfileImage> 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<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);
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;
}
}

0 comments on commit f6c1b28

Please sign in to comment.