diff --git a/backend/src/main/java/com/mapbefine/mapbefine/member/application/MemberCommandService.java b/backend/src/main/java/com/mapbefine/mapbefine/member/application/MemberCommandService.java index 2a3f4aa7b..f77ebc500 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/member/application/MemberCommandService.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/member/application/MemberCommandService.java @@ -32,6 +32,9 @@ public MemberCommandService( } public Long save(MemberCreateRequest request) { + validateUniqueName(request.name()); + validateUniqueEmail(request.email()); + Member member = Member.of( request.name(), request.email(), @@ -43,6 +46,18 @@ public Long save(MemberCreateRequest request) { .getId(); } + private void validateUniqueName(String name) { + if (memberRepository.existsByMemberInfoName(name)) { + throw new IllegalArgumentException("이미 존재하는 이름입니다."); + } + } + + private void validateUniqueEmail(String email) { + if (memberRepository.existsByMemberInfoEmail(email)) { + throw new IllegalArgumentException("이미 존재하는 이메일입니다."); + } + } + public Long saveMemberTopicPermission( AuthMember authMember, MemberTopicPermissionCreateRequest request diff --git a/backend/src/main/java/com/mapbefine/mapbefine/member/domain/MemberRepository.java b/backend/src/main/java/com/mapbefine/mapbefine/member/domain/MemberRepository.java index ce532c1e5..4935e1628 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/member/domain/MemberRepository.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/member/domain/MemberRepository.java @@ -7,6 +7,8 @@ public interface MemberRepository extends JpaRepository { Optional findByMemberInfoEmail(String email); + boolean existsByMemberInfoName(String name); + boolean existsByMemberInfoEmail(String email); } diff --git a/backend/src/test/java/com/mapbefine/mapbefine/member/application/MemberCommandServiceTest.java b/backend/src/test/java/com/mapbefine/mapbefine/member/application/MemberCommandServiceTest.java index d72fd0aad..c811892c3 100644 --- a/backend/src/test/java/com/mapbefine/mapbefine/member/application/MemberCommandServiceTest.java +++ b/backend/src/test/java/com/mapbefine/mapbefine/member/application/MemberCommandServiceTest.java @@ -9,12 +9,14 @@ import com.mapbefine.mapbefine.member.domain.Member; import com.mapbefine.mapbefine.member.domain.MemberRepository; import com.mapbefine.mapbefine.member.domain.Role; +import com.mapbefine.mapbefine.member.dto.request.MemberCreateRequest; import com.mapbefine.mapbefine.member.dto.request.MemberTopicPermissionCreateRequest; import com.mapbefine.mapbefine.member.dto.response.MemberDetailResponse; import com.mapbefine.mapbefine.topic.TopicFixture; import com.mapbefine.mapbefine.topic.domain.Topic; import com.mapbefine.mapbefine.topic.domain.TopicRepository; import java.util.NoSuchElementException; +import java.util.Optional; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -214,4 +216,75 @@ void deleteMemberTopicPermissionByCreator_whenNoneExistsPermission_thenFail() { .isInstanceOf(NoSuchElementException.class); } + @Test + @DisplayName("유저를 저장한다.") + void save() { + // given + Member member = MemberFixture.create( + "member", + "member@naver.com", + Role.USER + ); + MemberCreateRequest memberCreateRequest = new MemberCreateRequest( + member.getMemberInfo().getName(), + member.getMemberInfo().getEmail(), + member.getMemberInfo().getImageUrl(), + member.getMemberInfo().getRole() + ); + + // when + Long savedId = memberCommandService.save(memberCreateRequest); + Member savedResult = memberRepository.findById(savedId) + .orElseThrow(NoSuchElementException::new); + + // then + assertThat(savedResult).usingRecursiveComparison() + .ignoringFields("id") + .isEqualTo(member); + } + + @Test + @DisplayName("이미 존재하는 이름으로 유저를 저장할 때 예외가 발생한다.") + void save_whenDuplicateName_thenFail() { + // given + Member member = MemberFixture.create( + "member", + "member@naver.com", + Role.USER + ); + memberRepository.save(member); + MemberCreateRequest memberCreateRequest = new MemberCreateRequest( + member.getMemberInfo().getName(), + "memberr@naver.com", + member.getMemberInfo().getImageUrl(), + member.getMemberInfo().getRole() + ); + + // when + assertThatThrownBy(() -> memberCommandService.save(memberCreateRequest)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + @DisplayName("이미 존재하는 이메일로 유저를 저장할 때 예외가 발생한다.") + void save_whenDuplicateEmail_thenFail() { + // given + Member member = MemberFixture.create( + "member", + "member@naver.com", + Role.USER + ); + memberRepository.save(member); + MemberCreateRequest memberCreateRequest = new MemberCreateRequest( + "memberr", + member.getMemberInfo().getEmail(), + member.getMemberInfo().getImageUrl(), + member.getMemberInfo().getRole() + ); + + // when + assertThatThrownBy(() -> memberCommandService.save(memberCreateRequest)) + .isInstanceOf(IllegalArgumentException.class); + } + }