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

fix : 데이터베이스 유니크 중복 예외 처리 #555

Merged
merged 6 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -4,6 +4,7 @@
import java.security.SecureRandom;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.timecapsulearchive.core.domain.auth.data.dto.VerificationMessageSendDto;
Expand All @@ -13,6 +14,7 @@
import site.timecapsulearchive.core.domain.member.entity.Member;
import site.timecapsulearchive.core.domain.member.entity.MemberTemporary;
import site.timecapsulearchive.core.domain.member.exception.MemberNotFoundException;
import site.timecapsulearchive.core.domain.member.exception.MemberTagDuplicatedException;
import site.timecapsulearchive.core.domain.member.repository.MemberRepository;
import site.timecapsulearchive.core.domain.member.repository.MemberTemporaryRepository;
import site.timecapsulearchive.core.global.security.encryption.AESEncryptionManager;
Expand Down Expand Up @@ -118,7 +120,11 @@ private Long updateToVerifiedMember(final Long memberId, final byte[] plain) {
final Member verifiedMember = memberTemporary.toMember(hashEncryptionManager.encrypt(plain),
aesEncryptionManager.encryptWithPrefixIV(plain));

memberRepository.save(verifiedMember);
try {
memberRepository.saveAndFlush(verifiedMember);
} catch (DataIntegrityViolationException e) {
throw new MemberTagDuplicatedException();
}

return verifiedMember.getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,9 @@ private Member(String profileUrl, String nickname, SocialType socialType, String
public void updateTagLowerCaseSocialType() {
this.tag = TagGenerator.lowercase(email, socialType);
}

public void updateData(String nickname, String tag) {
this.nickname = nickname;
this.tag = tag;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package site.timecapsulearchive.core.domain.member.exception;

import site.timecapsulearchive.core.global.error.ErrorCode;
import site.timecapsulearchive.core.global.error.exception.BusinessException;

public class MemberTagDuplicatedException extends BusinessException {

public MemberTagDuplicatedException() {
super(ErrorCode.MEMBER_TAG_DUPLICATED_ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface MemberRepository extends Repository<Member, Long>, MemberQueryR

Member save(Member createMember);

void saveAndFlush(Member member);

Optional<Member> findMemberById(Long memberId);

@Modifying(clearAutomatically = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.timecapsulearchive.core.domain.member.data.dto.MemberDetailDto;
Expand All @@ -15,6 +16,7 @@
import site.timecapsulearchive.core.domain.member.entity.SocialType;
import site.timecapsulearchive.core.domain.member.exception.AlreadyVerifiedException;
import site.timecapsulearchive.core.domain.member.exception.MemberNotFoundException;
import site.timecapsulearchive.core.domain.member.exception.MemberTagDuplicatedException;
import site.timecapsulearchive.core.domain.member.exception.NotVerifiedMemberException;
import site.timecapsulearchive.core.domain.member.repository.MemberRepository;
import site.timecapsulearchive.core.domain.member.repository.MemberTemporaryRepository;
Expand Down Expand Up @@ -153,12 +155,15 @@ public void updateMemberData(
final Long memberId,
final UpdateMemberDataDto updateMemberDataDto
) {
final int updateMemberData = memberRepository.updateMemberData(memberId,
updateMemberDataDto.nickname(), updateMemberDataDto.tag());
final Member member = memberRepository.findMemberById(memberId).orElseThrow(MemberNotFoundException::new);

if (updateMemberData != 1) {
throw new MemberNotFoundException();
member.updateData(updateMemberDataDto.nickname(), updateMemberDataDto.tag());
try {
memberRepository.saveAndFlush(member);
} catch (DataIntegrityViolationException e) {
throw new MemberTagDuplicatedException();
}

}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public enum ErrorCode {

MEMBER_NOT_FOUND_ERROR(404, "MEMBER-003", "사용자 데이터를 찾지 못하였습니다."),

MEMBER_TAG_DUPLICATED_ERROR(400, "MEMBER-004", "사용자의 태그가 중복 되었습니다."),

//geo
GEO_TRANSFORMED_ERROR(400, "GEO-001", "좌표 변환에 실패했습니다. 입력이 유효한지 확인해주세요"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class MessageVerificationServiceTest {
RECEIVER);

//then
verify(memberRepository, times(1)).save(any(Member.class));
verify(memberRepository, times(1)).saveAndFlush(any(Member.class));
}

@Test
Expand Down
Loading