Added Keycloak related endpoints and other ones

This commit is contained in:
Alexis Leboeuf
2026-01-08 15:47:46 +01:00
parent e72243d355
commit 95ce13181f
5 changed files with 46 additions and 12 deletions

View File

@@ -165,7 +165,6 @@ public class ActiviteResource {
}
}
private Activite mapToEntity(ActiviteDTO dto) {
Activite activite = new Activite();
//ID géré par Postgre ?

View File

@@ -81,6 +81,13 @@ public class AthleteResource {
return ResponseEntity.ok(mapToDTO(athlete));
}
@GetMapping("/keycloak/{keycloak_id}")
@PreAuthorize("hasRole('admin') or hasRole('coach') or hasRole('athlete')")
public ResponseEntity<AthleteDTO> getByKeycloakId(@PathVariable String keycloak_id) {
Athlete athlete = athleteDAO.findByKeycloakId(keycloak_id).get();
return ResponseEntity.ok(mapToDTO(athlete));
}
@Operation(summary = "Met à jour l'athlète ayant l'identifiant correspondant")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Mise à jour effectuée", content = @Content(mediaType = "application/json", schema = @Schema(implementation = AthleteDTO.class)))

View File

@@ -39,14 +39,22 @@ public class CoachResource {
return dtos;
}
@GetMapping("/{keycloak_id}")
@GetMapping("/keycloak/{keycloak_id}")
@PreAuthorize("hasRole('Admin') or hasRole('Coach')")
public CoachDTO getById(@PathVariable String keycloak_id) {
public CoachDTO getByKeycloakId(@PathVariable String keycloak_id) {
Coach coach = coachDAO.findByKeycloakId(keycloak_id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Coach not found"));
return mapToDTO(coach);
}
@GetMapping("/{id}")
@PreAuthorize("hasRole('Admin') or hasRole('Coach')")
public CoachDTO getById(@PathVariable Integer id) {
Coach coach = coachDAO.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Coach not found"));
return mapToDTO(coach);
}
@PutMapping("/update/{id}")
@PreAuthorize("hasRole('Admin')")
public ResponseEntity<CoachDTO> update(@PathVariable Integer id, @RequestBody CoachDTO dto) {
@@ -59,6 +67,15 @@ public class CoachResource {
return ResponseEntity.ok(updatedDto);
}
@GetMapping("/{id}/session")
@PreAuthorize("hasRole('Admin') or hasRole('Coach')")
public ResponseEntity<List<?>> getSessionsForCoach(@PathVariable Integer id) {
Coach coach = coachDAO.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Coach not found"));
List<?> sessions = coach.getSessions();
return ResponseEntity.ok(sessions);
}
@DeleteMapping("/delete/{id}")
@PreAuthorize("hasRole('Admin')")
public ResponseEntity<Void> delete(@PathVariable Integer id) {

View File

@@ -59,6 +59,13 @@ public class UserResource {
return ResponseEntity.ok(mapToDTO(user));
}
@GetMapping("/keycloak/{keycloak_id}")
@PreAuthorize("hasRole('admin') or hasRole('coach') or hasRole('athlete')")
public ResponseEntity<UserDTO> getByKeycloakId(@PathVariable String keycloak_id) {
User user = userDAO.findByKeycloakId(keycloak_id).get();
return ResponseEntity.ok(mapToDTO(user));
}
private UserDTO mapToDTO(User user) {
UserDTO dto = new UserDTO();
dto.setId(user.getId());

View File

@@ -1,6 +1,7 @@
import axios from "axios";
import keycloak from "./keycloak";
import { get } from "http";
const api = axios.create({
@@ -17,7 +18,6 @@ api.interceptors.request.use((config) => {
if (keycloak?.token) {
// eslint-disable-next-line no-param-reassign
config.headers.Authorization = `Bearer ${keycloak.token}`;
console.log(config.headers.Authorization);
}
return config;
});
@@ -36,11 +36,13 @@ export function clearAuthToken() {
}
export const athleteService = {
create: (data: any) => api.post("/athletes/create", data),
getAll: () => api.get("/athletes/all"),
getByKeycloakId: (id: number | string) => api.get(`/athletes/${id}`),
update: (id: number | string, data: any) => api.put(`/athletes/${id}`, data),
delete: (id: number | string) => api.delete(`/athletes/${id}`),
// controller is mounted at /athlete
create: (data: any) => api.post("/athlete/create", data),
getAll: () => api.get("/athlete/all"),
getById: (id: number | string) => api.get(`/athlete/${id}`),
getByKeycloakId: (keycloakId: string) => api.get(`/athlete/keycloak/${encodeURIComponent(keycloakId)}`),
update: (id: number | string, data: any) => api.put(`/athlete/${id}`, data),
delete: (id: number | string) => api.delete(`/athlete/${id}`),
// session-related endpoints exposed by AthleteResource
getSessionsForAthlete: (athleteId: number | string) => api.get(`/athletes/athlete/${athleteId}/session`),
@@ -57,6 +59,7 @@ export const activiteService = {
getById: (id: number | string) => api.get(`/activite/${id}`),
getAll: () => api.get(`/activite/all`),
getByTheme: (theme: string) => api.get(`/activite/theme/${encodeURIComponent(theme)}`),
getDataActivite: (id: number | string) => api.get(`/activite/${id}`),
};
export const sessionService = {
@@ -83,15 +86,16 @@ export const coachService = {
getByKeycloakId: (id: number | string) => api.get(`/coach/${id}`),
update: (id: number | string, data: any) => api.put(`/coach/update/${id}`, data),
delete: (id: number | string) => api.delete(`/coach/delete/${id}`),
getSessionsForCoach: (coachId: number | string) => api.get(`/coach/${coachId}/session`),
// plural convenience
createPlural: (data: any) => api.post(`/coaches`, data),
getAllPlural: () => api.get(`/coaches`),
};
export const userService = {
getByKeycloakId: (id: number | string) => api.get(`/users/${id}`),
getAll: () => api.get(`/users`),
getByKeycloakId: (keycloak_id: string) => api.get(`/users/${keycloak_id}`),
getAll: () => api.get(`/users/all`),
sync: () => api.post(`/users/sync`),
};
export default api;