Finished refactoring classes, DTOs, endpoints I guess ?
This commit is contained in:
@@ -3,6 +3,8 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class AdminDTO {
|
||||
private String id_keycloak;
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String name;
|
||||
private String prenom;
|
||||
}
|
||||
|
||||
@@ -7,12 +7,12 @@ import java.util.List;
|
||||
|
||||
@Data
|
||||
public class AthleteDTO {
|
||||
private Integer id;
|
||||
private String id_keycloak;
|
||||
private String name;
|
||||
private String prenom;
|
||||
private String categorie;
|
||||
private String niveau;
|
||||
private List<String> groupes = new ArrayList<>();
|
||||
private List<Integer> sessionIds = new ArrayList<>();
|
||||
private Integer userId;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package hackathon.FrisbYEE.jpa.dto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class CoachDTO {
|
||||
private Integer id;
|
||||
private String id_keycloak;
|
||||
private String name;
|
||||
|
||||
private String prenom;
|
||||
private List<Integer> sessionIds;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import java.util.List;
|
||||
@Data
|
||||
public class SessionDTO {
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
private Boolean isRecurrent;
|
||||
private LocalDateTime creneau;
|
||||
|
||||
@@ -5,8 +5,9 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UserDTO {
|
||||
private Integer id;
|
||||
private String id_keycloak;
|
||||
private String name;
|
||||
private String prenom;
|
||||
private String email;
|
||||
private Role role;
|
||||
}
|
||||
|
||||
@@ -16,9 +16,10 @@ import jakarta.persistence.Entity;
|
||||
|
||||
public class Admin extends User{
|
||||
|
||||
public Admin(String name){
|
||||
super(name);
|
||||
public Admin(String id_keycloak, String name, String prenom){
|
||||
super(name, id_keycloak, prenom, Role.ADMIN );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Admin [id=" + super.getId() + " , name=" + super.getName() + "]";
|
||||
|
||||
@@ -28,15 +28,8 @@ public class Athlete extends User{
|
||||
@ManyToMany(mappedBy = "athletes")
|
||||
private List<Session> sessions = new ArrayList<>(); // plusieurs sessions sont possibles
|
||||
|
||||
public Athlete(String name){
|
||||
super(name);
|
||||
}
|
||||
|
||||
public Athlete(String name, String categorie, String niveau, List<String> groupe){
|
||||
super(name);
|
||||
this.categorie = categorie;
|
||||
this.niveau = niveau;
|
||||
this.groupe = groupe;
|
||||
public Athlete(String name, String id_keycloak, String prenom){
|
||||
super(name, id_keycloak, prenom, Role.ATHLETE);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -22,9 +22,10 @@ public class Coach extends User{
|
||||
@OneToMany(mappedBy = "coach")
|
||||
private List<Session> sessions = new ArrayList<>(); // Un coach peut avoir plusieurs sessions
|
||||
|
||||
public Coach(String name){
|
||||
super(name);
|
||||
public Coach(String name, String id_keycloak, String prenom){
|
||||
super(name, id_keycloak, prenom, Role.COACH );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Coach [id=" + super.getId() + " , name=" + super.getName() + "]";
|
||||
|
||||
@@ -30,33 +30,18 @@ public class User implements Serializable {
|
||||
private Integer id;
|
||||
@Column(nullable = false, unique = true)
|
||||
private String id_keycloak;
|
||||
|
||||
|
||||
private String name;
|
||||
@Column(nullable = false)
|
||||
private String motDePasse;
|
||||
private String email;
|
||||
private String prenom;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(nullable = false)
|
||||
private Role role;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private Coach coach;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private Athlete athlete;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private Admin admin;
|
||||
|
||||
public User(String name) {
|
||||
public User(String name, String id_keycloak, String prenom, Role role) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public User(String name, String motDePasse, String email, Role role) {
|
||||
this.name = name;
|
||||
this.motDePasse = motDePasse;
|
||||
this.email = email;
|
||||
this.id_keycloak = id_keycloak;
|
||||
this.prenom = prenom;
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ public class AthleteResource {
|
||||
@PostMapping("/create")
|
||||
@PreAuthorize("hasRole('Admin')") // Only admin can create??
|
||||
public ResponseEntity<AthleteDTO> create(@RequestBody AthleteDTO dto) {
|
||||
User athlete = mapToEntity(dto);
|
||||
Athlete athlete = mapToEntity(dto);
|
||||
athleteDAO.save(athlete);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(athlete));
|
||||
}
|
||||
@@ -126,7 +126,7 @@ public class AthleteResource {
|
||||
|
||||
private AthleteDTO mapToDTO(Athlete athlete) {
|
||||
AthleteDTO dto = new AthleteDTO();
|
||||
dto.setId(athlete.getId());
|
||||
dto.setId_keycloak(athlete.getId_keycloak());
|
||||
dto.setName(athlete.getName());
|
||||
dto.setCategorie(athlete.getCategorie());
|
||||
dto.setNiveau(athlete.getNiveau());
|
||||
@@ -135,10 +135,6 @@ public class AthleteResource {
|
||||
|
||||
private Athlete mapToEntity(AthleteDTO dto) {
|
||||
Athlete athlete = new Athlete();
|
||||
athlete.setId(dto.getId());
|
||||
athlete.setName(dto.getName());
|
||||
athlete.setCategorie(dto.getCategorie());
|
||||
athlete.setNiveau(dto.getNiveau());
|
||||
return athlete;
|
||||
}
|
||||
|
||||
@@ -156,7 +152,6 @@ 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());
|
||||
// Map other fields as necessary
|
||||
athleteSessions.add(dto);
|
||||
@@ -177,7 +172,6 @@ public class AthleteResource {
|
||||
List<SessionDTO> sessionDTOs = new ArrayList<>();
|
||||
for (Session session : sessions) {
|
||||
SessionDTO dto = new SessionDTO();
|
||||
dto.setId(session.getId());
|
||||
dto.setName(session.getName());
|
||||
// Map other fields as necessary
|
||||
sessionDTOs.add(dto);
|
||||
@@ -229,7 +223,6 @@ public class AthleteResource {
|
||||
// sympa les
|
||||
// dates
|
||||
SessionDTO dto = new SessionDTO();
|
||||
dto.setId(session.getId());
|
||||
dto.setName(session.getName());
|
||||
// Map other fields as necessary
|
||||
filteredSessions.add(dto);
|
||||
@@ -259,7 +252,6 @@ public class AthleteResource {
|
||||
&& session.getCreneau().isAfter(ChronoLocalDateTime.from(LocalDate.parse(startDate)))
|
||||
&& session.getCreneau().isBefore(ChronoLocalDateTime.from(LocalDate.parse(endDate)))) {
|
||||
SessionDTO dto = new SessionDTO();
|
||||
dto.setId(session.getId());
|
||||
dto.setName(session.getName());
|
||||
// Map other fields as necessary
|
||||
filteredSessions.add(dto);
|
||||
|
||||
@@ -7,7 +7,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
@@ -71,14 +70,14 @@ public class CoachResource {
|
||||
|
||||
private CoachDTO mapToDTO(Coach coach) {
|
||||
CoachDTO dto = new CoachDTO();
|
||||
dto.setId(coach.getId());
|
||||
dto.setId_keycloak(coach.getId_keycloak());
|
||||
dto.setName(coach.getName());
|
||||
return dto;
|
||||
}
|
||||
|
||||
private Coach mapToEntity(CoachDTO dto) {
|
||||
Coach coach = new Coach();
|
||||
coach.setId(dto.getId());
|
||||
coach.setId_keycloak(dto.getId_keycloak());
|
||||
coach.setName(dto.getName());
|
||||
return coach;
|
||||
}
|
||||
|
||||
@@ -3,11 +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;
|
||||
@@ -29,7 +30,10 @@ public class SessionResource {
|
||||
private SessionDAO sessionDAO;
|
||||
|
||||
@Autowired
|
||||
private UserDAO userDAO;
|
||||
private CoachDAO coachDAO;
|
||||
|
||||
@Autowired
|
||||
private AthleteDAO athleteDAO;
|
||||
|
||||
@Autowired
|
||||
private ActiviteDAO activiteDAO;
|
||||
@@ -40,7 +44,7 @@ public class SessionResource {
|
||||
public ResponseEntity<?> create(@RequestBody SessionDTO dto) {
|
||||
try {
|
||||
Session session = maptoEntity(dto);
|
||||
session.setCoach(userDAO.findById(dto.getCoachId()).orElse(null));
|
||||
session.setCoach(coachDAO.findById(dto.getCoachId()).orElse(null));
|
||||
sessionDAO.save(session);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(maptoDTO(session));
|
||||
} catch (Exception ex) {
|
||||
@@ -93,7 +97,7 @@ public class SessionResource {
|
||||
session.setDuree(dto.getDuree());
|
||||
}
|
||||
if (dto.getAthleteIds() != null) {
|
||||
List<User> athletes = userDAO.findAllById(dto.getAthleteIds());
|
||||
List<Athlete> athletes = athleteDAO.findAllById(dto.getAthleteIds());
|
||||
session.setAthletes(athletes);
|
||||
}
|
||||
if (dto.getActiviteIds() != null) {
|
||||
@@ -106,7 +110,6 @@ public class SessionResource {
|
||||
|
||||
private SessionDTO maptoDTO(Session s) {
|
||||
SessionDTO dto = new SessionDTO();
|
||||
dto.setId(s.getId());
|
||||
dto.setName(s.getName());
|
||||
dto.setIsRecurrent(s.getIsRecurrent());
|
||||
dto.setCreneau(s.getCreneau());
|
||||
@@ -119,7 +122,7 @@ public class SessionResource {
|
||||
// Athletes
|
||||
if (s.getAthletes() != null) {
|
||||
List<Integer> athleteIds = new ArrayList<>();
|
||||
for (User athlete : s.getAthletes()) {
|
||||
for (Athlete athlete : s.getAthletes()) {
|
||||
athleteIds.add(athlete.getId());
|
||||
}
|
||||
dto.setAthleteIds(athleteIds);
|
||||
@@ -137,7 +140,6 @@ public class SessionResource {
|
||||
|
||||
private Session maptoEntity(SessionDTO dto) {
|
||||
Session session = new Session();
|
||||
session.setId(dto.getId());
|
||||
session.setName(dto.getName());
|
||||
session.setIsRecurrent(dto.getIsRecurrent());
|
||||
session.setCreneau(dto.getCreneau());
|
||||
@@ -145,13 +147,13 @@ public class SessionResource {
|
||||
session.setGroupe(dto.getGroupe());
|
||||
// Coach
|
||||
if (dto.getCoachId() != null) {
|
||||
User coach = new User();
|
||||
Coach coach = new Coach();
|
||||
coach.setId(dto.getCoachId());
|
||||
session.setCoach(coach);
|
||||
}
|
||||
// Athletes
|
||||
if (dto.getAthleteIds() != null) {
|
||||
List<Athlete> athletes = userDAO.findAllById(dto.getAthleteIds());
|
||||
List<Athlete> athletes = athleteDAO.findAllById(dto.getAthleteIds());
|
||||
session.setAthletes(athletes);
|
||||
}
|
||||
// Activites
|
||||
|
||||
Reference in New Issue
Block a user