subscribe + unsubscribe + edt

This commit is contained in:
trochas
2026-01-10 16:50:53 +01:00
parent c720bc93ff
commit 9aeef08e65
13 changed files with 142 additions and 84 deletions

View File

@@ -40,6 +40,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses;
public class AthleteResource {
@Autowired
private AthleteDAO athleteDAO;
@Autowired
private SessionDAO sessionDAO;
@Operation(summary = "Crée un Athlète avec les informations fournies")
@@ -161,7 +162,7 @@ public class AthleteResource {
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Récupération effectuée", content = @Content(mediaType = "application/json", schema = @Schema(implementation = SessionDTO.class)))
})
@GetMapping("/athlete/{id}/session")
@GetMapping("/athlete/{athleteId}/session")
public List<SessionDTO> getSessionsAthlete(@PathVariable Integer athleteId) {
// return pet
System.out.println("ID A CHERCHER" + athleteId);
@@ -171,7 +172,25 @@ public class AthleteResource {
for (Session s : sessions) {
if (s.getAthletes().contains(j.get())) {
SessionDTO dto = new SessionDTO();
dto.setId(s.getId());
dto.setName(s.getName());
dto.setCreneau(s.getCreneau());
List<Integer> activiteIDs = new ArrayList<>();
for (Activite activite : s.getActivites()) {
activiteIDs.add(activite.getId());
}
dto.setActiviteIds(activiteIDs);
dto.setCoachId(s.getCoach().getId());
dto.setDuree(s.getDuree());
dto.setGroupe(s.getGroupe());
dto.setIsRecurrent(s.getIsRecurrent());
List<Integer> athleteIds = new ArrayList<>();
for (Athlete athlete : s.getAthletes()) {
athleteIds.add(athlete.getId());
}
dto.setAthleteIds(athleteIds);
// Map other fields as necessary
athleteSessions.add(dto);
}

View File

@@ -114,6 +114,38 @@ 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){
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){
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) {