Continuing refactoring

This commit is contained in:
Alexis Leboeuf
2026-01-07 17:09:37 +01:00
parent 816b1e3965
commit c8d1407bcc
4 changed files with 38 additions and 14 deletions

View File

@@ -32,7 +32,7 @@ public class Session {
private String groupe;
@ManyToOne
private User coach; // un coach par session
private Coach coach; // un coach par session
@ManyToMany
private List<Athlete> athletes = new ArrayList<>(); // plusieurs athlètes par session
@@ -56,4 +56,21 @@ public class Session {
public String toString() {
return "Session [id=" + id + " , name=" + name + "]";
}
public void setCoach(Coach coach) {
if (coach.getRole() != Role.COACH) {
throw new IllegalArgumentException("L'utilisateur n'est pas un coach");
}
this.coach = coach;
}
public void setAthletes(List<Athlete> athletes) {
for (Athlete athlete : athletes) {
if (athlete.getRole() != Role.ATHLETE) {
throw new IllegalArgumentException("L'utilisateur n'est pas un athlète");
}
}
this.athletes = athletes;
}
}

View File

@@ -0,0 +1,10 @@
package hackathon.FrisbYEE.jpa.service;
import hackathon.FrisbYEE.jpa.metier.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserDAO extends JpaRepository<User, Integer> {
}

View File

@@ -47,7 +47,7 @@ public class AthleteResource {
@PostMapping("/create")
@PreAuthorize("hasRole('Admin')") // Only admin can create??
public ResponseEntity<AthleteDTO> create(@RequestBody AthleteDTO dto) {
Athlete athlete = mapToEntity(dto);
User athlete = mapToEntity(dto);
athleteDAO.save(athlete);
return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(athlete));
}

View File

@@ -3,12 +3,12 @@ package hackathon.FrisbYEE.rest;
import hackathon.FrisbYEE.jpa.dto.SessionDTO;
import hackathon.FrisbYEE.jpa.metier.Activite;
import hackathon.FrisbYEE.jpa.metier.Athlete;
import hackathon.FrisbYEE.jpa.metier.Coach;
import hackathon.FrisbYEE.jpa.metier.Session;
import hackathon.FrisbYEE.jpa.metier.User;
import hackathon.FrisbYEE.jpa.service.ActiviteDAO;
import hackathon.FrisbYEE.jpa.service.AthleteDAO;
import hackathon.FrisbYEE.jpa.service.CoachDAO;
import hackathon.FrisbYEE.jpa.service.SessionDAO;
import hackathon.FrisbYEE.jpa.service.UserDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -29,10 +29,7 @@ public class SessionResource {
private SessionDAO sessionDAO;
@Autowired
private CoachDAO coachDAO;
@Autowired
private AthleteDAO athleteDAO;
private UserDAO userDAO;
@Autowired
private ActiviteDAO activiteDAO;
@@ -43,7 +40,7 @@ public class SessionResource {
public ResponseEntity<?> create(@RequestBody SessionDTO dto) {
try {
Session session = maptoEntity(dto);
session.setCoach(coachDAO.findById(dto.getCoachId()).orElse(null));
session.setCoach(userDAO.findById(dto.getCoachId()).orElse(null));
sessionDAO.save(session);
return ResponseEntity.status(HttpStatus.CREATED).body(maptoDTO(session));
} catch (Exception ex) {
@@ -96,7 +93,7 @@ public class SessionResource {
session.setDuree(dto.getDuree());
}
if (dto.getAthleteIds() != null) {
List<Athlete> athletes = athleteDAO.findAllById(dto.getAthleteIds());
List<User> athletes = userDAO.findAllById(dto.getAthleteIds());
session.setAthletes(athletes);
}
if (dto.getActiviteIds() != null) {
@@ -122,7 +119,7 @@ public class SessionResource {
// Athletes
if (s.getAthletes() != null) {
List<Integer> athleteIds = new ArrayList<>();
for (Athlete athlete : s.getAthletes()) {
for (User athlete : s.getAthletes()) {
athleteIds.add(athlete.getId());
}
dto.setAthleteIds(athleteIds);
@@ -148,13 +145,13 @@ public class SessionResource {
session.setGroupe(dto.getGroupe());
// Coach
if (dto.getCoachId() != null) {
Coach coach = new Coach();
User coach = new User();
coach.setId(dto.getCoachId());
session.setCoach(coach);
}
// Athletes
if (dto.getAthleteIds() != null) {
List<Athlete> athletes = athleteDAO.findAllById(dto.getAthleteIds());
List<Athlete> athletes = userDAO.findAllById(dto.getAthleteIds());
session.setAthletes(athletes);
}
// Activites