diff --git a/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityCommandService.java b/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityCommandService.java new file mode 100644 index 000000000..58816309b --- /dev/null +++ b/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityCommandService.java @@ -0,0 +1,105 @@ +package com.emmsale.member.application; + +import static java.util.stream.Collectors.toUnmodifiableList; + +import com.emmsale.activity.domain.ActivityRepository; +import com.emmsale.member.application.dto.MemberActivityAddRequest; +import com.emmsale.member.application.dto.MemberActivityInitialRequest; +import com.emmsale.member.application.dto.MemberActivityResponses; +import com.emmsale.member.domain.Member; +import com.emmsale.member.domain.MemberActivity; +import com.emmsale.member.domain.MemberActivityRepository; +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; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +@RequiredArgsConstructor +public class MemberActivityCommandService { + + private final MemberActivityRepository memberActivityRepository; + private final ActivityRepository activityRepository; + + 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); + + member.updateName(memberActivityInitialRequest.getName()); + } + + private void saveMemberActivities(final Member member, final List activityIds) { + final List memberActivities = activityRepository.findAllById(activityIds) + .stream() + .map(it -> new MemberActivity(it, member)) + .collect(toUnmodifiableList()); + + validateAllActivityIdsExist(activityIds, memberActivities); + + memberActivityRepository.saveAll(memberActivities); + } + + private void validateAllActivityIdsExist( + final List activityIds, + final List memberActivities + ) { + if (memberActivities.size() != activityIds.size()) { + throw new MemberException(MemberExceptionType.INVALID_ACTIVITY_IDS); + } + } + + public List addActivity( + final Member member, + final MemberActivityAddRequest memberActivityAddRequest + ) { + final List activityIds = memberActivityAddRequest.getActivityIds(); + final List memberActivities = memberActivityRepository.findAllByMember(member); + if (hasDuplicateId(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 activityIds) { + return new HashSet<>(activityIds).size() != activityIds.size(); + } + + public List deleteActivity( + final Member member, + final List deleteActivityIds + ) { + final List savedMemberActivityIds = + memberActivityRepository.findAllByMemberAndActivityIds(member, deleteActivityIds) + .stream() + .map(MemberActivity::getId) + .collect(toUnmodifiableList()); + + memberActivityRepository.deleteAllByIdInBatch(savedMemberActivityIds); + + return MemberActivityResponses.from(memberActivityRepository.findAllByMember(member)); + } +} diff --git a/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityQueryService.java b/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityQueryService.java new file mode 100644 index 000000000..f70313592 --- /dev/null +++ b/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityQueryService.java @@ -0,0 +1,24 @@ +package com.emmsale.member.application; + +import com.emmsale.member.application.dto.MemberActivityResponses; +import com.emmsale.member.domain.Member; +import com.emmsale.member.domain.MemberActivityRepository; +import com.emmsale.member.domain.MemberRepository; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional(readOnly = true) +@RequiredArgsConstructor +public class MemberActivityQueryService { + + private final MemberRepository memberRepository; + private final MemberActivityRepository memberActivityRepository; + + public List findActivities(final Long memberId) { + final Member member = memberRepository.getByIdOrElseThrow(memberId); + return MemberActivityResponses.from(memberActivityRepository.findAllByMember(member)); + } +} diff --git a/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityQueryServiceTest.java b/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityCommandServiceTest.java similarity index 98% rename from backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityQueryServiceTest.java rename to backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityCommandServiceTest.java index 99db4b09c..4730a665d 100644 --- a/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityQueryServiceTest.java +++ b/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityCommandServiceTest.java @@ -21,7 +21,7 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -class MemberActivityQueryServiceTest extends ServiceIntegrationTestHelper { +class MemberActivityCommandServiceTest extends ServiceIntegrationTestHelper { @Autowired private MemberActivityService memberActivityService;