From 4379794dba7d8fbb591ca28387a27eec114c9205 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sat, 16 Mar 2024 23:05:26 +0100 Subject: [PATCH] added /students endpoint --- .../Clyde/EndPoints/UserController.java | 18 ++++++++++++++++++ .../Clyde/Repositories/UserRepository.java | 3 +++ .../herisson/Clyde/Services/UserService.java | 2 ++ 3 files changed, 23 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 8437b81..238ebd3 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -90,6 +90,24 @@ public class UserController { } + @GetMapping("/students") + public ResponseEntity>> getAllStudent(@RequestHeader("Authorization") String token){ + if (authServ.getUserFromToken(token) == null) + return new UnauthorizedResponse<>(null); + + Iterable teachers = userService.getAllStudents(); + ArrayList> withoutPassword = new ArrayList<>(); + + for (User t: teachers){ + withoutPassword.add(userWithoutPassword(t)); + } + + return new ResponseEntity<>(withoutPassword, HttpStatus.OK); + } + + + + /** return user's data except password * @param user the user to return diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java index b2643e0..f44760c 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java @@ -19,4 +19,7 @@ public interface UserRepository extends CrudRepository { @Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Teacher") Iterable findAllTeachers(); + + @Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Student") + Iterable findAllStudents(); } \ No newline at end of file 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 a561512..d99692a 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java @@ -107,4 +107,6 @@ public class UserService { public Iterable getAllTeachers (){return userRepo.findAllTeachers();} + + public Iterable getAllStudents(){return userRepo.findAllStudents();} } \ No newline at end of file