Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -31,8 +31,8 @@ public ResponseEntity<MyPageResponse> getMyPageInfo(
@PatchMapping
public ResponseEntity<Void> updateMyPageInfo(
@AuthorizedUser SiteUser siteUser,
@RequestParam("file") MultipartFile imageFile,
@RequestParam("nickname") String nickname
@RequestParam(value = "file", required = false) MultipartFile imageFile,
@RequestParam(value = "nickname", required = false) String nickname
) {
myPageService.updateMyPageInfo(siteUser, imageFile, nickname);
return ResponseEntity.ok().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import static com.example.solidconnection.custom.exception.ErrorCode.CAN_NOT_CHANGE_NICKNAME_YET;
import static com.example.solidconnection.custom.exception.ErrorCode.NICKNAME_ALREADY_EXISTED;
import static com.example.solidconnection.custom.exception.ErrorCode.PROFILE_IMAGE_NEEDED;

@RequiredArgsConstructor
@Service
Expand All @@ -48,19 +47,21 @@ public MyPageResponse getMyPageInfo(SiteUser siteUser) {
* */
@Transactional
public void updateMyPageInfo(SiteUser siteUser, MultipartFile imageFile, String nickname) {
validateNicknameUnique(nickname);
validateNicknameNotChangedRecently(siteUser.getNicknameModifiedAt());
validateProfileImageNotEmpty(imageFile);

if (!isDefaultProfileImage(siteUser.getProfileImageUrl())) {
s3Service.deleteExProfile(siteUser);
if (nickname != null) {
validateNicknameUnique(nickname);
validateNicknameNotChangedRecently(siteUser.getNicknameModifiedAt());
siteUser.setNickname(nickname);
siteUser.setNicknameModifiedAt(LocalDateTime.now());
}
UploadedFileUrlResponse uploadedFile = s3Service.uploadFile(imageFile, ImgType.PROFILE);
String profileImageUrl = uploadedFile.fileUrl();

siteUser.setProfileImageUrl(profileImageUrl);
siteUser.setNickname(nickname);
siteUser.setNicknameModifiedAt(LocalDateTime.now());
if (imageFile != null && !imageFile.isEmpty()) {
UploadedFileUrlResponse uploadedFile = s3Service.uploadFile(imageFile, ImgType.PROFILE);
if (!isDefaultProfileImage(siteUser.getProfileImageUrl())) {
s3Service.deleteExProfile(siteUser);
}
String profileImageUrl = uploadedFile.fileUrl();
siteUser.setProfileImageUrl(profileImageUrl);
}
siteUserRepository.save(siteUser);
}

Expand All @@ -81,12 +82,6 @@ private void validateNicknameNotChangedRecently(LocalDateTime lastModifiedAt) {
}
}

private void validateProfileImageNotEmpty(MultipartFile imageFile) {
if (imageFile == null || imageFile.isEmpty()) {
throw new CustomException(PROFILE_IMAGE_NEEDED);
}
}

private boolean isDefaultProfileImage(String profileImageUrl) {
String prefix = "profile/";
return profileImageUrl == null || !profileImageUrl.startsWith(prefix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import static com.example.solidconnection.custom.exception.ErrorCode.CAN_NOT_CHANGE_NICKNAME_YET;
import static com.example.solidconnection.custom.exception.ErrorCode.NICKNAME_ALREADY_EXISTED;
import static com.example.solidconnection.custom.exception.ErrorCode.PROFILE_IMAGE_NEEDED;
import static com.example.solidconnection.siteuser.service.MyPageService.MIN_DAYS_BETWEEN_NICKNAME_CHANGES;
import static com.example.solidconnection.siteuser.service.MyPageService.NICKNAME_LAST_CHANGE_DATE_FORMAT;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -144,18 +143,6 @@ class 프로필_이미지_수정_테스트 {
// then
then(s3Service).should().deleteExProfile(testUser);
}

@Test
void 빈_이미지_파일로_프로필을_수정하면_예외_응답을_반환한다() {
// given
SiteUser testUser = createSiteUser();
MockMultipartFile emptyFile = createEmptyImageFile();

// when & then
assertThatCode(() -> myPageService.updateMyPageInfo(testUser, emptyFile, "newNickname"))
.isInstanceOf(CustomException.class)
.hasMessage(PROFILE_IMAGE_NEEDED.getMessage());
}
}

@Nested
Expand Down Expand Up @@ -273,15 +260,6 @@ private MockMultipartFile createValidImageFile() {
);
}

private MockMultipartFile createEmptyImageFile() {
return new MockMultipartFile(
"image",
"empty.jpg",
"image/jpeg",
new byte[0]
);
}

private String createExpectedErrorMessage(LocalDateTime modifiedAt) {
String formatLastModifiedAt = String.format(
"(마지막 수정 시간 : %s)",
Expand Down