Skip to content

Commit

Permalink
feat : 그룹 탈퇴 API 수정 및 알림 삭제 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
rladmstn committed Nov 18, 2024
1 parent cf87de2 commit 8fe7d7a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<StudyGroupService> studyGroupServiceProvider;
private final NotificationSettingRepository notificationSettingRepository;
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
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;

public interface NotificationRepository extends JpaRepository<Notification, Long> {
List<Notification> findAllByUser(User user);

List<Notification> findAllByUserAndIsRead(User user, boolean isRead);

@Modifying
@Query("delete from Notification n where n.studyGroup = :studyGroup")
void deleteAllByStudyGroup(StudyGroup studyGroup);
}

0 comments on commit 8fe7d7a

Please sign in to comment.