1 Commits

Author SHA1 Message Date
tuanvu
0bf66c8c08 add getGroupe dans session et athlete 2026-01-12 08:43:16 +01:00
3 changed files with 38 additions and 15 deletions

View File

@@ -51,8 +51,9 @@ public class AthleteResource {
@PreAuthorize("hasRole('admin') or hasRole('coach') or hasRole('athlete')")
public ResponseEntity<AthleteDTO> create(@RequestBody AthleteDTO dto) {
Athlete athlete = mapToEntity(dto);
if(athleteDAO.existsByKeycloakId(athlete.getKeycloakId())) {
return ResponseEntity.status(200).body(mapToDTO(athleteDAO.findByKeycloakId(athlete.getKeycloakId()).get()));
if (athleteDAO.existsByKeycloakId(athlete.getKeycloakId())) {
return ResponseEntity.status(200)
.body(mapToDTO(athleteDAO.findByKeycloakId(athlete.getKeycloakId()).get()));
}
athleteDAO.save(athlete);
return ResponseEntity.status(201).body(mapToDTO(athlete));
@@ -177,7 +178,7 @@ public class AthleteResource {
dto.setName(s.getName());
dto.setCreneau(s.getCreneau());
List<Integer> activiteIDs = new ArrayList<>();
for (Activite activite : s.getActivites()) {
for (Activite activite : s.getActivites()) {
activiteIDs.add(activite.getId());
}
dto.setActiviteIds(activiteIDs);
@@ -186,11 +187,11 @@ public class AthleteResource {
dto.setGroupe(s.getGroupe());
dto.setIsRecurrent(s.getIsRecurrent());
List<Integer> athleteIds = new ArrayList<>();
for (Athlete athlete : s.getAthletes()) {
for (Athlete athlete : s.getAthletes()) {
athleteIds.add(athlete.getId());
}
dto.setAthleteIds(athleteIds);
// Map other fields as necessary
athleteSessions.add(dto);
}
@@ -300,5 +301,18 @@ public class AthleteResource {
return new ArrayList<>();
}
@GetMapping("/{athleteId}/groupes")
public List<String> getGroupesByAthlete(@PathVariable Integer athleteId) {
java.util.Optional<Athlete> athleteOptional = athleteDAO.findById(athleteId);
List<String> groupes = new ArrayList<>();
if (athleteOptional.isPresent()) {
Athlete athlete = athleteOptional.get();
for (Session session : athlete.getSessions()) {
if (!groupes.contains(session.getGroupe())) {
groupes.add(session.getGroupe());
}
}
}
return groupes;
}
}

View File

@@ -80,6 +80,17 @@ public class SessionResource {
}
}
@GetMapping("{id}/groupe")
@PreAuthorize("hasRole('coach') or hasRole('athlete')")
public ResponseEntity<?> getGroupeById(@PathVariable Integer id) {
try {
Session session = sessionDAO.findById(id).orElseThrow();
return ResponseEntity.ok(session.getGroupe());
} catch (Exception ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
}
}
@DeleteMapping("/delete/{id}")
@ResponseBody
@PreAuthorize("hasRole('coach')")
@@ -114,38 +125,34 @@ public class SessionResource {
return ResponseEntity.noContent().build();
}
@PutMapping("/{id}/subscribe/{userId}")
@PreAuthorize("hasRole('admin') or hasRole('coach') or hasRole('athlete')")
public ResponseEntity<Void> subscribe(@PathVariable Integer id,@PathVariable Integer userId){
public ResponseEntity<Void> subscribe(@PathVariable Integer id, @PathVariable Integer userId) {
Session session = sessionDAO.findById(id).orElseThrow(() -> new ResponseStatusException(
HttpStatus.NOT_FOUND, "Session not found with id " + id));
Athlete athlete = athleteDAO.findById(userId).orElseThrow(() -> new ResponseStatusException(
HttpStatus.NOT_FOUND, "Athlete not found with id " + userId));
session.getAthletes().add(athlete);
sessionDAO.save(session);
return ResponseEntity.noContent().build();
}
@PutMapping("/{id}/unsubscribe/{userId}")
@PreAuthorize("hasRole('admin') or hasRole('coach') or hasRole('athlete')")
public ResponseEntity<Void> unsubscribe(@PathVariable Integer id,@PathVariable Integer userId){
public ResponseEntity<Void> unsubscribe(@PathVariable Integer id, @PathVariable Integer userId) {
Session session = sessionDAO.findById(id).orElseThrow(() -> new ResponseStatusException(
HttpStatus.NOT_FOUND, "Session not found with id " + id));
Athlete athlete = athleteDAO.findById(userId).orElseThrow(() -> new ResponseStatusException(
HttpStatus.NOT_FOUND, "Athlete not found with id " + userId));
session.getAthletes().remove(athlete);
sessionDAO.save(session);
return ResponseEntity.noContent().build();
}
@GetMapping("/{id}/activities")
@PreAuthorize("hasRole('coach') or hasRole('athlete')")
public ResponseEntity<?> getActivitiesBySessionId(@PathVariable Integer id) {

View File

@@ -50,7 +50,8 @@ export const athleteService = {
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}`)
addActivity: (id_sess: number, id_act: number) => api.get(`/${id_sess}/activities/add/${id_act}`),
getGroupes: (athleteId: number | string) => api.get(`/athlete/${athleteId}/groupes`),
};
export const activiteService = {
@@ -73,6 +74,7 @@ export const sessionService = {
getActivities: (sessionId: number | null) => api.get<ActiviteDTO[]>(`/session/${sessionId}/activities`),
addActivity: (sessionId: number | null, activityId: number) => api.post(`/session/${sessionId}/activities/${activityId}`),
getGroupe: (sessionId: number | null) => api.get(`/session/${sessionId}/groupe`),
subscribe: (sessionId: number | null, userId: number) => api.put(`/session/${sessionId}/subscribe/${userId}`),
unsubscribe: (sessionId: number | null, userId: number) => api.put(`/session/${sessionId}/unsubscribe/${userId}`),
};