From 999b8ed4374ef878193ac786b9b15cbf5f2d0232 Mon Sep 17 00:00:00 2001 From: Hunor Tot-Bagi Date: Fri, 20 Sep 2024 22:04:55 +0300 Subject: [PATCH] Fix: Delete `Subject` (#113) * fix: path variable typo * feat: add FetchType.EAGER * feat: remove all students and teachers before deleting --- .../com/electric_diary/controllers/SubjectController.java | 4 ++-- .../java/com/electric_diary/entities/SubjectEntity.java | 7 ++++--- .../electric_diary/services/impl/SubjectServiceImpl.java | 7 +++++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/electric_diary/controllers/SubjectController.java b/src/main/java/com/electric_diary/controllers/SubjectController.java index 5d4ef8c..6b6c222 100644 --- a/src/main/java/com/electric_diary/controllers/SubjectController.java +++ b/src/main/java/com/electric_diary/controllers/SubjectController.java @@ -45,8 +45,8 @@ public ResponseEntity updateSubject(@PathVariable Integer subject } @DeleteMapping("/{subjectId}") - public ResponseEntity deleteSubject(@PathVariable Integer subjectid) { - return new ResponseEntity<>(subjectService.deleteSubject(subjectid), HttpStatus.OK); + public ResponseEntity deleteSubject(@PathVariable Integer subjectId) { + return new ResponseEntity<>(subjectService.deleteSubject(subjectId), HttpStatus.OK); } @PutMapping("/{subjectId}/students/{studentId}") diff --git a/src/main/java/com/electric_diary/entities/SubjectEntity.java b/src/main/java/com/electric_diary/entities/SubjectEntity.java index 5646ded..6701d77 100644 --- a/src/main/java/com/electric_diary/entities/SubjectEntity.java +++ b/src/main/java/com/electric_diary/entities/SubjectEntity.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; @@ -35,14 +36,14 @@ public class SubjectEntity { @NotNull @JsonIgnore - @OneToMany(mappedBy = "subject") + @OneToMany(mappedBy = "subject", fetch = FetchType.EAGER) private Set grades = new HashSet(); - @ManyToMany + @ManyToMany(fetch = FetchType.EAGER) @JoinTable(name = "student_enrolled", joinColumns = @JoinColumn(name = "subject_id"), inverseJoinColumns = @JoinColumn(name = "student_id")) private Set enrolledStudents = new HashSet<>(); - @ManyToMany(mappedBy = "subjects") + @ManyToMany(mappedBy = "subjects", fetch = FetchType.EAGER) private Set teachers = new HashSet<>(); public void enrolStudents(StudentEntity student) { diff --git a/src/main/java/com/electric_diary/services/impl/SubjectServiceImpl.java b/src/main/java/com/electric_diary/services/impl/SubjectServiceImpl.java index ff91e9c..8f6d078 100644 --- a/src/main/java/com/electric_diary/services/impl/SubjectServiceImpl.java +++ b/src/main/java/com/electric_diary/services/impl/SubjectServiceImpl.java @@ -7,6 +7,7 @@ import com.electric_diary.DTO.Request.SubjectRequestDTO; import com.electric_diary.entities.StudentEntity; import com.electric_diary.entities.SubjectEntity; +import com.electric_diary.entities.TeacherEntity; import com.electric_diary.exception.NotFoundException; import com.electric_diary.repositories.StudentRepository; import com.electric_diary.repositories.SubjectRepository; @@ -68,6 +69,12 @@ public SubjectEntity updateSubject(Integer subjectId, SubjectRequestDTO subjectR @Override public SubjectEntity deleteSubject(Integer subjectId) { SubjectEntity subject = getSubjectById(subjectId); + for (StudentEntity student : subject.getEnrolledStudents()) + student.getSubjects().remove(subject); + + for (TeacherEntity teacher : subject.getTeachers()) + teacher.getSubjects().remove(subject); + subjectRepository.delete(subject); logger.info("Deleted subject with ID {}.", subjectId); return subject;