Skip to content

Commit

Permalink
fix: 삭제 이벤트 연관 객체 ID를 받아서 처리하도록 변경 (#760)
Browse files Browse the repository at this point in the history
fix: 참조 ID를 받아서 처리하도록 변경
  • Loading branch information
uwoobeat authored Sep 4, 2024
1 parent 83cc9a9 commit f7262d7
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,14 @@ public List<StudyAnnouncementResponse> getStudyAnnouncements(Long studyId) {
/**
* 이벤트 핸들러에서 사용되므로, `@Transactional` 을 사용하지 않습니다.
*/
public void deleteAttendanceByStudyHistory(Long studyHistoryId) {
StudyHistory studyHistory = studyHistoryRepository
.findById(studyHistoryId)
.orElseThrow(() -> new CustomException(STUDY_HISTORY_NOT_FOUND));

attendanceRepository.deleteByStudyHistory(studyHistory);
public void deleteAttendance(Long studyId, Long memberId) {
attendanceRepository.deleteByStudyIdAndMemberId(studyId, memberId);
}

/**
* 이벤트 핸들러에서 사용되므로, `@Transactional` 을 사용하지 않습니다.
*/
public void deleteAssignmentHistoryByStudyHistory(Long studyHistoryId) {
StudyHistory studyHistory = studyHistoryRepository
.findById(studyHistoryId)
.orElseThrow(() -> new CustomException(STUDY_HISTORY_NOT_FOUND));

assignmentHistoryRepository.deleteByStudyHistory(studyHistory);
public void deleteAssignmentHistory(Long studyId, Long memberId) {
assignmentHistoryRepository.deleteByStudyIdAndMemberId(studyId, memberId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class StudyEventHandler {

@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
public void handleStudyApplyCanceledEvent(StudyApplyCanceledEvent event) {
log.info("[StudyEventHandler] 스터디 수강신청 취소 이벤트 수신: studyHistoryId={}", event.studyHistoryId());
commonStudyService.deleteAttendanceByStudyHistory(event.studyHistoryId());
commonStudyService.deleteAssignmentHistoryByStudyHistory(event.studyHistoryId());
log.info("[StudyEventHandler] 스터디 수강신청 취소 이벤트 수신: studyId={}, memberId={}", event.studyId(), event.memberId());
commonStudyService.deleteAttendance(event.studyId(), event.memberId());
commonStudyService.deleteAssignmentHistory(event.studyId(), event.memberId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.gdschongik.gdsc.domain.member.domain.Member;
import com.gdschongik.gdsc.domain.study.domain.AssignmentHistory;
import com.gdschongik.gdsc.domain.study.domain.Study;
import com.gdschongik.gdsc.domain.study.domain.StudyHistory;
import java.util.List;

public interface AssignmentHistoryCustomRepository {
Expand All @@ -12,5 +11,5 @@ public interface AssignmentHistoryCustomRepository {

List<AssignmentHistory> findAssignmentHistoriesByStudentAndStudyId(Member member, Long studyId);

void deleteByStudyHistory(StudyHistory studyHistory);
void deleteByStudyIdAndMemberId(Long studyId, Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.gdschongik.gdsc.domain.member.domain.Member;
import com.gdschongik.gdsc.domain.study.domain.AssignmentHistory;
import com.gdschongik.gdsc.domain.study.domain.Study;
import com.gdschongik.gdsc.domain.study.domain.StudyHistory;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
Expand Down Expand Up @@ -56,10 +55,14 @@ private BooleanExpression eqStudyId(Long studyId) {
}

@Override
public void deleteByStudyHistory(StudyHistory studyHistory) {
public void deleteByStudyIdAndMemberId(Long studyId, Long memberId) {
queryFactory
.delete(assignmentHistory)
.where(eqMember(studyHistory.getStudent()).and(eqStudy(studyHistory.getStudy())))
.where(eqMemberId(memberId), eqStudyId(studyId))
.execute();
}

private BooleanExpression eqMemberId(Long memberId) {
return memberId != null ? assignmentHistory.member.id.eq(memberId) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import com.gdschongik.gdsc.domain.member.domain.Member;
import com.gdschongik.gdsc.domain.study.domain.Attendance;
import com.gdschongik.gdsc.domain.study.domain.StudyHistory;
import java.util.List;

public interface AttendanceCustomRepository {
List<Attendance> findByMemberAndStudyId(Member member, Long studyId);

void deleteByStudyHistory(StudyHistory studyHistory);
void deleteByStudyIdAndMemberId(Long studyId, Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import com.gdschongik.gdsc.domain.member.domain.Member;
import com.gdschongik.gdsc.domain.study.domain.Attendance;
import com.gdschongik.gdsc.domain.study.domain.StudyHistory;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
Expand Down Expand Up @@ -36,13 +35,10 @@ private BooleanExpression eqStudyId(Long studyId) {
}

@Override
public void deleteByStudyHistory(StudyHistory studyHistory) {
public void deleteByStudyIdAndMemberId(Long studyId, Long memberId) {
queryFactory
.delete(attendance)
.where(attendance
.student
.eq(studyHistory.getStudent())
.and(attendance.studyDetail.study.eq(studyHistory.getStudy())))
.where(eqMemberId(memberId), eqStudyId(studyId))
.execute();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.gdschongik.gdsc.domain.study.domain;

public record StudyApplyCanceledEvent(Long studyHistoryId) {}
public record StudyApplyCanceledEvent(Long studyId, Long memberId) {}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static StudyHistory create(Member student, Study study) {

@PreRemove
private void preRemove() {
registerEvent(new StudyApplyCanceledEvent(this.id));
registerEvent(new StudyApplyCanceledEvent(this.study.getId(), this.student.getId()));
}

/**
Expand Down

0 comments on commit f7262d7

Please sign in to comment.