diff --git a/src/main/java/com/gamzabat/algohub/feature/group/studygroup/service/StudyGroupService.java b/src/main/java/com/gamzabat/algohub/feature/group/studygroup/service/StudyGroupService.java index f5eb5d03..f05fe303 100644 --- a/src/main/java/com/gamzabat/algohub/feature/group/studygroup/service/StudyGroupService.java +++ b/src/main/java/com/gamzabat/algohub/feature/group/studygroup/service/StudyGroupService.java @@ -45,6 +45,8 @@ 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; @@ -52,6 +54,7 @@ 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; @@ -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 studyGroupServiceProvider; @@ -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 @@ -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 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"); @@ -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"); } diff --git a/src/main/java/com/gamzabat/algohub/feature/notice/repository/NoticeCommentRepository.java b/src/main/java/com/gamzabat/algohub/feature/notice/repository/NoticeCommentRepository.java index c5d3c6c0..2359da21 100644 --- a/src/main/java/com/gamzabat/algohub/feature/notice/repository/NoticeCommentRepository.java +++ b/src/main/java/com/gamzabat/algohub/feature/notice/repository/NoticeCommentRepository.java @@ -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; @@ -16,4 +17,8 @@ public interface NoticeCommentRepository extends JpaRepository { Page findAllByStudyGroup(StudyGroup studyGroup, Pageable pageable); + @Modifying + @Query("delete from Notice n where n.studyGroup = :studyGroup") + void deleteAllByStudyGroup(StudyGroup studyGroup); } diff --git a/src/main/java/com/gamzabat/algohub/feature/notification/repository/NotificationRepository.java b/src/main/java/com/gamzabat/algohub/feature/notification/repository/NotificationRepository.java index 5728d654..fa37f289 100644 --- a/src/main/java/com/gamzabat/algohub/feature/notification/repository/NotificationRepository.java +++ b/src/main/java/com/gamzabat/algohub/feature/notification/repository/NotificationRepository.java @@ -18,4 +18,8 @@ public interface NotificationRepository extends JpaRepository { Long countProblemsByGroupId(@Param("groupId") Long groupId); List findAllByEndDate(LocalDate endDate); + + @Modifying + @Query("delete from Problem p where p.studyGroup = :studyGroup") + void deleteAllByStudyGroup(StudyGroup studyGroup); } diff --git a/src/main/java/com/gamzabat/algohub/feature/solution/repository/SolutionCommentRepository.java b/src/main/java/com/gamzabat/algohub/feature/solution/repository/SolutionCommentRepository.java index 9c107944..1f38e158 100644 --- a/src/main/java/com/gamzabat/algohub/feature/solution/repository/SolutionCommentRepository.java +++ b/src/main/java/com/gamzabat/algohub/feature/solution/repository/SolutionCommentRepository.java @@ -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; @@ -14,4 +16,8 @@ public interface SolutionCommentRepository extends JpaRepository studyGroupServiceObjectProvider; @@ -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