From 1c58e412490a85f784e247019071d94a77b0be5f Mon Sep 17 00:00:00 2001 From: rladmstn Date: Fri, 22 Nov 2024 23:33:01 +0900 Subject: [PATCH 1/4] =?UTF-8?q?fix=20:=20=EA=B7=B8=EB=A3=B9=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EC=8B=9C,=20=EA=B7=B8=EB=A3=B9=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20=EC=A0=95=EB=B3=B4=20=EB=AA=A8=EB=91=90=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../studygroup/service/StudyGroupService.java | 30 +++++++++++++++---- .../repository/NoticeCommentRepository.java | 5 ++++ .../notice/repository/NoticeRepository.java | 5 ++++ .../problem/repository/ProblemRepository.java | 5 ++++ .../repository/SolutionCommentRepository.java | 6 ++++ .../repository/SolutionRepository.java | 6 ++++ 6 files changed, 52 insertions(+), 5 deletions(-) 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..5ba1ab98 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); + noticeRepository.deleteAllByStudyGroup(group); + noticeCommentRepository.deleteAllByStudyGroup(group); + problemRepository.deleteAllByStudyGroup(group); + solutionRepository.deleteAllByStudyGroup(group); + solutionCommentRepository.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"); 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..df7e16d1 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/problem/repository/ProblemRepository.java b/src/main/java/com/gamzabat/algohub/feature/problem/repository/ProblemRepository.java index 2de56fad..0243879a 100644 --- a/src/main/java/com/gamzabat/algohub/feature/problem/repository/ProblemRepository.java +++ b/src/main/java/com/gamzabat/algohub/feature/problem/repository/ProblemRepository.java @@ -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; @@ -27,4 +28,8 @@ public interface ProblemRepository 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 Date: Sat, 23 Nov 2024 15:50:21 +0900 Subject: [PATCH 2/4] =?UTF-8?q?fix=20:=20=EC=82=AD=EC=A0=9C=20=EC=BF=BC?= =?UTF-8?q?=EB=A6=AC=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20delete=20=EC=BF=BC?= =?UTF-8?q?=EB=A6=AC=20=EC=88=9C=EC=84=9C=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../feature/group/studygroup/service/StudyGroupService.java | 6 +++--- .../feature/notice/repository/NoticeCommentRepository.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) 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 5ba1ab98..d732dcf3 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 @@ -177,11 +177,11 @@ private void deleteAllAboutGroup(StudyGroup group) { rankingRepository.deleteAllByStudyGroup(group); notificationSettingRepository.deleteAllByStudyGroup(group); notificationRepository.deleteAllByStudyGroup(group); - noticeRepository.deleteAllByStudyGroup(group); noticeCommentRepository.deleteAllByStudyGroup(group); - problemRepository.deleteAllByStudyGroup(group); - solutionRepository.deleteAllByStudyGroup(group); + noticeRepository.deleteAllByStudyGroup(group); solutionCommentRepository.deleteAllByStudyGroup(group); + solutionRepository.deleteAllByStudyGroup(group); + problemRepository.deleteAllByStudyGroup(group); groupMemberRepository.deleteAllByStudyGroup(group); groupRepository.delete(group); } 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 df7e16d1..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 @@ -19,6 +19,6 @@ public interface NoticeCommentRepository extends JpaRepository Date: Sat, 23 Nov 2024 16:32:23 +0900 Subject: [PATCH 3/4] =?UTF-8?q?fix=20:=20=EB=A9=A4=EB=B2=84=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EC=8B=9C=20=EC=95=8C=EB=A6=BC=20=EC=82=AD=EC=A0=9C?= =?UTF-8?q?=20=EC=BF=BC=EB=A6=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../feature/group/studygroup/service/StudyGroupService.java | 2 +- .../notification/repository/NotificationRepository.java | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) 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 d732dcf3..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 @@ -241,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/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 Date: Sat, 23 Nov 2024 16:59:46 +0900 Subject: [PATCH 4/4] =?UTF-8?q?test=20:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/StudyGroupServiceTest.java | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/test/java/com/gamzabat/algohub/service/StudyGroupServiceTest.java b/src/test/java/com/gamzabat/algohub/service/StudyGroupServiceTest.java index b92eb8b8..ca5aa325 100644 --- a/src/test/java/com/gamzabat/algohub/service/StudyGroupServiceTest.java +++ b/src/test/java/com/gamzabat/algohub/service/StudyGroupServiceTest.java @@ -48,6 +48,8 @@ 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; @@ -55,6 +57,7 @@ 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; @@ -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 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