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 59412c1..238ebd3 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -1,10 +1,7 @@ package ovh.herisson.Clyde.EndPoints; -import jakarta.servlet.http.HttpServletRequest; import org.springframework.http.HttpStatus; - -import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import ovh.herisson.Clyde.Responses.UnauthorizedResponse; @@ -13,7 +10,7 @@ import ovh.herisson.Clyde.Services.UserService; import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.User; -import java.io.IOException; +import java.security.Key; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -43,7 +40,7 @@ public class UserController { @PostMapping("/user") public ResponseEntity postUser(@RequestBody User user,@RequestHeader("Authorization") String authorization){ - if (!isSecretaryOrAdmin(authorization)) + if (authServ.isNotSecretaryOrAdmin(authorization)) return new UnauthorizedResponse<>(null); userService.save(user); @@ -53,7 +50,7 @@ public class UserController { @GetMapping("/users") public ResponseEntity>> getAllUsers(@RequestHeader("Authorization") String authorization){ - if (!isSecretaryOrAdmin(authorization)) + if (authServ.isNotSecretaryOrAdmin(authorization)) return new UnauthorizedResponse<>(null); Iterable users = userService.getAll(); @@ -78,6 +75,39 @@ 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); + } + + + @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 @@ -85,7 +115,6 @@ public class UserController { */ private HashMap userWithoutPassword(User user){ HashMap toReturn = new HashMap<>(); - toReturn.put("regNo",user.getRegNo()); toReturn.put("firstName",user.getFirstName()); toReturn.put("lastName",user.getLastName()); @@ -93,18 +122,7 @@ public class UserController { toReturn.put("country",user.getCountry()); toReturn.put("address",user.getAddress()); toReturn.put("role",user.getRole()); - return toReturn; } - - private boolean isSecretaryOrAdmin(String authorization){ - if (authorization ==null) - return false; - - User poster = authServ.getUserFromToken(authorization); - if (poster == null) return false; - - return poster.getRole() == Role.Secretary || poster.getRole() == Role.Admin; - } } 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 c2d5fb4..ff214db 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java @@ -106,4 +106,6 @@ public class UserService { public Iterable getAllTeachers (){return userRepo.findAllTeachers();} + + public Iterable getAllStudents(){return userRepo.findAllStudents();} } \ No newline at end of file