diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java index f3e93d4..566121d 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java @@ -11,7 +11,6 @@ import ovh.herisson.Clyde.Services.TeacherCourseService; import ovh.herisson.Clyde.Tables.Course; import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.User; - import java.util.HashMap; import java.util.Map; @@ -119,4 +118,20 @@ public class CourseController { return new ResponseEntity<>(HttpStatus.OK); } + + + @DeleteMapping("course/{id}") + public ResponseEntity deleteUser(@RequestHeader("Authorization") String token, @PathVariable Long id){ + if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary}, token)) + return new UnauthorizedResponse<>(null); + + Course toDelete = courseServ.findById(id); + + if (toDelete == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + + courseServ.delete(courseServ.findById(id)); + return new ResponseEntity<>(HttpStatus.OK); + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java index 7a1bfe4..dda0c63 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java @@ -67,4 +67,18 @@ public class CurriculumController { return new ResponseEntity<>(HttpStatus.OK); } + + @DeleteMapping("/curriculum/{id}") + public ResponseEntity deleteCurriculum(@RequestHeader("Authorization") String token, @PathVariable Long id){ + if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary}, token)) + return new UnauthorizedResponse<>(null); + + Curriculum toDelete = curriculumServ.findById(id); + + if (toDelete == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + curriculumServ.delete(toDelete); + return new ResponseEntity<>(HttpStatus.OK); + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java index 37312d3..c70e4df 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java @@ -10,8 +10,6 @@ import ovh.herisson.Clyde.Services.ProtectionService; import ovh.herisson.Clyde.Tables.InscriptionRequest; import ovh.herisson.Clyde.Tables.RequestState; import ovh.herisson.Clyde.Tables.Role; -import java.util.ArrayList; -import java.util.HashMap; import java.util.Map; @RestController @@ -68,4 +66,18 @@ public class InscriptionController { return new ResponseEntity<>(HttpStatus.OK); } + @DeleteMapping("/request/register/{id}") + public ResponseEntity deleteRequest(@RequestHeader("Authorization") String token, @PathVariable Long id){ + if (authServ.isNotIn(new Role[]{Role.Admin,Role.InscriptionService}, token)) + return new UnauthorizedResponse<>(null); + + InscriptionRequest toDelete = inscriptionServ.getById(id); + + if (toDelete == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + inscriptionServ.delete(toDelete); + return new ResponseEntity<>(HttpStatus.OK); + } + } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java index 859bf54..3ebf67a 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -127,7 +127,12 @@ public class UserController { if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) return new UnauthorizedResponse<>(null); - userService.delete(userService.getUserById(id)); + User toDelete = userService.getUserById(id); + + if (toDelete == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + userService.delete(toDelete); return new ResponseEntity<>(HttpStatus.OK); } } \ No newline at end of file diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/CourseService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/CourseService.java index b5dd906..d17c7b0 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/CourseService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/CourseService.java @@ -77,4 +77,7 @@ public class CourseService { return true; } + public void delete(Course course) { + courseRepo.delete(course); + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumService.java index 0c9dc42..af04d78 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumService.java @@ -19,4 +19,7 @@ public class CurriculumService { return curriculumRepo.findById(id); } + public void delete(Curriculum curriculum) { + curriculumRepo.delete(curriculum); + } } \ No newline at end of file diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java index 7ae6e74..311dbf2 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java @@ -87,4 +87,8 @@ public class InscriptionService { save(inscrRequest); return true; } + + public void delete(InscriptionRequest toDelete) { + inscriptionRepo.delete(toDelete); + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/ProtectionService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/ProtectionService.java index 6c300ba..44e53a7 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/ProtectionService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/ProtectionService.java @@ -15,6 +15,10 @@ public class ProtectionService { * @return all the user data without the password */ public static HashMap userWithoutPassword(User user){ + + if (user ==null) + return null; + HashMap toReturn = new HashMap<>(); toReturn.put("regNo",user.getRegNo()); @@ -42,6 +46,9 @@ public class ProtectionService { public static HashMap courseWithoutPassword(Course course){ + if (course == null) + return null; + HashMap toReturn = new HashMap<>(); toReturn.put("courseId",course.getCourseID()); @@ -64,6 +71,10 @@ public class ProtectionService { public static Map requestWithoutPassword(InscriptionRequest inscriptionRequest) { + + if (inscriptionRequest == null) + return null; + Map toReturn = new HashMap<>(); toReturn.put("id", inscriptionRequest.getId()); diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java index e338d7d..df0421d 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java @@ -1,6 +1,8 @@ package ovh.herisson.Clyde.Tables; import jakarta.persistence.*; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; @Entity public class Course { @@ -11,6 +13,7 @@ public class Course { private String title; @ManyToOne(fetch = FetchType.EAGER) + @OnDelete(action = OnDeleteAction.SET_NULL) @JoinColumn(name = "Users") private User owner;