Skip to content

Commit

Permalink
Feature/#179 커리큘럼 파트의 메서드를 짧게 수정한다 (#210)
Browse files Browse the repository at this point in the history
* refactor: createCurriculumItemAndAssignToParticipant 메서드 리팩토링

* refactor: checkCurriculumItem 관련 리팩토링

* refactor: 권한 체크 구문은 별도의 클래스로 빠져나오도록 수정

* refactor : member관련 권한 class 수정

* refactor: 스터디 관련 권한 별도 class 수정

* refactor: 팀 관련 권한 체크 별도 class 구분

* refactor: findAll.size에서 count로 변경

* fix: studyRole 및 teamRole이 존재하는지 확인하는 로직으로 변경
- 더이상 역할 추가가 없을 예정으로 해당 방식으로 변경하였음

* chore: 적절한 파라미터명으로 변경

* fix: teamRole 관련 메서드 통일

* Merge branch 'develop' into Feature/#179-커리큘럼_파트의_메서드를_짧게_수정한다

# Conflicts:
#	src/main/java/doore/study/application/ParticipantQueryService.java
  • Loading branch information
JJimini authored Jul 25, 2024
1 parent 1db39ea commit 0004826
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package doore.member.application;

import static doore.member.exception.MemberExceptionType.NOT_FOUND_MEMBER_ROLE_IN_TEAM;
import static doore.member.exception.MemberExceptionType.UNAUTHORIZED;

import doore.member.domain.TeamRole;
import doore.member.domain.TeamRoleType;
import doore.member.application.convenience.TeamRoleValidateAccessPermission;
import doore.member.domain.repository.MemberTeamRepository;
import doore.member.domain.repository.TeamRoleRepository;
import doore.member.exception.MemberException;
import doore.team.domain.TeamRepository;
import doore.team.exception.TeamException;
import doore.team.exception.TeamExceptionType;
Expand All @@ -20,31 +14,18 @@
@RequiredArgsConstructor
public class MemberTeamCommandService {
private final MemberTeamRepository memberTeamRepository;
private final TeamRoleRepository teamRoleRepository;
private final TeamRepository teamRepository;

private final TeamRoleValidateAccessPermission teamRoleValidateAccessPermission;

public void deleteMemberTeam(final Long teamId, final Long deleteMemberId, final Long teamLeaderId) {
validateExistTeam(teamId);
validateTeamLeader(teamLeaderId, teamId);
validateTeamMember(deleteMemberId, teamId);
teamRoleValidateAccessPermission.validateExistTeamLeader(teamId, teamLeaderId);
teamRoleValidateAccessPermission.validateExistMemberTeam(teamId, deleteMemberId);
memberTeamRepository.deleteByTeamIdAndMemberId(teamId, deleteMemberId);
}

private void validateTeamMember(final Long deleteMemberId, final Long teamId) {
TeamRole teamRole = teamRoleRepository.findTeamRoleByTeamIdAndMemberId(teamId, deleteMemberId)
.orElseThrow(() -> new MemberException(NOT_FOUND_MEMBER_ROLE_IN_TEAM));
if (teamRole.getTeamRoleType().equals(TeamRoleType.ROLE_팀장)) {
throw new MemberException(UNAUTHORIZED);
}
}

private void validateExistTeam(final Long teamId) {
teamRepository.findById(teamId).orElseThrow(() -> new TeamException(TeamExceptionType.NOT_FOUND_TEAM));
}

private void validateTeamLeader(Long teamLeaderId, Long teamId) {
teamRoleRepository.findTeamRoleByTeamIdAndMemberId(teamId, teamLeaderId)
.orElseThrow(() -> new MemberException(NOT_FOUND_MEMBER_ROLE_IN_TEAM));
}

}
18 changes: 4 additions & 14 deletions src/main/java/doore/member/application/MemberTeamQueryService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package doore.member.application;

import static doore.member.domain.TeamRoleType.ROLE_팀원;
import static doore.member.domain.TeamRoleType.ROLE_팀장;
import static doore.member.exception.MemberExceptionType.NOT_FOUND_MEMBER;
import static doore.member.exception.MemberExceptionType.NOT_FOUND_MEMBER_ROLE_IN_TEAM;
import static doore.member.exception.MemberExceptionType.UNAUTHORIZED;

import doore.member.application.convenience.TeamRoleValidateAccessPermission;
import doore.member.application.dto.response.TeamMemberResponse;
import doore.member.domain.Member;
import doore.member.domain.MemberTeam;
import doore.member.domain.TeamRole;
import doore.member.domain.TeamRoleType;
import doore.member.domain.repository.MemberTeamRepository;
import doore.member.domain.repository.TeamRoleRepository;
Expand All @@ -29,9 +25,11 @@ public class MemberTeamQueryService {

private final MemberTeamRepository memberTeamRepository;
private final TeamRoleRepository teamRoleRepository;

private final TeamRoleValidateAccessPermission teamRoleValidateAccessPermission;

public List<TeamMemberResponse> findMemberTeams(final Long teamId, final String keyword, final Long memberId) {
validateExistTeamLeaderAndTeamMember(teamId, memberId);
teamRoleValidateAccessPermission.validateExistMemberTeam(teamId, memberId);
if (keyword == null || keyword.isBlank()) {
return findAllMemberOfTeam(teamId);
}
Expand Down Expand Up @@ -67,12 +65,4 @@ private Map<Member, TeamRoleType> getRoleOfMember(final Long teamId, final List<
.getTeamRoleType()
));
}

private void validateExistTeamLeaderAndTeamMember(final Long teamId, final Long memberId) {
final TeamRole teamRole = teamRoleRepository.findTeamRoleByTeamIdAndMemberId(teamId, memberId)
.orElseThrow(() -> new MemberException(NOT_FOUND_MEMBER_ROLE_IN_TEAM));
if (!(teamRole.getTeamRoleType().equals(ROLE_팀장) || teamRole.getTeamRoleType().equals(ROLE_팀원))){
throw new MemberException(UNAUTHORIZED);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package doore.member.application.convenience;

import static doore.member.domain.StudyRoleType.ROLE_스터디장;
import static doore.member.exception.MemberExceptionType.NOT_FOUND_MEMBER_ROLE_IN_STUDY;
import static doore.member.exception.MemberExceptionType.UNAUTHORIZED;

import doore.member.domain.StudyRole;
import doore.member.domain.repository.StudyRoleRepository;
import doore.member.exception.MemberException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class StudyRoleValidateAccessPermission {
private final StudyRoleRepository studyRoleRepository;

public void validateExistStudyLeader(final Long studyId, final Long memberId) {
final StudyRole studyRole = studyRoleRepository.findStudyRoleByStudyIdAndMemberId(studyId, memberId)
.orElseThrow(() -> new MemberException(NOT_FOUND_MEMBER_ROLE_IN_STUDY));
if (!studyRole.getStudyRoleType().equals(ROLE_스터디장)) {
throw new MemberException(UNAUTHORIZED);
}
}

public void validateExistParticipant(final Long studyId, final Long memberId) {
studyRoleRepository.findStudyRoleByStudyIdAndMemberId(studyId, memberId)
.orElseThrow(() -> new MemberException(UNAUTHORIZED));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package doore.member.application.convenience;

import static doore.member.domain.TeamRoleType.ROLE_팀장;
import static doore.member.exception.MemberExceptionType.NOT_FOUND_MEMBER_ROLE_IN_TEAM;
import static doore.member.exception.MemberExceptionType.UNAUTHORIZED;

import doore.member.domain.TeamRole;
import doore.member.domain.repository.TeamRoleRepository;
import doore.member.exception.MemberException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class TeamRoleValidateAccessPermission {
private final TeamRoleRepository teamRoleRepository;

public void validateExistTeamLeader(final Long teamId, final Long memberId) {
final TeamRole teamRole = teamRoleRepository.findTeamRoleByTeamIdAndMemberId(teamId, memberId)
.orElseThrow(() -> new MemberException(NOT_FOUND_MEMBER_ROLE_IN_TEAM));
if (!teamRole.getTeamRoleType().equals(ROLE_팀장)) {
throw new MemberException(UNAUTHORIZED);
}
}

public void validateExistMemberTeam(final Long teamId, final Long memberId) {
teamRoleRepository.findTeamRoleByTeamIdAndMemberId(teamId, memberId)
.orElseThrow(() -> new MemberException(UNAUTHORIZED));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package doore.study.application;

import static doore.member.domain.StudyRoleType.ROLE_스터디원;
import static doore.member.domain.StudyRoleType.ROLE_스터디장;
import static doore.member.exception.MemberExceptionType.NOT_FOUND_MEMBER_ROLE_IN_STUDY;
import static doore.member.exception.MemberExceptionType.UNAUTHORIZED;
import static doore.study.exception.CurriculumItemExceptionType.CANNOT_CREATE_CURRICULUM_ITEM;
import static doore.study.exception.CurriculumItemExceptionType.INVALID_ITEM_ORDER;
import static doore.study.exception.CurriculumItemExceptionType.NOT_FOUND_CURRICULUM_ITEM;
Expand All @@ -13,11 +9,9 @@
import doore.garden.domain.Garden;
import doore.garden.domain.GardenType;
import doore.garden.domain.repository.GardenRepository;
import doore.member.application.convenience.StudyRoleValidateAccessPermission;
import doore.member.domain.Participant;
import doore.member.domain.StudyRole;
import doore.member.domain.repository.ParticipantRepository;
import doore.member.domain.repository.StudyRoleRepository;
import doore.member.exception.MemberException;
import doore.study.application.dto.request.CurriculumItemManageDetailRequest;
import doore.study.application.dto.request.CurriculumItemManageRequest;
import doore.study.domain.CurriculumItem;
Expand Down Expand Up @@ -45,11 +39,12 @@ public class CurriculumItemCommandService {
private final ParticipantCurriculumItemRepository participantCurriculumItemRepository;
private final StudyRepository studyRepository;
private final ParticipantRepository participantRepository;
private final StudyRoleRepository studyRoleRepository;
private final GardenRepository gardenRepository;

private final StudyRoleValidateAccessPermission studyRoleValidateAccessPermission;

public void manageCurriculum(final CurriculumItemManageRequest request, final Long studyId, final Long memberId) {
validateExistStudyLeader(studyId, memberId);
studyRoleValidateAccessPermission.validateExistStudyLeader(studyId, memberId);
final List<CurriculumItemManageDetailRequest> curriculumItems = request.curriculumItems();
checkItemOrderDuplicate(curriculumItems);
checkItemOrderRange(curriculumItems);
Expand All @@ -63,20 +58,14 @@ public void manageCurriculum(final CurriculumItemManageRequest request, final Lo

public void checkCurriculum(final Long curriculumId, final Long participantId, final Long memberId) {
final Study study = studyRepository.findByCurriculumItemId(curriculumId);
validateExistStudyLeaderAndStudyMember(study.getId(),memberId);
final CurriculumItem curriculumItem = curriculumItemRepository.findById(curriculumId)
.orElseThrow(() -> new CurriculumItemException(NOT_FOUND_CURRICULUM_ITEM));
final Participant participant = participantRepository.findById(participantId)
.orElseThrow(() -> new StudyException(NOT_FOUND_PARTICIPANT));
final ParticipantCurriculumItem participantCurriculumItem = participantCurriculumItemRepository.findByCurriculumItemIdAndParticipantId(
curriculumItem.getId(), participant.getId()).orElseThrow();
studyRoleValidateAccessPermission.validateExistParticipant(study.getId(), memberId);
final CurriculumItem curriculumItem = getCurriculumItemOrThrow(curriculumId);
final Participant participant = getParticipantOrThrow(participantId);
final ParticipantCurriculumItem participantCurriculumItem = getParticipantCurriculumItemOrThrow(curriculumItem,
participant);

participantCurriculumItem.checkCompletion();
if (participantCurriculumItem.getIsChecked()) {
createGarden(participantCurriculumItem);
return;
}
deleteGarden(participantCurriculumItem);
handleGardenBasedOnCompletionStatus(participantCurriculumItem);
}

private void createGarden(final ParticipantCurriculumItem participantCurriculumItem) {
Expand All @@ -91,6 +80,14 @@ private void deleteGarden(final ParticipantCurriculumItem participantCurriculumI
gardenRepository.deleteByContributionIdAndType(contributionId, gardenType);
}

private void handleGardenBasedOnCompletionStatus(final ParticipantCurriculumItem participantCurriculumItem) {
if (participantCurriculumItem.getIsChecked()) {
createGarden(participantCurriculumItem);
return;
}
deleteGarden(participantCurriculumItem);
}

private void checkItemOrderDuplicate(final List<CurriculumItemManageDetailRequest> curriculumItems) {
final Set<Integer> uniqueItemOrders = new HashSet<>();

Expand All @@ -114,7 +111,7 @@ private void checkItemOrderRange(final List<CurriculumItemManageDetailRequest> c
}

private void createCurriculum(final Long studyId, final List<CurriculumItemManageDetailRequest> curriculumItems) {
if (curriculumItemRepository.findAll().size() >= 99) {
if (curriculumItemRepository.count() >= 99) {
throw new CurriculumItemException(CANNOT_CREATE_CURRICULUM_ITEM);
}
curriculumItems.stream()
Expand All @@ -128,30 +125,35 @@ private boolean isExistsCurriculumItem(final Long curriculumItemId) {

public void createCurriculumItemAndAssignToParticipants(final Long studyId,
final CurriculumItemManageDetailRequest curriculumItemRequest) {
final Study study = studyRepository.findById(studyId).orElseThrow(() -> new StudyException(NOT_FOUND_STUDY));
final Study study = getStudyOrThrow(studyId);
final List<Participant> participants = participantRepository.findAllByStudyId(studyId);
final CurriculumItem curriculumItems = createCurriculumItem(curriculumItemRequest, study);
final List<ParticipantCurriculumItem> participantCurriculumItems = createParticipantCurriculumItems(
curriculumItems, participants);
participantCurriculumItemRepository.saveAll(participantCurriculumItems);
}

final CurriculumItem createCurriculumItem = CurriculumItem.builder()
.name(curriculumItemRequest.name())
.itemOrder(curriculumItemRequest.itemOrder())
private CurriculumItem createCurriculumItem(final CurriculumItemManageDetailRequest request, final Study study) {
return curriculumItemRepository.save(CurriculumItem.builder()
.name(request.name())
.itemOrder(request.itemOrder())
.study(study)
.build();
curriculumItemRepository.save(createCurriculumItem);
.build());
}

final List<ParticipantCurriculumItem> participantCurriculumItems = participants.stream()
private List<ParticipantCurriculumItem> createParticipantCurriculumItems(final CurriculumItem curriculumItem,
final List<Participant> participants) {
return participants.stream()
.map(participant -> ParticipantCurriculumItem.builder()
.curriculumItem(createCurriculumItem)
.curriculumItem(curriculumItem)
.participantId(participant.getId())
.build())
.toList();
participantCurriculumItemRepository.saveAll(participantCurriculumItems);
}


private void updateCurriculum(final List<CurriculumItemManageDetailRequest> curriculumItems) {
for (final CurriculumItemManageDetailRequest requestItem : curriculumItems) {
final CurriculumItem existingItem = curriculumItemRepository.findById(requestItem.id())
.orElseThrow(() -> new CurriculumItemException(NOT_FOUND_CURRICULUM_ITEM));
final CurriculumItem existingItem = getCurriculumItemOrThrow(requestItem.id());

existingItem.updateIfNameDifferent(requestItem.name());
existingItem.updateIfItemOrderDifferent(requestItem.itemOrder());
Expand All @@ -175,19 +177,23 @@ private void sortCurriculum() {
curriculumItemRepository.saveAll(sortedCurriculum);
}

private void validateExistStudyLeader(final Long studyId, final Long memberId) {
final StudyRole studyRole = studyRoleRepository.findStudyRoleByStudyIdAndMemberId(studyId, memberId)
.orElseThrow(() -> new MemberException(NOT_FOUND_MEMBER_ROLE_IN_STUDY));
if (!studyRole.getStudyRoleType().equals(ROLE_스터디장)) {
throw new MemberException(UNAUTHORIZED);
}
private Study getStudyOrThrow(final Long studyId) {
return studyRepository.findById(studyId).orElseThrow(() -> new StudyException(NOT_FOUND_STUDY));
}

private void validateExistStudyLeaderAndStudyMember(final Long studyId, final Long memberId) {
final StudyRole studyRole = studyRoleRepository.findStudyRoleByStudyIdAndMemberId(studyId, memberId)
.orElseThrow(() -> new MemberException(NOT_FOUND_MEMBER_ROLE_IN_STUDY));
if (!(studyRole.getStudyRoleType().equals(ROLE_스터디장) || studyRole.getStudyRoleType().equals(ROLE_스터디원))) {
throw new MemberException(UNAUTHORIZED);
}
private CurriculumItem getCurriculumItemOrThrow(final Long curriculumId) {
return curriculumItemRepository.findById(curriculumId)
.orElseThrow(() -> new CurriculumItemException(NOT_FOUND_CURRICULUM_ITEM));
}

private Participant getParticipantOrThrow(final Long participantId) {
return participantRepository.findById(participantId)
.orElseThrow(() -> new StudyException(NOT_FOUND_PARTICIPANT));
}

private ParticipantCurriculumItem getParticipantCurriculumItemOrThrow(final CurriculumItem curriculumItem,
final Participant participant) {
return participantCurriculumItemRepository.findByCurriculumItemIdAndParticipantId(
curriculumItem.getId(), participant.getId()).orElseThrow();
}
}
Loading

0 comments on commit 0004826

Please sign in to comment.