sessionDTO

This commit is contained in:
tuanvu
2026-01-05 15:51:03 +01:00
parent c6f8e552eb
commit f4b6698090
3 changed files with 107 additions and 43 deletions

View File

@@ -1,7 +1,10 @@
package hackathon.FrisbYEE.jpa.dto;
import lombok.Data;
import java.util.List;
@Data
public class ActiviteDTO {
private String name;
private String theme;
@@ -9,44 +12,4 @@ public class ActiviteDTO {
private List<String> dataActivite;
private Integer sessionId;
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTheme() {
return theme;
}
public void setTheme(String theme) {
this.theme = theme;
}
public Long getDuree() {
return duree;
}
public void setDuree(Long duree) {
this.duree = duree;
}
public List<String> getDataActivite() {
return dataActivite;
}
public void setDataActivite(List<String> dataActivite) {
this.dataActivite = dataActivite;
}
public Integer getSessionId() {
return sessionId;
}
public void setSessionId(Integer sessionId) {
this.sessionId = sessionId;
}
}

View File

@@ -0,0 +1,26 @@
package hackathon.FrisbYEE.jpa.dto;
import hackathon.FrisbYEE.jpa.metier.Activite;
import hackathon.FrisbYEE.jpa.metier.Athlete;
import hackathon.FrisbYEE.jpa.metier.Session;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@Data
public class SessionDTO {
private Integer id;
private String name;
private Boolean isRecurrent;
private LocalDateTime creneau;
private Long duree;
private String groupe;
private Integer coachId;
private List<Integer> athleteIds;
private List<Integer> activiteIds;
}

View File

@@ -1,14 +1,23 @@
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.service.ActiviteDAO;
import hackathon.FrisbYEE.jpa.service.AthleteDAO;
import hackathon.FrisbYEE.jpa.service.CoachDAO;
import hackathon.FrisbYEE.jpa.service.SessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Controller
@RequestMapping("/session")
@@ -16,20 +25,55 @@ public class SessionResource {
@Autowired
private SessionDAO sessionDAO;
@Autowired
private CoachDAO coachDAO;
@Autowired
private AthleteDAO athleteDAO;
@PostMapping("/create")
@ResponseBody
@PreAuthorize("hasRole('Coach')")
public String create(@PathVariable("id") int id) {
String sessionID = "";
public String create(@RequestBody SessionDTO dto) {
try {
List<Athlete> athletes = athleteDAO.findAllById(dto.getAthleteIds());
Coach coach = coachDAO.findById(dto.getCoachId()).get();
Session session = new Session();
session.setName(dto.getName());
session.setIsRecurrent(dto.getIsRecurrent());
session.setCreneau(dto.getCreneau());
session.setDuree(dto.getDuree());
session.setGroupe(dto.getGroupe());
session.setCoach(coach);
session.setAthletes(athletes);
} catch (Exception ex) {
return ex.toString();
}
return "ok";
}
@GetMapping("/all")
@PreAuthorize("hasRole('Coach') or hasRole('Athlete')")
public List<SessionDTO> getAll() {
List<Session> sessions = sessionDAO.findAll();
List<SessionDTO> dtos = new ArrayList<>();
for (Session session : sessions) {
dtos.add(maptoDTO(session));
}
return dtos;
}
@GetMapping("/{id}")
@PreAuthorize("hasRole('Coach') or hasRole('Athlete')")
public SessionDTO getById(@PathVariable Integer id) {
try {
Session session = sessionDAO.findById(id).orElseThrow();
return maptoDTO(session);
} catch (Exception ex) {
throw new RuntimeException("Session not found with id " + id, ex);
}
}
@DeleteMapping("/delete/{id}")
@ResponseBody
@PreAuthorize("hasRole('Coach')")
@@ -42,4 +86,35 @@ public class SessionResource {
}
return "ok";
}
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());
dto.setDuree(s.getDuree());
dto.setGroupe(s.getGroupe());
// Coach
if (s.getCoach() != null) {
dto.setCoachId(s.getCoach().getId());
}
// Athletes
if (s.getAthletes() != null) {
List<Integer> athleteIds = new ArrayList<>();
for (Athlete athlete : s.getAthletes()) {
athleteIds.add(athlete.getId());
}
dto.setAthleteIds(athleteIds);
}
// Activites
if (s.getActivites() != null) {
List<Integer> activiteIds = new ArrayList<>();
for (Activite activite : s.getActivites()) {
activiteIds.add(activite.getId());
}
dto.setActiviteIds(activiteIds);
}
return dto;
}
}