-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
내 프로필 사진 변경 API 구현
- Loading branch information
Showing
12 changed files
with
378 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/com/ajou/hertz/domain/user/repository/UserProfileImageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.ajou.hertz.domain.user.repository; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.ajou.hertz.domain.user.entity.UserProfileImage; | ||
|
||
public interface UserProfileImageRepository extends JpaRepository<UserProfileImage, Long> { | ||
Optional<UserProfileImage> findByUser_Id(Long userId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/com/ajou/hertz/domain/user/service/UserProfileImageCommandService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.ajou.hertz.domain.user.service; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import com.ajou.hertz.common.file.dto.FileDto; | ||
import com.ajou.hertz.common.file.service.FileService; | ||
import com.ajou.hertz.domain.user.entity.User; | ||
import com.ajou.hertz.domain.user.entity.UserProfileImage; | ||
import com.ajou.hertz.domain.user.repository.UserProfileImageRepository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Transactional | ||
@Service | ||
public class UserProfileImageCommandService { | ||
|
||
private final FileService fileService; | ||
private final UserProfileImageRepository userProfileImageRepository; | ||
|
||
private static final String USER_PROFILE_IMAGE_UPLOAD_PATH = "user-profile-images/"; | ||
|
||
/** | ||
* 유저의 프로필 이미지를 업데이트한다. | ||
* | ||
* @param user 프로필 이미지를 업데이트할 유저 | ||
* @param newProfileImage 새로운 프로필 이미지 | ||
* | ||
* @return 새로운 프로필 이미지 URL | ||
*/ | ||
public String updateProfileImage(User user, MultipartFile newProfileImage) { | ||
deleteOldProfileImage(user.getId()); | ||
userProfileImageRepository.flush(); | ||
UserProfileImage newUserProfileImage = uploadNewProfileImage(user, newProfileImage); | ||
return 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); | ||
fileService.deleteFile(oldProfileImage.getStoredName()); | ||
} | ||
} | ||
|
||
/** | ||
* 새로운 프로필 이미지를 업로드한다. | ||
* @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); | ||
return newUserProfileImage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.