Skip to content
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

refactor: 동화 전체 조회API, 사용자 기존 비밀번호와 비교하는 예외처리 코드 추가 #46

Merged
merged 1 commit into from
Oct 16, 2023
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 @@ -37,6 +37,7 @@ public enum ErrorCode {
DUPLICATED_NICKNAME(MemberCode.NICKNAME_DUPLICATED.getCode(), CONFLICT, "동일한 닉네임이 존재하는 경우"),
MEMBER_IMAGE_NON_EXISTED(MemberCode.NON_EXISTED_IMAGE.getCode(), CONFLICT, "삭제할 이미지가 존재하지 않는 경우"),
LOGIN_FAILED(MemberCode.FAILED_LOGIN.getCode(), NOT_FOUND, "아이디 또는 비밀번호가 일치하지 않는 경우"),
INVALID_PASSWORD(MemberCode.INVALID_PASSWORD.getCode(), BAD_REQUEST, "기존의 비밀번호와 일치하지 않는 경우"),

// NOTICE
ANNOUNCE_NOT_FOUND(AnnouncementCode.ANNOUNCEMENT_NOT_FOUND.getCode(), NOT_FOUND, "DB에 해당 Announcement의 데이터가 존재하지 않는 경우"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum MemberCode {
NICKNAME_DUPLICATED("M-003"),
NON_EXISTED_IMAGE("M-004"),
FAILED_LOGIN("M-005"),
INVALID_PASSWORD("M-006"),
;

private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Long register(MemberRegisterCommand memberRegisterCommand) {
@Transactional
public Long login(MemberLoginCommand memberLoginCommand) {
Member member = findMemberForLogin(memberLoginCommand);
member.verifyCorrectPassword(memberLoginCommand);
member.verifyMemberEmailAndPassword(memberLoginCommand);
return member.getId();
}

Expand All @@ -47,7 +47,8 @@ public void updateNickname(Long memberId, String nickname) {
@Transactional
public void updatePassword(MemberUpdatePasswordCommand updatePasswordCommand) {
Member member = findMember(updatePasswordCommand.getMemberId());
member.updatePassword(updatePasswordCommand.getPassword());
member.verifyMemberPassword(updatePasswordCommand.getOriginalPassword());
member.updatePassword(updatePasswordCommand.getNewPassword());
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
@Builder
public class MemberUpdatePasswordCommand {
Long memberId;
String password;
String originalPassword;
String newPassword;
}
11 changes: 9 additions & 2 deletions src/main/java/hanium/englishfairytale/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import hanium.englishfairytale.exception.BusinessException;
import hanium.englishfairytale.exception.code.ErrorCode;
import hanium.englishfairytale.member.application.dto.MemberLoginCommand;
import hanium.englishfairytale.member.application.dto.MemberUpdatePasswordCommand;
import hanium.englishfairytale.tale.domain.Tale;
import lombok.AccessLevel;
import lombok.Builder;
Expand Down Expand Up @@ -90,9 +91,15 @@ public void makeImageNull() {
this.image = null;
}

public void verifyCorrectPassword(MemberLoginCommand memberLoginCommand) {
if (!Objects.equals(password, memberLoginCommand.getPassword())) {
public void verifyMemberEmailAndPassword(MemberLoginCommand memberLoginCommand) {
if (!Objects.equals(password, memberLoginCommand.getPassword()) || !Objects.equals(email, memberLoginCommand.getEmail())) {
throw new BusinessException(ErrorCode.LOGIN_FAILED);
}
}

public void verifyMemberPassword(String originalPassword) {
if (!Objects.equals(password, originalPassword)) {
throw new BusinessException(ErrorCode.INVALID_PASSWORD);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public void updateNickname(@RequestParam Long memberId, @RequestParam String nic
memberCommandService.updateNickname(memberId, nickname);
}

@PatchMapping("/password")
public void updatePassword(@Validated @RequestBody MemberUpdatePasswordDto updatePasswordDto) {
memberCommandService.updatePassword(toPasswordUpdateCommand(updatePasswordDto));
@PatchMapping("/password/{memberId}")
public void updatePassword(@PathVariable Long memberId ,@Validated @RequestBody MemberUpdatePasswordDto updatePasswordDto) {
memberCommandService.updatePassword(toPasswordUpdateCommand(memberId, updatePasswordDto));
}

@PatchMapping("/{memberId}/image")
Expand All @@ -72,8 +72,8 @@ private MemberRegisterCommand toCreateCommand(MemberRegisterDto memberRegisterDt
return converter.toCommand(memberRegisterDto, image);
}

private MemberUpdatePasswordCommand toPasswordUpdateCommand(MemberUpdatePasswordDto updatePasswordDto) {
return converter.toCommand(updatePasswordDto);
private MemberUpdatePasswordCommand toPasswordUpdateCommand(Long memberId, MemberUpdatePasswordDto updatePasswordDto) {
return converter.toCommand(memberId, updatePasswordDto);
}

private MemberImageUpdateCommand toImageUpdateCommand(Long memberId, MultipartFile image) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ public MemberRegisterCommand toCommand(MemberRegisterDto dto, MultipartFile imag
.build();
}

public MemberUpdatePasswordCommand toCommand(MemberUpdatePasswordDto dto) {
public MemberUpdatePasswordCommand toCommand(Long memberId, MemberUpdatePasswordDto dto) {
return MemberUpdatePasswordCommand.builder()
.memberId(dto.getMemberId())
.password(dto.getPassword())
.memberId(memberId)
.originalPassword(dto.getOriginalPassword())
.newPassword(dto.getNewPassword())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
@AllArgsConstructor
public class MemberUpdatePasswordDto {
@NotNull
private Long memberId;
@Pattern(regexp = "^(?i)(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]{2,10}$")
private String originalPassword;
@NotNull
@Pattern(regexp = "^(?i)(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]{2,10}$")
private String password;
private String newPassword;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ public class TalesInfo {

private Long taleId;
private String title;
private String imgUrl;
private String imgStatus;
private List<String> keywords;

public TalesInfo(Tale tale, List<Keyword> keywordList) {
this.taleId = tale.getId();
this.title = tale.getTitle();
this.imgUrl = tale.getImage().getTaleImage() == null ? null : tale.getImage().getUrl();
this.imgStatus = tale.getImageStatus();

keywords = new ArrayList<>();
for (Keyword keyword : keywordList) {
keywords.add(keyword.getWord());
Expand Down