From 8fe7d7a6baed2ef7d067b2acf1d53f2954901e24 Mon Sep 17 00:00:00 2001 From: rladmstn Date: Mon, 18 Nov 2024 22:26:00 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=EA=B7=B8=EB=A3=B9=20=ED=83=88?= =?UTF-8?q?=ED=87=B4=20API=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EC=95=8C?= =?UTF-8?q?=EB=A6=BC=20=EC=82=AD=EC=A0=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../studygroup/service/StudyGroupService.java | 29 +++++++++++-------- .../repository/NotificationRepository.java | 7 +++++ 2 files changed, 24 insertions(+), 12 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 afbd6910..f5eb5d03 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 @@ -47,6 +47,7 @@ import com.gamzabat.algohub.feature.image.service.ImageService; 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; @@ -71,6 +72,7 @@ public class StudyGroupService { private final StudyGroupRepository studyGroupRepository; private final BookmarkedStudyGroupRepository bookmarkedStudyGroupRepository; private final RankingRepository rankingRepository; + private final NotificationRepository notificationRepository; private final ObjectProvider studyGroupServiceProvider; private final NotificationSettingRepository notificationSettingRepository; @@ -162,6 +164,7 @@ public void deleteGroup(User user, Long groupId) { bookmarkedStudyGroupRepository.deleteAllByStudyGroup(group); rankingRepository.deleteAllByStudyGroup(group); notificationSettingRepository.deleteAllByStudyGroup(group); + notificationRepository.deleteAllByStudyGroup(group); groupMemberRepository.deleteAllByStudyGroup(group); groupRepository.delete(group); @@ -171,22 +174,23 @@ public void deleteGroup(User user, Long groupId) { @Transactional public void exitGroup(User user, Long groupId) { StudyGroup studyGroup = groupRepository.findById(groupId) - .orElseThrow(() -> new StudyGroupValidationException(HttpStatus.NOT_FOUND.value(), "존재하지 않는 그룹 입니다.")); + .orElseThrow(() -> new CannotFoundGroupException("존재하지 않는 그룹입니다.")); GroupMember groupMember = groupMemberRepository.findByUserAndStudyGroup(user, studyGroup) .orElseThrow( - () -> new GroupMemberValidationException(HttpStatus.BAD_REQUEST.value(), "이미 참여하지 않은 그룹 입니다.")); - - if (RoleOfGroupMember.isOwner(groupMember)) { // owner - bookmarkedStudyGroupRepository.deleteAll(bookmarkedStudyGroupRepository.findAllByStudyGroup(studyGroup)); - rankingRepository.deleteAll(rankingRepository.findAllByStudyGroup(studyGroup)); - notificationSettingRepository.deleteAll(notificationSettingRepository.findAllByStudyGroup(studyGroup)); - groupMemberRepository.deleteAll(groupMemberRepository.findAllByStudyGroup(studyGroup)); - groupRepository.delete(studyGroup); - } else { // member - studyGroupServiceProvider.getObject().deleteMemberFromStudyGroup(user, groupMember, studyGroup); + () -> new GroupMemberValidationException(HttpStatus.BAD_REQUEST.value(), "참여하지 않은 그룹입니다.")); + + studyGroupServiceProvider.getObject().deleteMemberFromStudyGroup(user, groupMember, studyGroup); + + if (RoleOfGroupMember.isOwner(groupMember)) { + GroupMember member = groupMemberRepository.findAllByStudyGroup(studyGroup) + .stream() + .sorted(Comparator.comparing(GroupMember::getRole).thenComparing(GroupMember::getJoinDate)) + .toList().getFirst(); + member.updateRole(RoleOfGroupMember.OWNER); } - log.info("success to delete(exit) study group"); + + log.info("success to exit study group"); } @Transactional @@ -217,6 +221,7 @@ public void deleteMemberFromStudyGroup(User user, GroupMember groupMember, Study .ifPresent(bookmarkedStudyGroupRepository::delete); rankingRepository.deleteByMember(groupMember); notificationSettingRepository.deleteByMember(groupMember); + notificationRepository.deleteAllByStudyGroup(groupMember.getStudyGroup()); 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 78221881..5728d654 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 @@ -3,7 +3,10 @@ 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 com.gamzabat.algohub.feature.group.studygroup.domain.StudyGroup; import com.gamzabat.algohub.feature.notification.domain.Notification; import com.gamzabat.algohub.feature.user.domain.User; @@ -11,4 +14,8 @@ public interface NotificationRepository extends JpaRepository findAllByUser(User user); List findAllByUserAndIsRead(User user, boolean isRead); + + @Modifying + @Query("delete from Notification n where n.studyGroup = :studyGroup") + void deleteAllByStudyGroup(StudyGroup studyGroup); }