1
0
forked from PGL/Clyde

added mocks users with their role as password and token

This commit is contained in:
2024-03-07 00:55:07 +01:00
parent 4a85a55290
commit 2938707f0f
6 changed files with 77 additions and 14 deletions

View File

@ -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();
}