From d5f6656e2bba2ba52e1d36c892401d6a23347c67 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sat, 16 Mar 2024 17:16:46 +0100 Subject: [PATCH] added the GET /teachers (#130) had to fix the mock and the UserController isAdminOrSecretary Reviewed-on: https://git.herisson.ovh/PGL/Clyde/pulls/130 Reviewed-by: Wal Reviewed-by: Debucquoy Anthony Co-authored-by: Bartha Maxime <231026@umons.ac.be> Co-committed-by: Bartha Maxime <231026@umons.ac.be> --- .../Clyde/EndPoints/MockController.java | 2 +- .../Clyde/EndPoints/UserController.java | 19 +++++++++++++++++++ .../Clyde/Repositories/UserRepository.java | 4 ++++ .../herisson/Clyde/Services/UserService.java | 4 ++++ 4 files changed, 28 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 608a450..089da2d 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java @@ -49,7 +49,7 @@ public class MockController { User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand",new Date(0), null,Role.Admin,passwordEncoder.encode("admin")); User joe = new User("Mama","Joe","student@student.com","roundabout","DaWarudo",new Date(0), null,Role.Student,passwordEncoder.encode("student")); - User meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0), null,Role.Teacher,passwordEncoder.encode("secretary")); + User meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0), null,Role.Secretary,passwordEncoder.encode("secretary")); User joke = new User("CthemBalls","Lemme","teacher@teacher.com","lab","faculty",new Date(0), null,Role.Teacher,passwordEncoder.encode("teacher")); User lena = new User("Louille","Lena","inscriptionService@InscriptionService.com","no","yes",new Date(0), null,Role.Teacher,passwordEncoder.encode("inscriptionService")); mockUsers = new ArrayList<>(Arrays.asList(herobrine,joe,meh,joke)); 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 412ba80..8437b81 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -9,6 +9,8 @@ import ovh.herisson.Clyde.Services.AuthenticatorService; import ovh.herisson.Clyde.Services.UserService; import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.User; + +import java.security.Key; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -72,6 +74,23 @@ public class UserController { return new ResponseEntity<>("data modified", HttpStatus.OK); } + + @GetMapping("/teachers") + public ResponseEntity>> getAllTeachers(@RequestHeader("Authorization") String token){ + if (authServ.getUserFromToken(token) == null) + return new UnauthorizedResponse<>(null); + Iterable teachers = userService.getAllTeachers(); + 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 * @return all the user data without the password 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 af2bd08..b2643e0 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java @@ -15,4 +15,8 @@ public interface UserRepository extends CrudRepository { /** @Query(value = "select a.* from Users a ",nativeQuery = true) Iterable findAllUsers();**/ + + @Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Teacher") + Iterable findAllTeachers(); + } \ 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 55a2f92..a561512 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java @@ -103,4 +103,8 @@ public class UserService { public Iterable getAll(){ return userRepo.findAll(); } + + + + public Iterable getAllTeachers (){return userRepo.findAllTeachers();} } \ No newline at end of file