Updated API connection
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
package hackathon.FrisbYEE.jpa.service;
|
||||
|
||||
import hackathon.FrisbYEE.jpa.metier.Coach;
|
||||
import hackathon.FrisbYEE.jpa.metier.User;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface UserDAO extends JpaRepository<User, Integer> {
|
||||
|
||||
Optional<Coach> findByKeycloakId(String keycloakId);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package hackathon.FrisbYEE.rest;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import hackathon.FrisbYEE.jpa.dto.AthleteDTO;
|
||||
import hackathon.FrisbYEE.jpa.dto.UserDTO;
|
||||
import hackathon.FrisbYEE.jpa.metier.Athlete;
|
||||
import hackathon.FrisbYEE.jpa.metier.User;
|
||||
import hackathon.FrisbYEE.jpa.service.UserDAO;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.parameters.RequestBody;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/users")
|
||||
@CrossOrigin(origins = "http://localhost:3000")
|
||||
public class UserResource {
|
||||
@Autowired
|
||||
private UserDAO userDAO;
|
||||
|
||||
@Operation(summary = "Récupère tous les utilisateurs")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "Récupère tous les athlètes", content = @Content(mediaType = "application/json", schema = @Schema(implementation = List.class)))
|
||||
})
|
||||
@GetMapping("/all")
|
||||
@PreAuthorize("hasRole('admin') or hasRole('coach')")
|
||||
public ResponseEntity<List<UserDTO>> all() {
|
||||
List<User> users = userDAO.findAll();
|
||||
List<UserDTO> dtos = new ArrayList<>();
|
||||
for (User user : users) {
|
||||
dtos.add(mapToDTO(user));
|
||||
}
|
||||
return ResponseEntity.ok(dtos);
|
||||
}
|
||||
|
||||
@Operation(summary = "Récupère l'utilisateur ayant l'identifiant correspondant")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "Récupération effectuée", content = @Content(mediaType = "application/json", schema = @Schema(implementation = UserDTO.class)))
|
||||
})
|
||||
@GetMapping("/{id}")
|
||||
@PreAuthorize("hasRole('admin') or hasRole('coach') or hasRole('athlete')")
|
||||
public ResponseEntity<UserDTO> getById(@PathVariable String id) {
|
||||
User user = userDAO.findByKeycloakId(id).get();
|
||||
return ResponseEntity.ok(mapToDTO(user));
|
||||
}
|
||||
|
||||
private UserDTO mapToDTO(User user) {
|
||||
UserDTO dto = new UserDTO();
|
||||
dto.setId_keycloak(user.getKeycloakId());
|
||||
dto.setName(user.getName());
|
||||
dto.setPrenom(user.getPrenom());
|
||||
dto.setRole(user.getRole());
|
||||
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user