Feat: Stats athlètes fonctionnel

This commit is contained in:
Amaël Kesteman
2026-01-11 21:27:32 +01:00
parent b9e67589ed
commit cbd53ba471
6 changed files with 129 additions and 43 deletions

View File

@@ -1,10 +1,14 @@
package hackathon.FrisbYEE.jpa.service;
import hackathon.FrisbYEE.jpa.metier.Session;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface SessionDAO extends JpaRepository<Session, Integer> {
List<Session> findByAthletes_Id(Integer athleteId);
}

View File

@@ -4,6 +4,7 @@ import java.time.LocalDate;
import java.time.chrono.ChronoLocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@@ -164,41 +165,43 @@ public class AthleteResource {
})
@GetMapping("/athlete/{athleteId}/session")
public List<SessionDTO> getSessionsAthlete(@PathVariable Integer athleteId) {
// return pet
System.out.println("ID A CHERCHER" + athleteId);
java.util.Optional<Athlete> j = athleteDAO.findById(athleteId);
List<Session> sessions = sessionDAO.findAll();
List<SessionDTO> athleteSessions = new ArrayList<>();
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);
}
Optional<Athlete> athleteOpt = athleteDAO.findById(athleteId);
if (athleteOpt.isEmpty()) {
return new ArrayList<>();
}
System.out.println(j);
List<Session> sessions = sessionDAO.findByAthletes_Id(athleteId);
List<SessionDTO> athleteSessions = new ArrayList<>();
for (Session s : sessions) {
SessionDTO dto = new SessionDTO();
dto.setId(s.getId());
dto.setName(s.getName());
dto.setCreneau(s.getCreneau());
dto.setDuree(s.getDuree());
dto.setGroupe(s.getGroupe());
dto.setIsRecurrent(s.getIsRecurrent());
dto.setCoachId(s.getCoach() != null ? s.getCoach().getId() : null);
List<Integer> activiteIDs = new ArrayList<>();
for (Activite activite : s.getActivites()) {
activiteIDs.add(activite.getId());
}
dto.setActiviteIds(activiteIDs);
List<Integer> athleteIds = new ArrayList<>();
for (Athlete a : s.getAthletes()) {
athleteIds.add(a.getId());
}
dto.setAthleteIds(athleteIds);
athleteSessions.add(dto);
}
return athleteSessions;
}
@Operation(summary = "Récupère toutes les sessions")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Récupération effectuée", content = @Content(mediaType = "application/json", schema = @Schema(implementation = SessionDTO.class)))

View File

@@ -43,8 +43,12 @@ public class SessionResource {
@ResponseBody
@PreAuthorize("hasRole('coach')")
public ResponseEntity<?> create(@RequestBody SessionDTO dto) {
System.out.println("=== SESSION DTO RECEIVED ===");
System.out.println(dto);
System.out.println("Coach ID: " + dto.getCoachId());
System.out.println("ID null");
try {
if (sessionDAO.findById(dto.getId()).isPresent()) {
if (dto.getId() != null && sessionDAO.findById(dto.getId()).isPresent()) {
return ResponseEntity.status(HttpStatus.OK).body("Session with ID " + dto.getId() + " already exists.");
}
@@ -54,6 +58,7 @@ public class SessionResource {
sessionDAO.save(session);
return ResponseEntity.status(HttpStatus.CREATED).body(maptoDTO(session));
} catch (Exception ex) {
ex.printStackTrace();
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
}
}