From b39b1b3f15480e9bdb0f84e860e71a51b054caad Mon Sep 17 00:00:00 2001 From: Hunor Tot-Bagi Date: Sun, 22 Sep 2024 17:13:33 +0200 Subject: [PATCH] fix assigning teacher to a subject --- .../com/electric_diary/controllers/TeacherController.java | 6 +++--- .../java/com/electric_diary/services/TeacherService.java | 2 +- .../com/electric_diary/services/impl/ParentServiceImpl.java | 2 +- .../electric_diary/services/impl/StudentServiceImpl.java | 4 ++++ .../electric_diary/services/impl/TeacherServiceImpl.java | 4 ++-- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/electric_diary/controllers/TeacherController.java b/src/main/java/com/electric_diary/controllers/TeacherController.java index 4c708df..698a3cc 100644 --- a/src/main/java/com/electric_diary/controllers/TeacherController.java +++ b/src/main/java/com/electric_diary/controllers/TeacherController.java @@ -51,8 +51,8 @@ public ResponseEntity deleteTeacher(@PathVariable Integer teacher return new ResponseEntity<>(teacherService.deleteTeacher(teacherId), HttpStatus.OK); } - @PutMapping("/teaches/Subject") - public ResponseEntity teacherTeachesSubject(@PathVariable Integer teacherId, @RequestBody TeacherRequestDTO teacherDTOBody) { - return new ResponseEntity<>(teacherService.teacherTeachesSubject(teacherId, teacherDTOBody), HttpStatus.OK); + @PutMapping("/{teacherId}/subject/{subjectId}") + public ResponseEntity teacherTeachesSubject(@PathVariable Integer teacherId, @PathVariable Integer subjectId) { + return new ResponseEntity<>(teacherService.teacherTeachesSubject(teacherId, subjectId), HttpStatus.OK); } } \ No newline at end of file diff --git a/src/main/java/com/electric_diary/services/TeacherService.java b/src/main/java/com/electric_diary/services/TeacherService.java index e8a717f..647f0c6 100644 --- a/src/main/java/com/electric_diary/services/TeacherService.java +++ b/src/main/java/com/electric_diary/services/TeacherService.java @@ -14,5 +14,5 @@ public interface TeacherService { public TeacherEntity deleteTeacher(Integer teacherId); - public TeacherEntity teacherTeachesSubject(Integer teacherId, TeacherRequestDTO teacherRequestDTO); + public TeacherEntity teacherTeachesSubject(Integer teacherId, Integer subjectId); } diff --git a/src/main/java/com/electric_diary/services/impl/ParentServiceImpl.java b/src/main/java/com/electric_diary/services/impl/ParentServiceImpl.java index 47ec0d1..8b8515d 100644 --- a/src/main/java/com/electric_diary/services/impl/ParentServiceImpl.java +++ b/src/main/java/com/electric_diary/services/impl/ParentServiceImpl.java @@ -117,7 +117,7 @@ private UserEntity createUser(String firstName, String lastName, String email, S throw new IllegalArgumentException("Invalid email format."); } - RoleEntity newRole = roleRepository.findById(202).orElseThrow(() -> new NotFoundException("Role", 202)); + RoleEntity newRole = roleRepository.findById(3).orElseThrow(() -> new NotFoundException("Role", 3)); UserEntity user = new UserEntity(); user.setName(firstName); diff --git a/src/main/java/com/electric_diary/services/impl/StudentServiceImpl.java b/src/main/java/com/electric_diary/services/impl/StudentServiceImpl.java index 0764267..e1687f5 100644 --- a/src/main/java/com/electric_diary/services/impl/StudentServiceImpl.java +++ b/src/main/java/com/electric_diary/services/impl/StudentServiceImpl.java @@ -8,6 +8,7 @@ import com.electric_diary.entities.ClassEntity; import com.electric_diary.entities.ParentEntity; import com.electric_diary.entities.StudentEntity; +import com.electric_diary.entities.TeacherEntity; import com.electric_diary.entities.UserEntity; import com.electric_diary.exception.NotFoundException; import com.electric_diary.repositories.ClassRepository; @@ -90,6 +91,9 @@ public StudentEntity updateStudent(Integer studentId, StudentRequestDTO studentR @Override public StudentEntity deleteStudent(Integer studentId) { StudentEntity student = getStudentById(studentId); + + student.getParent().getStudents().remove(student); + studentRepository.delete(student); logger.info("Deleted student with ID {}.", studentId); return student; diff --git a/src/main/java/com/electric_diary/services/impl/TeacherServiceImpl.java b/src/main/java/com/electric_diary/services/impl/TeacherServiceImpl.java index f7271d1..025a113 100644 --- a/src/main/java/com/electric_diary/services/impl/TeacherServiceImpl.java +++ b/src/main/java/com/electric_diary/services/impl/TeacherServiceImpl.java @@ -100,9 +100,9 @@ public TeacherEntity deleteTeacher(Integer teacherId) { } @Override - public TeacherEntity teacherTeachesSubject(Integer teacherId, TeacherRequestDTO teacherRequestDTO) { + public TeacherEntity teacherTeachesSubject(Integer teacherId, Integer subjectId) { TeacherEntity teacher = getTeacherById(teacherId); - SubjectEntity subject = getSubjectById(teacherRequestDTO.getSubjectId()); + SubjectEntity subject = getSubjectById(subjectId); teacher.addSubjects(subject); teacherRepository.save(teacher);