Skip to content

Commit

Permalink
Fix: Delete Subject (#113)
Browse files Browse the repository at this point in the history
* fix: path variable typo

* feat: add FetchType.EAGER

* feat: remove all students and teachers before deleting
  • Loading branch information
HunorTotBagi authored Sep 20, 2024
1 parent ed2bf85 commit 999b8ed
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public ResponseEntity<SubjectEntity> updateSubject(@PathVariable Integer subject
}

@DeleteMapping("/{subjectId}")
public ResponseEntity<SubjectEntity> deleteSubject(@PathVariable Integer subjectid) {
return new ResponseEntity<>(subjectService.deleteSubject(subjectid), HttpStatus.OK);
public ResponseEntity<SubjectEntity> deleteSubject(@PathVariable Integer subjectId) {
return new ResponseEntity<>(subjectService.deleteSubject(subjectId), HttpStatus.OK);
}

@PutMapping("/{subjectId}/students/{studentId}")
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/electric_diary/entities/SubjectEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,14 +36,14 @@ public class SubjectEntity {

@NotNull
@JsonIgnore
@OneToMany(mappedBy = "subject")
@OneToMany(mappedBy = "subject", fetch = FetchType.EAGER)
private Set<GradeEntity> grades = new HashSet<GradeEntity>();

@ManyToMany
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "student_enrolled", joinColumns = @JoinColumn(name = "subject_id"), inverseJoinColumns = @JoinColumn(name = "student_id"))
private Set<StudentEntity> enrolledStudents = new HashSet<>();

@ManyToMany(mappedBy = "subjects")
@ManyToMany(mappedBy = "subjects", fetch = FetchType.EAGER)
private Set<TeacherEntity> teachers = new HashSet<>();

public void enrolStudents(StudentEntity student) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 999b8ed

Please sign in to comment.