Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : 방장 혼자 있는 그룹 탈퇴 시 권한 이전 오류 해결 및 삭제 로직 추가 #200

Merged
merged 4 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@
import com.gamzabat.algohub.feature.group.studygroup.repository.GroupMemberRepository;
import com.gamzabat.algohub.feature.group.studygroup.repository.StudyGroupRepository;
import com.gamzabat.algohub.feature.image.service.ImageService;
import com.gamzabat.algohub.feature.notice.repository.NoticeCommentRepository;
import com.gamzabat.algohub.feature.notice.repository.NoticeRepository;
import com.gamzabat.algohub.feature.notification.domain.NotificationSetting;
import com.gamzabat.algohub.feature.notification.enums.NotificationCategory;
import com.gamzabat.algohub.feature.notification.repository.NotificationRepository;
import com.gamzabat.algohub.feature.notification.repository.NotificationSettingRepository;
import com.gamzabat.algohub.feature.notification.service.NotificationService;
import com.gamzabat.algohub.feature.problem.domain.Problem;
import com.gamzabat.algohub.feature.problem.repository.ProblemRepository;
import com.gamzabat.algohub.feature.solution.repository.SolutionCommentRepository;
import com.gamzabat.algohub.feature.solution.repository.SolutionRepository;
import com.gamzabat.algohub.feature.user.domain.User;
import com.gamzabat.algohub.feature.user.repository.UserRepository;
Expand All @@ -72,6 +75,9 @@ public class StudyGroupService {
private final StudyGroupRepository studyGroupRepository;
private final BookmarkedStudyGroupRepository bookmarkedStudyGroupRepository;
private final RankingRepository rankingRepository;
private final NoticeRepository noticeRepository;
private final NoticeCommentRepository noticeCommentRepository;
private final SolutionCommentRepository solutionCommentRepository;
private final NotificationRepository notificationRepository;

private final ObjectProvider<StudyGroupService> studyGroupServiceProvider;
Expand Down Expand Up @@ -161,14 +167,23 @@ public void deleteGroup(User user, Long groupId) {
throw new GroupMemberValidationException(HttpStatus.FORBIDDEN.value(), "스터디 그룹 삭제는 방장만 가능합니다.");
}

deleteAllAboutGroup(group);

log.info("success to delete study group");
}

private void deleteAllAboutGroup(StudyGroup group) {
bookmarkedStudyGroupRepository.deleteAllByStudyGroup(group);
rankingRepository.deleteAllByStudyGroup(group);
notificationSettingRepository.deleteAllByStudyGroup(group);
notificationRepository.deleteAllByStudyGroup(group);
noticeCommentRepository.deleteAllByStudyGroup(group);
noticeRepository.deleteAllByStudyGroup(group);
solutionCommentRepository.deleteAllByStudyGroup(group);
solutionRepository.deleteAllByStudyGroup(group);
problemRepository.deleteAllByStudyGroup(group);
groupMemberRepository.deleteAllByStudyGroup(group);
groupRepository.delete(group);

log.info("success to delete study group");
}

@Transactional
Expand All @@ -183,11 +198,16 @@ public void exitGroup(User user, Long groupId) {
studyGroupServiceProvider.getObject().deleteMemberFromStudyGroup(user, groupMember, studyGroup);

if (RoleOfGroupMember.isOwner(groupMember)) {
GroupMember member = groupMemberRepository.findAllByStudyGroup(studyGroup)
List<GroupMember> members = groupMemberRepository.findAllByStudyGroup(studyGroup)
.stream()
.sorted(Comparator.comparing(GroupMember::getRole).thenComparing(GroupMember::getJoinDate))
.toList().getFirst();
member.updateRole(RoleOfGroupMember.OWNER);
.toList();

if (members.isEmpty()) {
deleteAllAboutGroup(studyGroup);
} else {
members.getFirst().updateRole(RoleOfGroupMember.OWNER);
}
}

log.info("success to exit study group");
Expand Down Expand Up @@ -221,7 +241,7 @@ public void deleteMemberFromStudyGroup(User user, GroupMember groupMember, Study
.ifPresent(bookmarkedStudyGroupRepository::delete);
rankingRepository.deleteByMember(groupMember);
notificationSettingRepository.deleteByMember(groupMember);
notificationRepository.deleteAllByStudyGroup(groupMember.getStudyGroup());
notificationRepository.deleteAllByUserAndStudyGroup(user, studyGroup);
groupMemberRepository.delete(groupMember);
log.info("success to delete group member");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.gamzabat.algohub.feature.group.studygroup.domain.StudyGroup;
import com.gamzabat.algohub.feature.notice.domain.Notice;
import com.gamzabat.algohub.feature.notice.domain.NoticeComment;

Expand All @@ -16,4 +17,8 @@ public interface NoticeCommentRepository extends JpaRepository<NoticeComment, Lo
@Modifying
@Query("delete from NoticeComment c where c.notice = :notice")
void deleteAllCommentByNotice(Notice notice);

@Modifying
@Query("DELETE FROM NoticeComment nc WHERE nc.notice.studyGroup = :group")
void deleteAllByStudyGroup(StudyGroup group);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.gamzabat.algohub.feature.group.studygroup.domain.StudyGroup;
import com.gamzabat.algohub.feature.notice.domain.Notice;

public interface NoticeRepository extends JpaRepository<Notice, Long> {
Page<Notice> findAllByStudyGroup(StudyGroup studyGroup, Pageable pageable);

@Modifying
@Query("delete from Notice n where n.studyGroup = :studyGroup")
void deleteAllByStudyGroup(StudyGroup studyGroup);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ public interface NotificationRepository extends JpaRepository<Notification, Long
@Modifying
@Query("delete from Notification n where n.studyGroup = :studyGroup")
void deleteAllByStudyGroup(StudyGroup studyGroup);

@Modifying
@Query("DELETE FROM Notification n WHERE n.user = :user AND n.studyGroup = :studyGroup")
void deleteAllByUserAndStudyGroup(User user, StudyGroup studyGroup);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

Expand All @@ -27,4 +28,8 @@ public interface ProblemRepository extends JpaRepository<Problem, Long> {
Long countProblemsByGroupId(@Param("groupId") Long groupId);

List<Problem> findAllByEndDate(LocalDate endDate);

@Modifying
@Query("delete from Problem p where p.studyGroup = :studyGroup")
void deleteAllByStudyGroup(StudyGroup studyGroup);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.gamzabat.algohub.feature.group.studygroup.domain.StudyGroup;
import com.gamzabat.algohub.feature.solution.domain.Solution;
import com.gamzabat.algohub.feature.solution.domain.SolutionComment;

Expand All @@ -14,4 +16,8 @@ public interface SolutionCommentRepository extends JpaRepository<SolutionComment

@Query("SELECT COUNT(c) FROM SolutionComment c WHERE c.solution.id = :solutionId")
long countCommentsBySolutionId(@Param("solutionId") Long solutionId);

@Modifying
@Query("DELETE FROM SolutionComment sc WHERE sc.solution.problem.studyGroup = :studyGroup")
void deleteAllByStudyGroup(StudyGroup studyGroup);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.gamzabat.algohub.feature.solution.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.gamzabat.algohub.feature.group.studygroup.domain.StudyGroup;
import com.gamzabat.algohub.feature.problem.domain.Problem;
import com.gamzabat.algohub.feature.solution.domain.Solution;
import com.gamzabat.algohub.feature.solution.repository.querydsl.CustomSolutionRepository;
Expand All @@ -26,4 +28,8 @@ Integer countDistinctUsersWithCorrectSolutionsByProblemId(@Param("problemId") Lo
"AND s.result = :correct")
Long countDistinctCorrectSolutionsByUserAndGroup(@Param("user") User user, @Param("groupId") Long groupId,
@Param("correct") String correct);

@Modifying
@Query("DELETE FROM Solution s WHERE s.problem.studyGroup = :studyGroup")
void deleteAllByStudyGroup(StudyGroup studyGroup);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@
import com.gamzabat.algohub.feature.group.studygroup.repository.StudyGroupRepository;
import com.gamzabat.algohub.feature.group.studygroup.service.StudyGroupService;
import com.gamzabat.algohub.feature.image.service.ImageService;
import com.gamzabat.algohub.feature.notice.repository.NoticeCommentRepository;
import com.gamzabat.algohub.feature.notice.repository.NoticeRepository;
import com.gamzabat.algohub.feature.notification.domain.NotificationSetting;
import com.gamzabat.algohub.feature.notification.repository.NotificationRepository;
import com.gamzabat.algohub.feature.notification.repository.NotificationSettingRepository;
import com.gamzabat.algohub.feature.notification.service.NotificationService;
import com.gamzabat.algohub.feature.problem.domain.Problem;
import com.gamzabat.algohub.feature.problem.repository.ProblemRepository;
import com.gamzabat.algohub.feature.solution.domain.Solution;
import com.gamzabat.algohub.feature.solution.repository.SolutionCommentRepository;
import com.gamzabat.algohub.feature.solution.repository.SolutionRepository;
import com.gamzabat.algohub.feature.user.domain.User;
import com.gamzabat.algohub.feature.user.repository.UserRepository;
Expand Down Expand Up @@ -82,6 +85,12 @@ class StudyGroupServiceTest {
@Mock
private NotificationSettingRepository notificationSettingRepository;
@Mock
private NoticeRepository noticeRepository;
@Mock
private NoticeCommentRepository noticeCommentRepository;
@Mock
private SolutionCommentRepository solutionCommentRepository;
@Mock
private RankingRepository rankingRepository;
@Mock
private ObjectProvider<StudyGroupService> studyGroupServiceObjectProvider;
Expand Down Expand Up @@ -299,18 +308,18 @@ void deleteGroupFailed_2() {
void exitGroup() {
// given
when(studyGroupRepository.findById(10L)).thenReturn(Optional.of(group));
when(groupMemberRepository.findByUserAndStudyGroup(user, group)).thenReturn(Optional.ofNullable(groupMember1));
when(groupMemberRepository.findAllByStudyGroup(group)).thenReturn(
List.of(groupMember2, groupMember3));
when(groupMemberRepository.findByUserAndStudyGroup(user2, group)).thenReturn(Optional.ofNullable(groupMember2));
// when(groupMemberRepository.findAllByStudyGroup(group)).thenReturn(
// List.of(groupMember2, groupMember3));
when(studyGroupServiceObjectProvider.getObject()).thenReturn(studyGroupService);
// when
studyGroupService.exitGroup(user, 10L);
studyGroupService.exitGroup(user2, 10L);
// then
verify(rankingRepository, times(1)).deleteByMember(groupMember1);
verify(notificationSettingRepository, times(1)).deleteByMember(groupMember1);
verify(groupMemberRepository, times(1)).delete(groupMember1);
verify(notificationRepository, times(1)).deleteAllByStudyGroup(group);
assertThat(groupMember3.getRole()).isEqualTo(RoleOfGroupMember.OWNER);
verify(rankingRepository, times(1)).deleteByMember(groupMember2);
verify(notificationSettingRepository, times(1)).deleteByMember(groupMember2);
verify(groupMemberRepository, times(1)).delete(groupMember2);
verify(notificationRepository, times(1)).deleteAllByUserAndStudyGroup(user2, group);
// assertThat(groupMember3.getRole()).isEqualTo(RoleOfGroupMember.OWNER);
}

@Test
Expand Down
Loading