-
Notifications
You must be signed in to change notification settings - Fork 8
refactor: Siteuser 도메인 리팩터링 #336
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
Changes from all commits
389ce9b
0699152
e5a7cc2
158a5f8
6f7cf1f
c8115f3
afdf86f
074ca56
d455281
c88c131
484bcd3
a2c81ab
4f7f4f4
b4c9f98
16e55f7
6265684
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
|
|
||
| import static com.example.solidconnection.common.exception.ErrorCode.CAN_NOT_CHANGE_NICKNAME_YET; | ||
| import static com.example.solidconnection.common.exception.ErrorCode.NICKNAME_ALREADY_EXISTED; | ||
| import static com.example.solidconnection.common.exception.ErrorCode.USER_NOT_FOUND; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Service | ||
|
|
@@ -47,27 +48,23 @@ public MyPageResponse getMyPageInfo(SiteUser siteUser) { | |
| * */ | ||
| @Transactional | ||
| public void updateMyPageInfo(SiteUser siteUser, MultipartFile imageFile, String nickname) { | ||
| SiteUser user = siteUserRepository.findById(siteUser.getId()) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메서드 파라미터로 |
||
| .orElseThrow(() -> new CustomException(USER_NOT_FOUND)); | ||
|
|
||
whqtker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (nickname != null) { | ||
| validateNicknameNotChangedRecently(user.getNicknameModifiedAt()); | ||
whqtker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| validateNicknameUnique(nickname); | ||
| validateNicknameNotChangedRecently(siteUser.getNicknameModifiedAt()); | ||
| siteUser.setNickname(nickname); | ||
| siteUser.setNicknameModifiedAt(LocalDateTime.now()); | ||
| user.setNickname(nickname); | ||
| user.setNicknameModifiedAt(LocalDateTime.now()); | ||
| } | ||
|
|
||
| if (imageFile != null && !imageFile.isEmpty()) { | ||
| UploadedFileUrlResponse uploadedFile = s3Service.uploadFile(imageFile, ImgType.PROFILE); | ||
| if (!isDefaultProfileImage(siteUser.getProfileImageUrl())) { | ||
| s3Service.deleteExProfile(siteUser); | ||
| if (!isDefaultProfileImage(user.getProfileImageUrl())) { | ||
| s3Service.deleteExProfile(user); | ||
| } | ||
| String profileImageUrl = uploadedFile.fileUrl(); | ||
| siteUser.setProfileImageUrl(profileImageUrl); | ||
| } | ||
| siteUserRepository.save(siteUser); | ||
| } | ||
|
|
||
| private void validateNicknameUnique(String nickname) { | ||
| if (siteUserRepository.existsByNickname(nickname)) { | ||
| throw new CustomException(NICKNAME_ALREADY_EXISTED); | ||
| user.setProfileImageUrl(profileImageUrl); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -82,6 +79,12 @@ private void validateNicknameNotChangedRecently(LocalDateTime lastModifiedAt) { | |
| } | ||
| } | ||
|
|
||
| private void validateNicknameUnique(String nickname) { | ||
| if (siteUserRepository.existsByNickname(nickname)) { | ||
| throw new CustomException(NICKNAME_ALREADY_EXISTED); | ||
| } | ||
| } | ||
nayonsoso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private boolean isDefaultProfileImage(String profileImageUrl) { | ||
| String prefix = "profile/"; | ||
| return profileImageUrl == null || !profileImageUrl.startsWith(prefix); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ALTER TABLE site_user | ||
| ADD CONSTRAINT uk_site_user_nickname | ||
| UNIQUE (nickname); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,8 +24,8 @@ class 이메일과_인증_유형이_동일한_사용자는_저장할_수_없다 | |
| @Test | ||
| void 이메일과_인증_유형이_동일한_사용자를_저장하면_예외_응답을_반환한다() { | ||
| // given | ||
| SiteUser user1 = createSiteUser("email", AuthType.KAKAO); | ||
| SiteUser user2 = createSiteUser("email", AuthType.KAKAO); | ||
| SiteUser user1 = createSiteUser("email", "nickname1", AuthType.KAKAO); | ||
| SiteUser user2 = createSiteUser("email", "nickname2", AuthType.KAKAO); | ||
| siteUserRepository.save(user1); | ||
|
|
||
| // when, then | ||
|
|
@@ -36,8 +36,8 @@ class 이메일과_인증_유형이_동일한_사용자는_저장할_수_없다 | |
| @Test | ||
| void 이메일이_같더라도_인증_유형이_다른_사용자는_정상_저장한다() { | ||
| // given | ||
| SiteUser user1 = createSiteUser("email", AuthType.KAKAO); | ||
| SiteUser user2 = createSiteUser("email", AuthType.APPLE); | ||
| SiteUser user1 = createSiteUser("email", "nickname1", AuthType.KAKAO); | ||
| SiteUser user2 = createSiteUser("email", "nickname2", AuthType.APPLE); | ||
| siteUserRepository.save(user1); | ||
|
|
||
| // when, then | ||
|
|
@@ -46,10 +46,42 @@ class 이메일과_인증_유형이_동일한_사용자는_저장할_수_없다 | |
| } | ||
| } | ||
|
|
||
| private SiteUser createSiteUser(String email, AuthType authType) { | ||
| @Nested | ||
| class 닉네임은_중복될_수_없다 { | ||
|
|
||
| @Test | ||
| void 중복된_닉네임으로_사용자를_저장하면_예외_응답을_반환한다() { | ||
| // given | ||
| SiteUser user1 = createSiteUser("email1", "nickname", AuthType.KAKAO); | ||
| SiteUser user2 = createSiteUser("email2", "nickname", AuthType.KAKAO); | ||
| siteUserRepository.save(user1); | ||
|
|
||
| // when, then | ||
| assertThatCode(() -> siteUserRepository.saveAndFlush(user2)) | ||
| .isInstanceOf(DataIntegrityViolationException.class); | ||
|
Comment on lines
+60
to
+61
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. save에서
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DB 레벨 테스트는
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
이 부분에 대해서 제가 아는 내용으로는, (아니라면 지적해주세요!) 물론 Transactional 어노테이션이 있는 테스트 코드라면 반드시 saveAndFlush를 써야 assert문에 걸리겠지만요 ㅎㅎ
저도 고민이 되네요.. 🤔 우리 컨벤션대로라면 save를 써도 될텐데,.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그렇군요 .. 저는 방어적으로 작성한다고 손해보지는 않는다고 생각합니다 ㅎㅎ |
||
| } | ||
|
|
||
| @Test | ||
| void 중복된_닉네임으로_변경하면_예외_응답을_반환한다() { | ||
| // given | ||
| SiteUser user1 = createSiteUser("email1", "nickname1", AuthType.KAKAO); | ||
| SiteUser user2 = createSiteUser("email2", "nickname2", AuthType.KAKAO); | ||
| siteUserRepository.save(user1); | ||
| siteUserRepository.save(user2); | ||
|
|
||
| // when | ||
| user2.setNickname("nickname1"); | ||
|
|
||
| // then | ||
| assertThatCode(() -> siteUserRepository.saveAndFlush(user2)) | ||
| .isInstanceOf(DataIntegrityViolationException.class); | ||
| } | ||
| } | ||
|
|
||
| private SiteUser createSiteUser(String email, String nickname, AuthType authType) { | ||
| return new SiteUser( | ||
| email, | ||
| "nickname", | ||
| nickname, | ||
| "profileImageUrl", | ||
| PreparationStatus.CONSIDERING, | ||
| Role.MENTEE, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.