Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/BDD-CLUB/01-doo-re-back
Browse files Browse the repository at this point in the history
…into develop
  • Loading branch information
lcqff committed Jul 25, 2024
2 parents b058d04 + 0004826 commit 0dfa1db
Show file tree
Hide file tree
Showing 17 changed files with 283 additions and 214 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));
}
}
2 changes: 2 additions & 0 deletions src/main/java/doore/member/domain/Participant.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@SQLDelete(sql = "UPDATE participant SET is_deleted = true where id = ?")
public class Participant extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import doore.member.domain.TeamRole;
import doore.member.domain.TeamRoleType;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -12,4 +13,5 @@ public interface TeamRoleRepository extends JpaRepository<TeamRole, Long> {
Optional<TeamRole> findTeamRoleByMemberId(Long memberId);
@Query("SELECT tr.memberId FROM TeamRole tr WHERE tr.teamId = :teamId AND tr.teamRoleType = 'ROLE_팀장'")
Long findLeaderIdByTeamId(Long teamId);
List<TeamRole> findAllByTeamId(final Long teamId);
}
8 changes: 4 additions & 4 deletions src/main/java/doore/study/api/ParticipantController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package doore.study.api;

import doore.member.domain.Member;
import doore.member.domain.Participant;
import doore.resolver.LoginMember;
import doore.study.application.ParticipantCommandService;
import doore.study.application.ParticipantQueryService;
import doore.study.application.dto.response.ParticipantResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -43,9 +43,9 @@ public ResponseEntity<Void> withdrawParticipant(@PathVariable final Long studyId
}

@GetMapping("/studies/{studyId}/members") // 스터디장 & 스터디원
public ResponseEntity<List<Participant>> getParticipant(@PathVariable final Long studyId,
@LoginMember final Member member) {
final List<Participant> participants = participantQueryService.findAllParticipants(studyId, member.getId());
public ResponseEntity<List<ParticipantResponse>> getParticipant(@PathVariable final Long studyId,
@LoginMember final Member member) {
final List<ParticipantResponse> participants = participantQueryService.findAllParticipants(studyId, member.getId());
return ResponseEntity.ok(participants);
}
}
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 0dfa1db

Please sign in to comment.