-
Notifications
You must be signed in to change notification settings - Fork 0
feat: 멤버 관련 기능 추가 #8
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 3 commits
e16de95
b2bdf51
7b3f77b
5e4578b
8be1292
4398120
86dce3e
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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package knu_chatbot.application.dto.response; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.ToString; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| @ToString | ||
| public class ChangePasswordResponse { | ||
|
|
||
| private String message; | ||
|
|
||
| @Builder | ||
| public ChangePasswordResponse(String message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| public static ChangePasswordResponse of(String message) { | ||
| return ChangePasswordResponse.builder() | ||
| .message(message) | ||
| .build(); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package knu_chatbot.application.dto.response; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class ChangePasswordServiceRequest { | ||
|
|
||
| private String oldPassword; | ||
| private String newPassword; | ||
|
|
||
| @Builder | ||
| public ChangePasswordServiceRequest(String oldPassword, String newPassword) { | ||
| this.oldPassword = oldPassword; | ||
| this.newPassword = newPassword; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package knu_chatbot.application.dto.response; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| public class LogoutResponse { | ||
|
|
||
| private String message; | ||
|
|
||
| @Builder | ||
| public LogoutResponse(String message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| public static LogoutResponse of(String message) { | ||
| return LogoutResponse.builder() | ||
| .message(message) | ||
| .build(); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package knu_chatbot.application.dto.response; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| public class WithdrawResponse { | ||
|
|
||
| private String message; | ||
|
|
||
| @Builder | ||
| public WithdrawResponse(String message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| public static WithdrawResponse of(String message) { | ||
| return WithdrawResponse.builder() | ||
| .message(message) | ||
| .build(); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,11 @@ | ||
| package knu_chatbot.application.service; | ||
|
|
||
| import knu_chatbot.application.dto.AuthUser; | ||
| import knu_chatbot.application.dto.MemberDto; | ||
| import knu_chatbot.application.dto.request.CheckEmailServiceRequest; | ||
| import knu_chatbot.application.dto.request.LoginServiceRequest; | ||
| import knu_chatbot.application.dto.request.SignupServiceRequest; | ||
| import knu_chatbot.application.dto.response.CheckEmailResponse; | ||
| import knu_chatbot.application.dto.response.LoginResponse; | ||
| import knu_chatbot.application.dto.response.ReissueTokensResponse; | ||
| import knu_chatbot.application.dto.response.SignupResponse; | ||
| import knu_chatbot.application.dto.response.*; | ||
| import knu_chatbot.application.error.ErrorType; | ||
| import knu_chatbot.application.error.KnuChatbotException; | ||
| import knu_chatbot.application.repository.MemberRepository; | ||
|
|
@@ -96,11 +94,39 @@ public MyPageResponse getMyPage(String email) { | |
| return MyPageResponse.from(memberDto); | ||
| } | ||
|
|
||
| public LogoutResponse logout(String refreshToken) { | ||
| memberRepository.deleteRefreshToken(refreshToken); | ||
| return LogoutResponse.of("회원가입이 완료되었습니다."); | ||
|
||
| } | ||
|
|
||
| @Transactional | ||
| public WithdrawResponse withdraw(String email) { | ||
| memberRepository.deleteMemberByEmail(email); | ||
|
|
||
| // TODO : 히스토리 및 채팅 삭제 | ||
|
|
||
| return WithdrawResponse.of("회원탈퇴가 완료되었습니다."); | ||
| } | ||
|
Comment on lines
+106
to
+112
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 | ||
| public ChangePasswordResponse changePassword(AuthUser authUser, ChangePasswordServiceRequest request) { | ||
| MemberDto memberDto = memberRepository.findByEmail(authUser.getEmail()); | ||
|
|
||
| // 비밀번호 검증 | ||
| if (!passwordEncryptor.verifyPassword(request.getOldPassword(), memberDto.getPassword())) { | ||
| throw new KnuChatbotException(ErrorType.USER_LOGIN_ERROR); | ||
|
||
| } | ||
|
|
||
| String newEncryptedPassword = passwordEncryptor.encryptPassword(request.getNewPassword()); | ||
| memberRepository.updatePasswordByEmail(authUser.getEmail(),newEncryptedPassword); | ||
|
|
||
| return ChangePasswordResponse.of("비밀번호가 변경되었습니다."); | ||
| } | ||
|
|
||
| private void validatePasswordMatch(SignupServiceRequest request) { | ||
| if (!request.getPassword().equals(request.getConfirmPassword())) { | ||
| throw new KnuChatbotException(ErrorType.USER_CONFIRM_PASSWORD_ERROR, request); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,8 +35,7 @@ public void save(Member member) { | |
|
|
||
| @Override | ||
| public MemberDto findByEmail(String email) { | ||
| Member member = memberJpaRepository.findByEmail(email) | ||
| .orElseThrow(() -> new KnuChatbotException(ErrorType.USER_INVALID_ERROR)); | ||
| Member member = getOrThrowMemberByEmail(email); | ||
| return memberMapper.convert(member); | ||
| } | ||
|
|
||
|
|
@@ -56,4 +55,23 @@ public void deleteRefreshToken(String refreshToken) { | |
| redisTemplate.delete(refreshToken); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteMemberByEmail(String email) { | ||
| memberJpaRepository.deleteByEmail(email); | ||
| } | ||
|
|
||
| @Override | ||
| public void updatePasswordByEmail(String email, String newEncryptedPassword) { | ||
| Member member = getOrThrowMemberByEmail(email); | ||
|
|
||
| member.changePassword(newEncryptedPassword); | ||
|
|
||
| memberJpaRepository.save(member); | ||
|
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.
|
||
| } | ||
|
|
||
| private Member getOrThrowMemberByEmail(String email) { | ||
| return memberJpaRepository.findByEmail(email) | ||
| .orElseThrow(() -> new KnuChatbotException(ErrorType.USER_INVALID_ERROR)); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package knu_chatbot.presentation.request; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import knu_chatbot.application.dto.response.ChangePasswordServiceRequest; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import org.hibernate.validator.constraints.Length; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| public class ChangePasswordRequest { | ||
|
|
||
| @NotBlank(message = "이전 비밀번호는 필수입니다.") | ||
| private String oldPassword; | ||
|
|
||
| @NotBlank(message = "새로운 비밀번호는 필수입니다.") | ||
| @Length(min = 8, max = 20, message = "길이는 최소 8글자, 최대 20글자 입니다.") | ||
| private String newPassword; | ||
|
|
||
| @Builder | ||
| public ChangePasswordRequest(String oldPassword, String newPassword) { | ||
| this.oldPassword = oldPassword; | ||
| this.newPassword = newPassword; | ||
| } | ||
|
|
||
| public ChangePasswordServiceRequest toServiceRequest() { | ||
| return ChangePasswordServiceRequest.builder() | ||
| .oldPassword(oldPassword) | ||
| .newPassword(newPassword) | ||
| .build(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,10 @@ | ||
| package knu_chatbot.application.service; | ||
|
|
||
| import knu_chatbot.application.dto.AuthUser; | ||
| import knu_chatbot.application.dto.request.CheckEmailServiceRequest; | ||
| import knu_chatbot.application.dto.request.LoginServiceRequest; | ||
| import knu_chatbot.application.dto.request.SignupServiceRequest; | ||
| import knu_chatbot.application.dto.response.CheckEmailResponse; | ||
| import knu_chatbot.application.dto.response.LoginResponse; | ||
| import knu_chatbot.application.dto.response.ReissueTokensResponse; | ||
| import knu_chatbot.application.dto.response.SignupResponse; | ||
| import knu_chatbot.application.dto.response.*; | ||
| import knu_chatbot.application.error.ErrorType; | ||
| import knu_chatbot.application.error.KnuChatbotException; | ||
| import knu_chatbot.application.util.JwtProvider; | ||
|
|
@@ -204,4 +202,36 @@ void reissueTokensWithInvalidRefreshToken() { | |
| .isEqualTo(ErrorType.USER_INVALID_REFRESH_TOKEN_ERROR); | ||
| } | ||
|
|
||
| @DisplayName("비밀번호를 변경한다.") | ||
| @Test | ||
| void changePassword() { | ||
| // given | ||
| String email = "test@test.com"; | ||
| String oldPassword = "oldPassword"; | ||
|
|
||
| SignupServiceRequest signupRequest = SignupServiceRequest.builder() | ||
| .email(email) | ||
| .password(oldPassword) | ||
| .confirmPassword(oldPassword) | ||
| .build(); | ||
|
|
||
| memberService.signup(signupRequest); | ||
|
|
||
| AuthUser authUser = AuthUser.builder() | ||
| .email(email) | ||
| .build(); | ||
|
|
||
| String newPassword = "newPassword"; | ||
| ChangePasswordServiceRequest request = ChangePasswordServiceRequest.builder() | ||
| .oldPassword(oldPassword) | ||
| .newPassword(newPassword) | ||
| .build(); | ||
|
|
||
| // when | ||
| ChangePasswordResponse response = memberService.changePassword(authUser, request); | ||
|
|
||
| // then | ||
| assertThat(response).isNotNull(); | ||
|
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. 테스트의 // then
assertThat(response).isNotNull();
// 새 비밀번호로 로그인 시도
LoginServiceRequest loginRequest = LoginServiceRequest.builder()
.email(email)
.password(newPassword)
.build();
// 예외가 발생하지 않아야 함
assertThatCode(() -> memberService.login(loginRequest)).doesNotThrowAnyException();
// 이전 비밀번호로 로그인 시도
LoginServiceRequest oldLoginRequest = LoginServiceRequest.builder()
.email(email)
.password(oldPassword)
.build();
// 예외가 발생해야 함
assertThatThrownBy(() -> memberService.login(oldLoginRequest))
.isInstanceOf(KnuChatbotException.class); |
||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 클래스는 서비스 계층에 대한 요청 데이터를 담는 DTO이므로,
knu_chatbot.application.dto.request패키지로 이동하는 것이 패키지 구조의 일관성 측면에서 더 적절해 보입니다. 현재는response패키지에 위치해 있어 혼란을 줄 수 있습니다.