Continuing refactoring
This commit is contained in:
@@ -32,7 +32,7 @@ public class Session {
|
|||||||
private String groupe;
|
private String groupe;
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
private User coach; // un coach par session
|
private Coach coach; // un coach par session
|
||||||
|
|
||||||
@ManyToMany
|
@ManyToMany
|
||||||
private List<Athlete> athletes = new ArrayList<>(); // plusieurs athlètes par session
|
private List<Athlete> athletes = new ArrayList<>(); // plusieurs athlètes par session
|
||||||
@@ -56,4 +56,21 @@ public class Session {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "Session [id=" + id + " , name=" + name + "]";
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -47,7 +47,7 @@ public class AthleteResource {
|
|||||||
@PostMapping("/create")
|
@PostMapping("/create")
|
||||||
@PreAuthorize("hasRole('Admin')") // Only admin can create??
|
@PreAuthorize("hasRole('Admin')") // Only admin can create??
|
||||||
public ResponseEntity<AthleteDTO> create(@RequestBody AthleteDTO dto) {
|
public ResponseEntity<AthleteDTO> create(@RequestBody AthleteDTO dto) {
|
||||||
Athlete athlete = mapToEntity(dto);
|
User athlete = mapToEntity(dto);
|
||||||
athleteDAO.save(athlete);
|
athleteDAO.save(athlete);
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(athlete));
|
return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(athlete));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,12 +3,12 @@ package hackathon.FrisbYEE.rest;
|
|||||||
import hackathon.FrisbYEE.jpa.dto.SessionDTO;
|
import hackathon.FrisbYEE.jpa.dto.SessionDTO;
|
||||||
import hackathon.FrisbYEE.jpa.metier.Activite;
|
import hackathon.FrisbYEE.jpa.metier.Activite;
|
||||||
import hackathon.FrisbYEE.jpa.metier.Athlete;
|
import hackathon.FrisbYEE.jpa.metier.Athlete;
|
||||||
import hackathon.FrisbYEE.jpa.metier.Coach;
|
|
||||||
import hackathon.FrisbYEE.jpa.metier.Session;
|
import hackathon.FrisbYEE.jpa.metier.Session;
|
||||||
|
import hackathon.FrisbYEE.jpa.metier.User;
|
||||||
import hackathon.FrisbYEE.jpa.service.ActiviteDAO;
|
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.SessionDAO;
|
||||||
|
import hackathon.FrisbYEE.jpa.service.UserDAO;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@@ -29,10 +29,7 @@ public class SessionResource {
|
|||||||
private SessionDAO sessionDAO;
|
private SessionDAO sessionDAO;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CoachDAO coachDAO;
|
private UserDAO userDAO;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private AthleteDAO athleteDAO;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ActiviteDAO activiteDAO;
|
private ActiviteDAO activiteDAO;
|
||||||
@@ -43,7 +40,7 @@ public class SessionResource {
|
|||||||
public ResponseEntity<?> create(@RequestBody SessionDTO dto) {
|
public ResponseEntity<?> create(@RequestBody SessionDTO dto) {
|
||||||
try {
|
try {
|
||||||
Session session = maptoEntity(dto);
|
Session session = maptoEntity(dto);
|
||||||
session.setCoach(coachDAO.findById(dto.getCoachId()).orElse(null));
|
session.setCoach(userDAO.findById(dto.getCoachId()).orElse(null));
|
||||||
sessionDAO.save(session);
|
sessionDAO.save(session);
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).body(maptoDTO(session));
|
return ResponseEntity.status(HttpStatus.CREATED).body(maptoDTO(session));
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
@@ -96,7 +93,7 @@ public class SessionResource {
|
|||||||
session.setDuree(dto.getDuree());
|
session.setDuree(dto.getDuree());
|
||||||
}
|
}
|
||||||
if (dto.getAthleteIds() != null) {
|
if (dto.getAthleteIds() != null) {
|
||||||
List<Athlete> athletes = athleteDAO.findAllById(dto.getAthleteIds());
|
List<User> athletes = userDAO.findAllById(dto.getAthleteIds());
|
||||||
session.setAthletes(athletes);
|
session.setAthletes(athletes);
|
||||||
}
|
}
|
||||||
if (dto.getActiviteIds() != null) {
|
if (dto.getActiviteIds() != null) {
|
||||||
@@ -122,7 +119,7 @@ public class SessionResource {
|
|||||||
// Athletes
|
// Athletes
|
||||||
if (s.getAthletes() != null) {
|
if (s.getAthletes() != null) {
|
||||||
List<Integer> athleteIds = new ArrayList<>();
|
List<Integer> athleteIds = new ArrayList<>();
|
||||||
for (Athlete athlete : s.getAthletes()) {
|
for (User athlete : s.getAthletes()) {
|
||||||
athleteIds.add(athlete.getId());
|
athleteIds.add(athlete.getId());
|
||||||
}
|
}
|
||||||
dto.setAthleteIds(athleteIds);
|
dto.setAthleteIds(athleteIds);
|
||||||
@@ -148,13 +145,13 @@ public class SessionResource {
|
|||||||
session.setGroupe(dto.getGroupe());
|
session.setGroupe(dto.getGroupe());
|
||||||
// Coach
|
// Coach
|
||||||
if (dto.getCoachId() != null) {
|
if (dto.getCoachId() != null) {
|
||||||
Coach coach = new Coach();
|
User coach = new User();
|
||||||
coach.setId(dto.getCoachId());
|
coach.setId(dto.getCoachId());
|
||||||
session.setCoach(coach);
|
session.setCoach(coach);
|
||||||
}
|
}
|
||||||
// Athletes
|
// Athletes
|
||||||
if (dto.getAthleteIds() != null) {
|
if (dto.getAthleteIds() != null) {
|
||||||
List<Athlete> athletes = athleteDAO.findAllById(dto.getAthleteIds());
|
List<Athlete> athletes = userDAO.findAllById(dto.getAthleteIds());
|
||||||
session.setAthletes(athletes);
|
session.setAthletes(athletes);
|
||||||
}
|
}
|
||||||
// Activites
|
// Activites
|
||||||
|
|||||||
Reference in New Issue
Block a user