diff --git a/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityService.java b/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityService.java index d5e1947e9..128457ade 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityService.java +++ b/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityService.java @@ -13,6 +13,7 @@ import com.emmsale.member.domain.MemberRepository; import com.emmsale.member.exception.MemberException; import com.emmsale.member.exception.MemberExceptionType; +import java.util.HashSet; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -31,6 +32,9 @@ public void registerActivities( final Member member, final MemberActivityInitialRequest memberActivityInitialRequest ) { + if (member.isOnboarded()) { + throw new MemberException(MemberExceptionType.ALREADY_ONBOARDING); + } final List activityIds = memberActivityInitialRequest.getActivityIds(); saveMemberActivities(member, activityIds); @@ -62,11 +66,32 @@ public List addActivity( final MemberActivityAddRequest memberActivityAddRequest ) { final List activityIds = memberActivityAddRequest.getActivityIds(); + final List memberActivities = memberActivityRepository.findAllByMember(member); + if (hasDuplicateId(memberActivities, activityIds)) { + throw new MemberException(MemberExceptionType.DUPLICATE_ACTIVITY); + } + if (isAlreadyExistActivity(memberActivities, activityIds)) { + throw new MemberException(MemberExceptionType.ALREADY_EXIST_ACTIVITY); + } saveMemberActivities(member, activityIds); return MemberActivityResponses.from(memberActivityRepository.findAllByMember(member)); } + private boolean isAlreadyExistActivity(final List memberActivities, + final List activityIds) { + return memberActivities + .stream() + .anyMatch(memberActivity -> + activityIds.contains(memberActivity.getActivity().getId()) + ); + } + + private boolean hasDuplicateId(final List memberActivities, + final List activityIds) { + return new HashSet<>(activityIds).size() != memberActivities.size(); + } + public List deleteActivity( final Member member, final MemberActivityDeleteRequest memberActivityDeleteRequest diff --git a/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java b/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java index b846c4920..9d98ed427 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java +++ b/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java @@ -10,10 +10,26 @@ public enum MemberExceptionType implements BaseExceptionType { "해당 멤버는 존재하지 않습니다." ), + ALREADY_ONBOARDING( + HttpStatus.BAD_REQUEST, + "이미 온보딩을 완료한 사용자입니다." + ), + INVALID_ACTIVITY_IDS( HttpStatus.BAD_REQUEST, "요청한 activity id들 중에 유효하지 않은 값이 존재합니다" ), + + ALREADY_EXIST_ACTIVITY( + HttpStatus.BAD_REQUEST, + "이미 등록된 활동입니다." + ), + + DUPLICATE_ACTIVITY( + HttpStatus.BAD_REQUEST, + "요청에 중복된 활동 ID가 포함되어 있습니다." + ), + NULL_DESCRIPTION( HttpStatus.BAD_REQUEST, "한 줄 자기소개는 null이 될 수 없습니다." diff --git a/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityServiceTest.java b/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityServiceTest.java index ef4f4629e..c4a3d203a 100644 --- a/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityServiceTest.java +++ b/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityServiceTest.java @@ -17,6 +17,7 @@ import com.emmsale.member.exception.MemberException; import com.emmsale.member.exception.MemberExceptionType; import java.util.List; +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -33,7 +34,7 @@ class MemberActivityServiceTest extends ServiceIntegrationTestHelper { @DisplayName("Activity의 id를 통해서, 사용자의 Activity를 등록하고 사용자의 이름을 수정할 수 있다.") void registerActivities() throws Exception { //given - final List activityIds = List.of(1L, 2L, 3L, 4L); + final List activityIds = List.of(1L, 2L, 3L); final long savedMemberId = 1L; final Member member = memberRepository.findById(savedMemberId).get(); @@ -49,6 +50,31 @@ void registerActivities() throws Exception { ); } + @Test + @DisplayName("특정 사용자에 대해 이미 등록되어 있는 Activity를 등록하려고 하면 예외를 반환한다.") + void registerActivities_fail() throws Exception { + //given + final List activityIds = List.of(1L, 2L, 3L, 4L); + final long savedMemberId = 1L; + + final Member member = memberRepository.findById(savedMemberId).get(); + final String updateName = "우르"; + + final MemberActivityInitialRequest request = new MemberActivityInitialRequest(updateName, + activityIds); + + // when + memberActivityService.registerActivities(member, request); + final ThrowingCallable actual = () -> memberActivityService.registerActivities(member, + request); + + // then + assertThatThrownBy(actual) + .isInstanceOf(MemberException.class) + .hasMessage(MemberExceptionType.ALREADY_ONBOARDING.errorMessage()); + + } + @Test @DisplayName("Activity의 id를 통해서, 사용자의 Activity에 추가할 수 있다.") void addActivity() throws Exception { @@ -82,7 +108,6 @@ void addActivity() throws Exception { //when final List actual = memberActivityService.addActivity(member, request); - //then assertThat(expected) .usingRecursiveComparison() @@ -95,7 +120,7 @@ void addActivity() throws Exception { void test_addActivity_invalid_activity_ids_Exception() throws Exception { //given final Member savedMember = memberRepository.findById(1L).get(); - final List activityIds = List.of(1L, 2L, 7L); + final List activityIds = List.of(4L, 5L, 7L); final MemberActivityAddRequest request = new MemberActivityAddRequest(activityIds); //when & then @@ -104,6 +129,34 @@ void test_addActivity_invalid_activity_ids_Exception() throws Exception { .hasMessage(MemberExceptionType.INVALID_ACTIVITY_IDS.errorMessage()); } + @Test + @DisplayName("addActivity() : 이미 존재하는 activityId들이 있으면 ALREADY_EXIST_ACTIVITY Exception이 발생합니다.") + void test_addActivity_ALREADY_EXIST_ACTIVITY_Exception_duplicate_try() throws Exception { + //given + final Member savedMember = memberRepository.findById(1L).get(); + final List activityIds = List.of(1L, 2L, 7L); + final MemberActivityAddRequest request = new MemberActivityAddRequest(activityIds); + + // when, then + assertThatThrownBy(() -> memberActivityService.addActivity(savedMember, request)) + .isInstanceOf(MemberException.class) + .hasMessage(MemberExceptionType.ALREADY_EXIST_ACTIVITY.errorMessage()); + } + + @Test + @DisplayName("addActivity() : 중복되는 Activity Id가 포함되어 있으면 ALREADY_EXIST_ACTIVITY Exception이 발생합니다.") + void test_addActivity_ALREADY_EXIST_ACTIVITY_Exception_duplicate_input() throws Exception { + //given + final Member savedMember = memberRepository.findById(1L).get(); + final List activityIds = List.of(4L, 4L, 5L); + final MemberActivityAddRequest request = new MemberActivityAddRequest(activityIds); + + // when, then + assertThatThrownBy(() -> memberActivityService.addActivity(savedMember, request)) + .isInstanceOf(MemberException.class) + .hasMessage(MemberExceptionType.DUPLICATE_ACTIVITY.errorMessage()); + } + @Test @DisplayName("deleteActivity() : member Id와 삭제할 activity Id를 통해서 사용자의 Activity를 삭제할 수 있다.") void test_deleteActivity() throws Exception { diff --git a/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberQueryServiceTest.java b/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberQueryServiceTest.java index e26038ee8..2be30c077 100644 --- a/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberQueryServiceTest.java +++ b/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberQueryServiceTest.java @@ -24,7 +24,7 @@ class MemberQueryServiceTest extends ServiceIntegrationTestHelper { @DisplayName("사용자를 조회하고 조회 결과를 반환한다.") void findOrCreateMemberTest() { //given - final MemberQueryResponse expectResponse = new MemberQueryResponse(1L, true); + final MemberQueryResponse expectResponse = new MemberQueryResponse(1L, false); final GithubProfileResponse githubProfileFromGithub = new GithubProfileResponse("1", "name", "username", "https://imageUrl.com"); @@ -67,7 +67,7 @@ class FindProfile { void findProfile_success() { //given final Long memberId = 1L; - final MemberProfileResponse expectResponse = new MemberProfileResponse(memberId, "member1", + final MemberProfileResponse expectResponse = new MemberProfileResponse(memberId, null, "", "https://imageurl.com"); //when diff --git a/backend/emm-sale/src/test/resources/data-test.sql b/backend/emm-sale/src/test/resources/data-test.sql index 22365ae5a..4fa6785ae 100644 --- a/backend/emm-sale/src/test/resources/data-test.sql +++ b/backend/emm-sale/src/test/resources/data-test.sql @@ -28,7 +28,7 @@ insert into activity(id, type, name) values (6, 'JOB', 'Backend'); insert into member(id, name, image_url, open_profile_url, github_id, created_at, updated_at) -values (1, 'member1', 'https://imageurl.com', 'https://openprofileurl.com', 1, CURRENT_TIMESTAMP(), +values (1, null, 'https://imageurl.com', 'https://openprofileurl.com', 1, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP()); insert into member(id, name, image_url, open_profile_url, github_id, created_at, updated_at)