From 2fb6aef67c8e77c83eb4926c4418112c84c924f7 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sun, 17 Mar 2024 21:48:58 +0100 Subject: [PATCH 1/7] added GET /user/{id} --- .../ovh/herisson/Clyde/EndPoints/UserController.java | 10 ++++++++++ .../java/ovh/herisson/Clyde/Services/UserService.java | 4 ++++ 2 files changed, 14 insertions(+) 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 2ace404..4be3443 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -40,6 +40,16 @@ public class UserController { return new ResponseEntity<>(ProtectionService.userWithoutPassword(user), HttpStatus.OK); } + + @GetMapping("/user/{id}") + public ResponseEntity> getUserById(@RequestHeader("Authorization") String token, @PathVariable Long id){ + + if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary,Role.InscriptionService},token)) + return new UnauthorizedResponse<>(null); + + return new ResponseEntity<>(ProtectionService.userWithoutPassword(userService.getUserById(id)), HttpStatus.OK); + } + @PostMapping("/user") public ResponseEntity> postUser(@RequestBody User user,@RequestHeader("Authorization") String token){ diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java index 3d30a89..b4409e9 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java @@ -122,4 +122,8 @@ public class UserService { public Iterable getAllTeachers (){return userRepo.findAllTeachers();} public Iterable getAllStudents(){return userRepo.findAllStudents();} + + public User getUserById(long id) { + return userRepo.findById(id); + } } \ No newline at end of file From f484fb095e58129cab610caacf287953e7f591c8 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Mon, 18 Mar 2024 00:14:26 +0100 Subject: [PATCH 2/7] added protection for inscription requests --- .../EndPoints/InscriptionController.java | 31 +++---------------- .../Clyde/EndPoints/LoginController.java | 9 ++++-- .../Clyde/EndPoints/UserController.java | 9 ++++++ .../Clyde/Services/ProtectionService.java | 29 +++++++++++++++++ 4 files changed, 50 insertions(+), 28 deletions(-) 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 6c71fd3..37312d3 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java @@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.*; import ovh.herisson.Clyde.Responses.UnauthorizedResponse; import ovh.herisson.Clyde.Services.AuthenticatorService; import ovh.herisson.Clyde.Services.InscriptionService; +import ovh.herisson.Clyde.Services.ProtectionService; import ovh.herisson.Clyde.Tables.InscriptionRequest; import ovh.herisson.Clyde.Tables.RequestState; import ovh.herisson.Clyde.Tables.Role; @@ -34,13 +35,8 @@ public class InscriptionController { return new UnauthorizedResponse<>(null); Iterable inscriptionRequests = inscriptionServ.getAll(); - ArrayList> toReturn = new ArrayList<>(); - for (InscriptionRequest i:inscriptionRequests){ - toReturn.add(requestWithoutPassword(i)); - } - - return new ResponseEntity<>(toReturn, HttpStatus.OK); + return new ResponseEntity<>(ProtectionService.requestsWithoutPasswords(inscriptionRequests), HttpStatus.OK); } @@ -55,38 +51,21 @@ public class InscriptionController { if (foundInscriptionRequest == null) return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST); - return new ResponseEntity<>(requestWithoutPassword(foundInscriptionRequest), HttpStatus.OK); + return new ResponseEntity<>(ProtectionService.requestWithoutPassword(foundInscriptionRequest), HttpStatus.OK); } @PatchMapping("/request/register/{id}") public ResponseEntity changeRequestState(@PathVariable long id, @RequestHeader("Authorization") String token, - @RequestBody RequestState requestState) + @RequestBody RequestState state) { if (authServ.isNotIn(new Role[]{Role.InscriptionService,Role.Admin},token)) return new UnauthorizedResponse<>(null); - if (!inscriptionServ.modifyState(id, requestState)) + if (!inscriptionServ.modifyState(id, state)) return new ResponseEntity<>(HttpStatus.BAD_REQUEST); return new ResponseEntity<>(HttpStatus.OK); } - - private Map requestWithoutPassword(InscriptionRequest inscriptionRequest) { - Map toReturn = new HashMap<>(); - - toReturn.put("id", inscriptionRequest.getId()); - toReturn.put("lastName", inscriptionRequest.getLastName()); - toReturn.put("firstName", inscriptionRequest.getFirstName()); - toReturn.put("address", inscriptionRequest.getAddress()); - toReturn.put("email",inscriptionRequest.getEmail()); - toReturn.put("birthDate", inscriptionRequest.getBirthDate()); - toReturn.put("country", inscriptionRequest.getCountry()); - toReturn.put("curriculum", inscriptionRequest.getCurriculumId()); - toReturn.put("state", inscriptionRequest.getState()); - toReturn.put("profilePictureUrl", inscriptionRequest.getProfilePicture()); - - return toReturn; - } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java index 9367484..ef3c559 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java @@ -7,8 +7,10 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import ovh.herisson.Clyde.Responses.UnauthorizedResponse; import ovh.herisson.Clyde.Services.AuthenticatorService; +import ovh.herisson.Clyde.Services.ProtectionService; import ovh.herisson.Clyde.Tables.InscriptionRequest; import java.util.Date; +import java.util.Map; @RestController @CrossOrigin(originPatterns = "*", allowCredentials = "true") @@ -45,7 +47,10 @@ public class LoginController { } @PostMapping("/register") - public ResponseEntity register(@RequestBody InscriptionRequest inscriptionRequest){ - return new ResponseEntity<>(authServ.register(inscriptionRequest), HttpStatus.CREATED); + public ResponseEntity> register(@RequestBody InscriptionRequest inscriptionRequest){ + + InscriptionRequest returnedInscriptionRequest = authServ.register(inscriptionRequest); + + return new ResponseEntity<>(ProtectionService.requestWithoutPassword(returnedInscriptionRequest), HttpStatus.CREATED); } } 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 4be3443..859bf54 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -121,4 +121,13 @@ public class UserController { return new ResponseEntity<>(ProtectionService.usersWithoutPasswords(students), HttpStatus.OK); } + + @DeleteMapping("/user/{id}") + public ResponseEntity deleteStudent(@RequestHeader("Authorization") String token, @PathVariable Long id){ + if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) + return new UnauthorizedResponse<>(null); + + userService.delete(userService.getUserById(id)); + return new ResponseEntity<>(HttpStatus.OK); + } } \ No newline at end of file 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 7f2bea8..6c300ba 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/ProtectionService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/ProtectionService.java @@ -1,10 +1,12 @@ package ovh.herisson.Clyde.Services; import ovh.herisson.Clyde.Tables.Course; +import ovh.herisson.Clyde.Tables.InscriptionRequest; import ovh.herisson.Clyde.Tables.User; import java.util.ArrayList; import java.util.HashMap; +import java.util.Map; public class ProtectionService { @@ -61,5 +63,32 @@ public class ProtectionService { } + public static Map requestWithoutPassword(InscriptionRequest inscriptionRequest) { + Map toReturn = new HashMap<>(); + + toReturn.put("id", inscriptionRequest.getId()); + toReturn.put("lastName", inscriptionRequest.getLastName()); + toReturn.put("firstName", inscriptionRequest.getFirstName()); + toReturn.put("address", inscriptionRequest.getAddress()); + toReturn.put("email",inscriptionRequest.getEmail()); + toReturn.put("birthDate", inscriptionRequest.getBirthDate()); + toReturn.put("country", inscriptionRequest.getCountry()); + toReturn.put("curriculum", inscriptionRequest.getCurriculumId()); + toReturn.put("state", inscriptionRequest.getState()); + toReturn.put("profilePictureUrl", inscriptionRequest.getProfilePicture()); + + return toReturn; + } + + public static Iterable> requestsWithoutPasswords(Iterable inscriptionRequests){ + + ArrayList> toReturn = new ArrayList<>(); + + for (InscriptionRequest i:inscriptionRequests){ + toReturn.add(requestWithoutPassword(i)); + } + return toReturn; + } + } From a80fb2b2971c5ba675041fc0217e4b6d41073a56 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Mon, 18 Mar 2024 00:14:48 +0100 Subject: [PATCH 3/7] added DELETE user and prepared tables for cascade deletion --- .../ovh/herisson/Clyde/Services/AuthenticatorService.java | 1 + .../main/java/ovh/herisson/Clyde/Services/UserService.java | 4 ++++ .../java/ovh/herisson/Clyde/Tables/CurriculumCourse.java | 4 ++++ .../java/ovh/herisson/Clyde/Tables/ReInscriptionRequest.java | 4 ++++ .../main/java/ovh/herisson/Clyde/Tables/TeacherCourse.java | 4 ++++ backend/src/main/java/ovh/herisson/Clyde/Tables/Token.java | 3 +++ .../main/java/ovh/herisson/Clyde/Tables/UserCurriculum.java | 5 ++++- 7 files changed, 24 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java index fc29fb6..25c127f 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java @@ -32,6 +32,7 @@ public class AuthenticatorService { } public InscriptionRequest register(InscriptionRequest inscriptionRequest) { + inscriptionRequest.setState(RequestState.Pending); return inscriptionService.save(inscriptionRequest); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java index b4409e9..6a7685b 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java @@ -126,4 +126,8 @@ public class UserService { public User getUserById(long id) { return userRepo.findById(id); } + + public void delete(User user) { + userRepo.delete(user); + } } \ No newline at end of file diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/CurriculumCourse.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/CurriculumCourse.java index 8202e8d..0b660eb 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/CurriculumCourse.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/CurriculumCourse.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 CurriculumCourse { @@ -10,9 +12,11 @@ public class CurriculumCourse { @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "Curriculum") + @OnDelete(action = OnDeleteAction.CASCADE) private Curriculum curriculum; @ManyToOne(fetch = FetchType.EAGER) + @OnDelete(action = OnDeleteAction.CASCADE) @JoinColumn(name = "Course") private Course course; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/ReInscriptionRequest.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/ReInscriptionRequest.java index 9bd3fba..b96ed42 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/ReInscriptionRequest.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/ReInscriptionRequest.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 ReInscriptionRequest { @@ -10,10 +12,12 @@ public class ReInscriptionRequest { @ManyToOne @JoinColumn(name = "Users") + @OnDelete(action = OnDeleteAction.CASCADE) private User user; @ManyToOne @JoinColumn(name = "Curriculum") + @OnDelete(action = OnDeleteAction.CASCADE) private Curriculum newCurriculum; private RequestState state; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/TeacherCourse.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/TeacherCourse.java index 3392c72..bce123b 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/TeacherCourse.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/TeacherCourse.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 TeacherCourse { @@ -9,11 +11,13 @@ public class TeacherCourse { private int id; @ManyToOne(fetch = FetchType.EAGER) + @OnDelete(action = OnDeleteAction.CASCADE) @JoinColumn(name = "Users") private User user; @ManyToOne(fetch = FetchType.EAGER) + @OnDelete(action = OnDeleteAction.CASCADE) @JoinColumn(name = "Course") private Course course; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Token.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Token.java index 29ef906..a68f15b 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Token.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Token.java @@ -1,6 +1,8 @@ package ovh.herisson.Clyde.Tables; import jakarta.persistence.*; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; import java.util.Date; @@ -11,6 +13,7 @@ public class Token { private int id; @ManyToOne(fetch = FetchType.EAGER) + @OnDelete(action = OnDeleteAction.CASCADE) @JoinColumn(name ="Users") private User user; private String token; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/UserCurriculum.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/UserCurriculum.java index 2202763..bd04113 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/UserCurriculum.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/UserCurriculum.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 UserCurriculum { @@ -10,10 +12,11 @@ public class UserCurriculum { //Un étudiant peut avoir plusieurs curriculums @ManyToOne(fetch = FetchType.EAGER) + @OnDelete(action = OnDeleteAction.CASCADE) @JoinColumn(name = "Users") private User user; - @OneToOne(fetch = FetchType.EAGER) + @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "Curriculum") private Curriculum curriculum; From 93c161be4cb910bf83a1a091ad1cd9b0c9f2684f Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Mon, 18 Mar 2024 11:22:21 +0100 Subject: [PATCH 4/7] added every delete endpoint required --- .../Clyde/EndPoints/CourseController.java | 17 ++++++++++++++++- .../Clyde/EndPoints/CurriculumController.java | 14 ++++++++++++++ .../Clyde/EndPoints/InscriptionController.java | 16 ++++++++++++++-- .../Clyde/EndPoints/UserController.java | 7 ++++++- .../herisson/Clyde/Services/CourseService.java | 3 +++ .../Clyde/Services/CurriculumService.java | 3 +++ .../Clyde/Services/InscriptionService.java | 4 ++++ .../Clyde/Services/ProtectionService.java | 11 +++++++++++ .../java/ovh/herisson/Clyde/Tables/Course.java | 3 +++ 9 files changed, 74 insertions(+), 4 deletions(-) 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; From fd18df7c3aaccf5a20d94a9b863a846c1bf433b0 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Mon, 18 Mar 2024 11:33:50 +0100 Subject: [PATCH 5/7] added a InscriptionRequest to POST /mock --- .../ovh/herisson/Clyde/EndPoints/MockController.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java index 358602b..6707fb7 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java @@ -22,16 +22,19 @@ public class MockController { public final CurriculumCourseService CurriculumCourseService; public final CurriculumService curriculumService; public final CourseService courseService; + + public final InscriptionService inscriptionService; ArrayList mockUsers; - public MockController(UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService, CurriculumCourseService CurriculumCourseService, CurriculumService curriculumService, CourseService courseService){ + public MockController(UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService, CurriculumCourseService CurriculumCourseService, CurriculumService curriculumService, CourseService courseService, InscriptionService inscriptionService){ this.tokenRepo = tokenRepo; this.userRepo = userRepo; this.tokenService = tokenService; this.CurriculumCourseService = CurriculumCourseService; this.curriculumService = curriculumService; this.courseService = courseService; + this.inscriptionService = inscriptionService; } /** Saves an example of each user type by : @@ -87,6 +90,11 @@ public class MockController { CurriculumCourseService.save(new CurriculumCourse(chemistryBab1,commun)); CurriculumCourseService.save(new CurriculumCourse(chemistryBab1,chemistry1)); + + InscriptionRequest inscriptionRequest = new InscriptionRequest("helen","prenom","non","helen@gmail.com","america",new Date(),(long) 1,RequestState.Refused,"yes.png","password"); + + inscriptionService.save(inscriptionRequest); + } } From e03a01ec89c7aa1c011b9b70ac8e3c6b3c7c9d79 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Mon, 18 Mar 2024 11:53:33 +0100 Subject: [PATCH 6/7] oups forgot somthing --- .../src/main/java/ovh/herisson/Clyde/Tables/UserCurriculum.java | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/UserCurriculum.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/UserCurriculum.java index bd04113..f42e588 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/UserCurriculum.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/UserCurriculum.java @@ -18,6 +18,7 @@ public class UserCurriculum { @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "Curriculum") + @OnDelete(action = OnDeleteAction.CASCADE) private Curriculum curriculum; public UserCurriculum(User user, Curriculum curriculum){ From fd357ba938215fd5e367d4f1204a6f1adb20ecfa Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Mon, 18 Mar 2024 12:15:13 +0100 Subject: [PATCH 7/7] GET /curriculum returns user's curriculum --- .../Clyde/EndPoints/CurriculumController.java | 21 +++++++++++++++---- .../UserCurriculumRepository.java | 6 ++++++ .../Clyde/Services/UserCurriculumService.java | 20 ++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Services/UserCurriculumService.java 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 dda0c63..409e269 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java @@ -5,9 +5,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import ovh.herisson.Clyde.Responses.UnauthorizedResponse; -import ovh.herisson.Clyde.Services.AuthenticatorService; -import ovh.herisson.Clyde.Services.CurriculumCourseService; -import ovh.herisson.Clyde.Services.CurriculumService; +import ovh.herisson.Clyde.Services.*; import ovh.herisson.Clyde.Tables.Curriculum; import ovh.herisson.Clyde.Tables.Role; @@ -21,11 +19,13 @@ public class CurriculumController { private final CurriculumService curriculumServ; private final AuthenticatorService authServ; + private final UserCurriculumService userCurriculumServ; private final CurriculumCourseService curriculumCourseServ; - public CurriculumController(CurriculumService curriculumServ, AuthenticatorService authServ, CurriculumCourseService curriculumCourseServ){ + public CurriculumController(CurriculumService curriculumServ, AuthenticatorService authServ, UserCurriculumService userCurriculumServ, CurriculumCourseService curriculumCourseServ){ this.curriculumServ = curriculumServ; this.authServ = authServ; + this.userCurriculumServ = userCurriculumServ; this.curriculumCourseServ = curriculumCourseServ; } @@ -39,6 +39,19 @@ public class CurriculumController { return new ResponseEntity<>(curriculumCourseServ.getDepthCurriculum(foundCurriculum), HttpStatus.OK); } + @GetMapping("/curriculum") + public ResponseEntity> findSelfCurriculum(@RequestHeader("Authorization") String token){ + if (authServ.getUserFromToken(token) == null) + return new UnauthorizedResponse<>(null); + + Curriculum curriculum = userCurriculumServ.findByUser(authServ.getUserFromToken(token)); + + if (curriculum == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + return new ResponseEntity<>(curriculumCourseServ.getDepthCurriculum(curriculum),HttpStatus.OK); + } + @GetMapping("/curriculums") public ResponseEntity>> findAllIndDepth(){ return new ResponseEntity<>(curriculumCourseServ.getAllDepthCurriculum(),HttpStatus.OK); diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserCurriculumRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserCurriculumRepository.java index 93cb10f..32f207a 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserCurriculumRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserCurriculumRepository.java @@ -1,7 +1,13 @@ package ovh.herisson.Clyde.Repositories; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; +import ovh.herisson.Clyde.Tables.Curriculum; +import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.UserCurriculum; public interface UserCurriculumRepository extends CrudRepository { + + @Query("select uc.curriculum from UserCurriculum uc where uc.user = ?1") + Curriculum findByUser(User student); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/UserCurriculumService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/UserCurriculumService.java new file mode 100644 index 0000000..6484e2b --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/UserCurriculumService.java @@ -0,0 +1,20 @@ +package ovh.herisson.Clyde.Services; + +import org.springframework.stereotype.Service; +import ovh.herisson.Clyde.Repositories.UserCurriculumRepository; +import ovh.herisson.Clyde.Tables.Curriculum; +import ovh.herisson.Clyde.Tables.User; + +@Service +public class UserCurriculumService { + + private final UserCurriculumRepository userCurriculumRepository; + + public UserCurriculumService(UserCurriculumRepository userCurriculumRepository) { + this.userCurriculumRepository = userCurriculumRepository; + } + + public Curriculum findByUser(User student){ + return userCurriculumRepository.findByUser(student); + } +}