-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: MemberActivityService Query와 Command 로 분리
- Loading branch information
Showing
3 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
...d/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityCommandService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Long> activityIds = memberActivityInitialRequest.getActivityIds(); | ||
saveMemberActivities(member, activityIds); | ||
|
||
member.updateName(memberActivityInitialRequest.getName()); | ||
} | ||
|
||
private void saveMemberActivities(final Member member, final List<Long> activityIds) { | ||
final List<MemberActivity> memberActivities = activityRepository.findAllById(activityIds) | ||
.stream() | ||
.map(it -> new MemberActivity(it, member)) | ||
.collect(toUnmodifiableList()); | ||
|
||
validateAllActivityIdsExist(activityIds, memberActivities); | ||
|
||
memberActivityRepository.saveAll(memberActivities); | ||
} | ||
|
||
private void validateAllActivityIdsExist( | ||
final List<Long> activityIds, | ||
final List<MemberActivity> memberActivities | ||
) { | ||
if (memberActivities.size() != activityIds.size()) { | ||
throw new MemberException(MemberExceptionType.INVALID_ACTIVITY_IDS); | ||
} | ||
} | ||
|
||
public List<MemberActivityResponses> addActivity( | ||
final Member member, | ||
final MemberActivityAddRequest memberActivityAddRequest | ||
) { | ||
final List<Long> activityIds = memberActivityAddRequest.getActivityIds(); | ||
final List<MemberActivity> 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<MemberActivity> memberActivities, | ||
final List<Long> activityIds) { | ||
return memberActivities | ||
.stream() | ||
.anyMatch(memberActivity -> | ||
activityIds.contains(memberActivity.getActivity().getId()) | ||
); | ||
} | ||
|
||
private boolean hasDuplicateId(final List<Long> activityIds) { | ||
return new HashSet<>(activityIds).size() != activityIds.size(); | ||
} | ||
|
||
public List<MemberActivityResponses> deleteActivity( | ||
final Member member, | ||
final List<Long> deleteActivityIds | ||
) { | ||
final List<Long> savedMemberActivityIds = | ||
memberActivityRepository.findAllByMemberAndActivityIds(member, deleteActivityIds) | ||
.stream() | ||
.map(MemberActivity::getId) | ||
.collect(toUnmodifiableList()); | ||
|
||
memberActivityRepository.deleteAllByIdInBatch(savedMemberActivityIds); | ||
|
||
return MemberActivityResponses.from(memberActivityRepository.findAllByMember(member)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...end/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityQueryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MemberActivityResponses> findActivities(final Long memberId) { | ||
final Member member = memberRepository.getByIdOrElseThrow(memberId); | ||
return MemberActivityResponses.from(memberActivityRepository.findAllByMember(member)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters