added InscriptionController
This commit is contained in:
parent
157e5951fc
commit
f0a411c031
@ -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<Iterable<Map<String,Object>>> getAllRequests(@RequestHeader("Authorization") String token){
|
||||
|
||||
if (!isSecretaryOrAdmin(token)){return new UnauthorizedResponse<>(null);}
|
||||
|
||||
Iterable<InscriptionRequest> inscriptionRequests = inscriptionServ.getAll();
|
||||
ArrayList<Map<String,Object>> toReturn = new ArrayList<>();
|
||||
|
||||
for (InscriptionRequest i:inscriptionRequests){
|
||||
toReturn.add(requestWithoutPassword(i));
|
||||
}
|
||||
|
||||
return new ResponseEntity<>(toReturn, HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/inscriptionRequest/{id}")
|
||||
public ResponseEntity<Map<String,Object>> 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<String,Object> requestWithoutPassword(InscriptionRequest inscriptionRequest) {
|
||||
Map<String, Object> 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;
|
||||
}
|
||||
}
|
@ -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<String> register(@RequestBody InscriptionRequest inscriptionRequest){
|
||||
|
||||
authServ.register(inscriptionRequest);
|
||||
|
||||
return new ResponseEntity<>("Is OK", HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
@ -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,Long> {
|
||||
|
||||
InscriptionRequest findById(long aLong);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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<InscriptionRequest> getAll(){
|
||||
return incriptionRepo.findAll();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user