added mocks users with their role as password and token
This commit is contained in:
@ -6,6 +6,9 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ovh.herisson.Clyde.Repositories.UserRepository;
|
||||
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
|
||||
import ovh.herisson.Clyde.Services.TokenService;
|
||||
import ovh.herisson.Clyde.Services.UserService;
|
||||
import ovh.herisson.Clyde.Tables.User;
|
||||
|
||||
|
||||
@ -13,30 +16,36 @@ import ovh.herisson.Clyde.Tables.User;
|
||||
@CrossOrigin(origins = "http://localhost:5173")
|
||||
public class UserController {
|
||||
|
||||
private final UserRepository userRepo;
|
||||
private final UserService userService;
|
||||
|
||||
public UserController(UserRepository userRepo){
|
||||
this.userRepo = userRepo;
|
||||
private final TokenService tokenService;
|
||||
public UserController(UserService userService, TokenService tokenService){
|
||||
this.userService =userService;
|
||||
this.tokenService = tokenService; // todo find a way to be clearer
|
||||
|
||||
tokenService.postMockToken(userService.postMockUsers());// todo find a better place to put that
|
||||
}
|
||||
|
||||
@GetMapping("/user")
|
||||
public ResponseEntity<User> getUsers(@RequestHeader("Authorization") String token){
|
||||
//TODO
|
||||
// Get the token thru the data base
|
||||
// tokenRepo.findToken(token) => User userFromToken
|
||||
// si role != secretary => return error : ResponseEntity<User>(null, HttpStatus.UNAUTHORIZED)
|
||||
return new ResponseEntity<User>(/**userRepo.findById(userFromToken.id),**/ HttpStatus.OK);
|
||||
|
||||
User user = tokenService.getUserFromToken(token);
|
||||
|
||||
if (user == null) {
|
||||
return new UnauthorizedResponse<User>(null);
|
||||
}
|
||||
return new ResponseEntity<User>(user, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping("/user")
|
||||
public ResponseEntity<String> postUser(@RequestBody User user){
|
||||
userRepo.save(user);
|
||||
userService.save(user);
|
||||
return new ResponseEntity<String>(String.format("Account created with ID:%s",user.getRegNo()),HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@GetMapping("/users")
|
||||
public Iterable<User> getAllUsers(){//TODO ne l'accepter que si c'est le secrétariat
|
||||
return userRepo.findAll();
|
||||
return userService.getAll();
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user