From f0a411c03173a2470413a53db000ced81e610c9e Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Thu, 14 Mar 2024 16:36:09 +0100 Subject: [PATCH] added InscriptionController --- .../EndPoints/InscriptionController.java | 84 +++++++++++++++++++ .../Clyde/EndPoints/LoginController.java | 2 +- .../Repositories/InscriptionRepository.java | 3 + .../Clyde/Services/AuthenticatorService.java | 8 +- .../Clyde/Services/InscriptionService.java | 17 +++- 5 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java new file mode 100644 index 0000000..a997c54 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/InscriptionController.java @@ -0,0 +1,84 @@ +package ovh.herisson.Clyde.EndPoints; + +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.InscriptionService; +import ovh.herisson.Clyde.Tables.InscriptionRequest; +import ovh.herisson.Clyde.Tables.Role; +import ovh.herisson.Clyde.Tables.User; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +@RestController +@CrossOrigin(originPatterns = "*", allowCredentials = "true") + +public class InscriptionController { + + + private final InscriptionService inscriptionServ; + private final AuthenticatorService authServ; + + public InscriptionController(InscriptionService inscriptionServ, AuthenticatorService authServ){ + this.inscriptionServ = inscriptionServ; + this.authServ = authServ; + } + + + @GetMapping("/inscriptionRequests") + public ResponseEntity>> getAllRequests(@RequestHeader("Authorization") String token){ + + if (!isSecretaryOrAdmin(token)){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); + } + + + @GetMapping("/inscriptionRequest/{id}") + public ResponseEntity> getById(@PathVariable long id){ + + + + InscriptionRequest inscriptionRequest = inscriptionServ.getById(id); + if (inscriptionRequest == null) {return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);} + + return new ResponseEntity<>(requestWithoutPassword(inscriptionRequest), HttpStatus.OK); + } + + + private Map requestWithoutPassword(InscriptionRequest inscriptionRequest) { + Map toReturn = new HashMap<>(); + + toReturn.put("id", inscriptionRequest.getId()); + toReturn.put("firstName", inscriptionRequest.getFirstName()); + toReturn.put("lastName", inscriptionRequest.getLastName()); + toReturn.put("address", inscriptionRequest.getAddress()); + toReturn.put("birthDate", inscriptionRequest.getBirthDate()); + toReturn.put("country", inscriptionRequest.getCountry()); + toReturn.put("cursus", inscriptionRequest.getCursus()); + toReturn.put("profilePictureUrl", inscriptionRequest.getProfilePicture()); + toReturn.put("state", inscriptionRequest.getState()); + 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/EndPoints/LoginController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java index 0b96535..20542b5 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LoginController.java @@ -13,6 +13,7 @@ import ovh.herisson.Clyde.Tables.InscriptionRequest; import ovh.herisson.Clyde.Tables.User; import java.util.Date; +import java.util.HashMap; @RestController @CrossOrigin(originPatterns = "*", allowCredentials = "true") @@ -51,7 +52,6 @@ public class LoginController { public ResponseEntity register(@RequestBody InscriptionRequest inscriptionRequest){ authServ.register(inscriptionRequest); - return new ResponseEntity<>("Is OK", HttpStatus.OK); } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/InscriptionRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/InscriptionRepository.java index 9f09b4b..0d83e6b 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/InscriptionRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/InscriptionRepository.java @@ -3,5 +3,8 @@ package ovh.herisson.Clyde.Repositories; import org.springframework.data.repository.CrudRepository; import ovh.herisson.Clyde.Tables.InscriptionRequest; + public interface InscriptionRepository extends CrudRepository { + + InscriptionRequest findById(long aLong); } 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 13d0fce..60dc6bc 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java @@ -2,6 +2,7 @@ package ovh.herisson.Clyde.Services; import org.springframework.stereotype.Service; import ovh.herisson.Clyde.EndPoints.LoginController; +import ovh.herisson.Clyde.Repositories.InscriptionRepository; import ovh.herisson.Clyde.Tables.InscriptionRequest; import ovh.herisson.Clyde.Tables.Token; import ovh.herisson.Clyde.Tables.User; @@ -13,10 +14,12 @@ public class AuthenticatorService { private final TokenService tokenService; private final UserService userService; + private final InscriptionService inscriptionService; - public AuthenticatorService(TokenService tokenService, UserService userService){ + public AuthenticatorService(TokenService tokenService, UserService userService, InscriptionService inscriptionService){ this.tokenService = tokenService; this.userService = userService; + this.inscriptionService = inscriptionService; } public User getUserFromToken(String token){ @@ -34,7 +37,6 @@ public class AuthenticatorService { } public void register(InscriptionRequest inscriptionRequest) { - - + inscriptionService.save(inscriptionRequest); } } 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 b0739d1..45495b7 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/InscriptionService.java @@ -4,6 +4,9 @@ import org.springframework.stereotype.Service; import ovh.herisson.Clyde.Repositories.InscriptionRepository; import ovh.herisson.Clyde.Tables.InscriptionRequest; +import java.util.HashMap; +import java.util.Map; + @Service public class InscriptionService { @@ -16,8 +19,16 @@ public class InscriptionService { this.incriptionRepo = inscriptionRepo; } - //todo return sans le mdp - public InscriptionRequest getById(int id){ - return null; + public InscriptionRequest getById(long id){ + InscriptionRequest inscriptionRequest = incriptionRepo.findById(id); + + if (inscriptionRequest == null){ + return null; + } + return inscriptionRequest; + } + + public Iterable getAll(){ + return incriptionRepo.findAll(); } } \ No newline at end of file