109 lines
5.2 KiB
TypeScript
109 lines
5.2 KiB
TypeScript
import axios from "axios";
|
|
|
|
import keycloak from "./keycloak";
|
|
import { get } from "http";
|
|
import { ActiviteDTO, AdminDTO, AthleteDTO, CoachDTO, SessionDTO } from "./classesDTO";
|
|
|
|
|
|
const api = axios.create({
|
|
baseURL: "http://localhost:8081/api",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
withCredentials: true,
|
|
});
|
|
|
|
// Simple interceptor to ensure headers object exists; actual token should be set via setAuthToken()
|
|
api.interceptors.request.use((config) => {
|
|
if (keycloak?.token) {
|
|
// eslint-disable-next-line no-param-reassign
|
|
config.headers.Authorization = `Bearer ${keycloak.token}`;
|
|
}
|
|
return config;
|
|
});
|
|
|
|
// Helpers to set/clear the Authorization header programmatically (call after Keycloak login)
|
|
export function setAuthToken(token: string | null | undefined) {
|
|
if (token) {
|
|
api.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
|
} else {
|
|
delete api.defaults.headers.common["Authorization"];
|
|
}
|
|
}
|
|
|
|
export function clearAuthToken() {
|
|
delete api.defaults.headers.common["Authorization"];
|
|
}
|
|
|
|
export const athleteService = {
|
|
// controller is mounted at /athlete
|
|
create: (data: any) => api.post<AthleteDTO>("/athlete/create", data),
|
|
getAll: () => api.get<AthleteDTO[]>("/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 | null) => api.get<SessionDTO[]>(`/athlete/athlete/${athleteId}/session`),
|
|
getAllSessions: () => api.get(`/athletes/session`),
|
|
getActivitiesForSession: (sessionId: number | string) => api.get(`/athletes/session/${sessionId}/activities`),
|
|
getSessionsAfterDate: (athleteId: number | string, date: string) => api.get(`/athletes/${athleteId}/session/after/${encodeURIComponent(date)}`),
|
|
getSessionsBetweenDates: (athleteId: number | string, startDate: string, endDate: string) => api.get(`/athletes/${athleteId}/session/between/${encodeURIComponent(startDate)}/${encodeURIComponent(endDate)}`),
|
|
addActivity: (id_sess: number, id_act: number) => api.get(`/${id_sess}/activities/add/${id_act}`)
|
|
};
|
|
|
|
export const activiteService = {
|
|
create: (data: any) => api.post("/activite/create", data),
|
|
delete: (id: number | string) => api.delete(`/activite/delete/${id}`),
|
|
update: (id: number | string, data: ActiviteDTO) => api.post(`/activite/update/${id}`, data),
|
|
getById: (id: number | string) => api.get(`/activite/${id}`),
|
|
getAll: () => api.get<ActiviteDTO[]>(`/activite/all`),
|
|
getByTheme: (theme: string) => api.get(`/activite/theme/${encodeURIComponent(theme)}`),
|
|
getDataActivite: (id: number | string) => api.get(`/activite/${id}`),
|
|
};
|
|
type DateBetween = {
|
|
startDate: string;
|
|
endDate: string;
|
|
}
|
|
|
|
export const sessionService = {
|
|
// controller uses singular /session/* endpoints
|
|
create: (data: SessionDTO) => api.post(`/session/create`, data),
|
|
getAll: () => api.get<SessionDTO[]>(`/session/all`),
|
|
getAllBetweenDate: (data: any) => api.get<SessionDTO[]>(`/session/all-between-dates`,{params: data,}),
|
|
getById: (id: number | null) => api.get(`/session/${id}`),
|
|
delete: (id: number | null) => api.delete(`/session/delete/${id}`),
|
|
update: (id: number | null, data: any) => api.put(`/session/update/${id}`, data),
|
|
|
|
getActivities: (sessionId: number | null) => api.get<ActiviteDTO[]>(`/session/${sessionId}/activities`),
|
|
addActivity: (sessionId: number | null, activityId: number) => api.post(`/session/${sessionId}/activities/${activityId}`),
|
|
subscribe: (sessionId: number | null, userId: number) => api.put(`/session/${sessionId}/subscribe/${userId}`),
|
|
unsubscribe: (sessionId: number | null, userId: number) => api.put(`/session/${sessionId}/unsubscribe/${userId}`),
|
|
};
|
|
|
|
export const coachService = {
|
|
// controller doesn't declare a class-level path consistently; support both common patterns
|
|
create: (data: CoachDTO) => api.post<CoachDTO>(`/coach/create`, data),
|
|
getAll: () => api.get<CoachDTO[]>(`/coach/all`),
|
|
getById: (id: number) => api.get(`/coach/${id}`),
|
|
getByKeycloakId: (keycloakId: string) => api.get(`/coach/keycloak/${keycloakId}`),
|
|
update: (id: number | string, data: any) => api.put(`/coach/update/${id}`, data),
|
|
delete: (id: number | string) => api.delete(`/coach/delete/${id}`),
|
|
getSessionsForCoach: (coachId: number | null) => api.get<SessionDTO[]>(`/coach/${coachId}/session`),
|
|
};
|
|
|
|
export const userService = {
|
|
getById: (id: number) => api.get(`/users/${id}`),
|
|
getByKeycloakId: (keycloak_id: string) => api.get(`/users/keycloak/${keycloak_id}`),
|
|
getAll: () => api.get(`/users/all`),
|
|
sync: () => api.post(`/users/sync`),
|
|
};
|
|
|
|
export const adminService = {
|
|
getByKeycloakId: (keycloak_id: string) => api.get(`/admin/keycloak/${keycloak_id}`),
|
|
getById: (id: number | string) => api.get<AdminDTO>(`/admin/${id}`),
|
|
create: (data: AdminDTO) => api.post<AdminDTO>("/admin/create", data),
|
|
};
|
|
|
|
export default api; |