Merge remote-tracking branch 'refs/remotes/origin/jpa' into jpa

This commit is contained in:
Alexis Leboeuf
2026-01-06 10:14:01 +01:00
6 changed files with 186 additions and 23 deletions

View File

@@ -12,5 +12,4 @@ public class ActiviteDTO {
private Long duree; // optional, can be null
private List<String> dataActivite;
private Integer sessionId;
}

View File

@@ -1,16 +1,16 @@
package hackathon.FrisbYEE.jpa.dto;
import lombok.Data;
import java.util.List;
@Data
public class AthleteDTO {
private Integer id;
private String nom;
private String niveau;
private String name;
private String categorie;
private List<String> groupes;
private String niveau;
private List<String> groupes = new ArrayList<>();
private List<Integer> sessionIds = new ArrayList<>();
private Integer userId;
}

View File

@@ -15,7 +15,7 @@ import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
@Entity
@Getter @Setter @NoArgsConstructor
@Data @NoArgsConstructor
@Access(AccessType.FIELD)
public class Athlete {

View File

@@ -22,25 +22,91 @@ import hackathon.FrisbYEE.jpa.metier.Session;
import hackathon.FrisbYEE.jpa.service.AthleteDAO;
import hackathon.FrisbYEE.jpa.service.SessionDAO;
@RestController
@RequestMapping("/athletes")
public class AthleteResource {
@Autowired
private AthleteDAO athleteDAO;
private SessionDAO sessionDAO;
@Operation(summary = "Récupère tous les utilisateurs")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Récupère le Joueur ayant l'identifiant correspondant",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = AthleteDTO.class)))
})
@GetMapping("/athlete/{id}")
public AthleteDTO getAthleteById(@PathVariable Integer athleteId) {
// return pet
System.out.println("ID A CHERCHER" + athleteId);
java.util.Optional<Athlete> j = athleteDAO.findById(athleteId);
AthleteDTO jDTO = new AthleteDTO();
System.out.println(j);
return jDTO;
@PostMapping("/create")
@PreAuthorize("hasRole('Admin')") // Only admin can create??
public ResponseEntity<AthleteDTO> create(@RequestBody AthleteDTO dto) {
Athlete ahtlete = new Athlete();
athlete.setName(dto.getName())
athlete.setCategorie(dto.getCategorie())
athlete.setNiveau(dto.getNiveau())
return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(athlete));
}
@PostMapping("/all")
@PreAuthorize("hasRole('Admin') or hasRole('Coach') or hasRole('Athlete')")
public ReponseEntity<List<AthleteDTO>> all() {
List<Athlete> athletes = athleteDAO.findAll();
List<AthleteDTO> dtos = new ArrayList<>();
for (Athlete athlete : athletes) {
dtos.add(maptoDTO(athlete));
}
return ResponseEntity.ok(dtos);
}
@GetMapping("/{id}")
@PreAuthorize("hasRole('Admin') or hasRole('Coach') or hasRole('Athlete')")
public ResponseEntity<AthleteDTO> getById(@PathVariable Integer id) {
return athleteDAO.findById(id)
.map(athlete -> ResponseEntity.ok(mapToDTO(athlete)))
.orElse(ResponseEntity.notFound().build());
}
@PutMapping("/{id}")
@PreAuthorize("hasRole('ADMIN') or #id == principal.id")
public ResponseEntity<AthleteDTO> update(@PathVariable Integer id,@RequestBody AthleteDTO dto) {
try {
Athlete athlete = athleteDAO.findById(id).get();
athlete.setName(dto.getName());
athlete.setCategorie(dto.getCategorie());
athlete.setNiveau(dto.getNiveau());
// Optional
athlete.setDuree(dto.getDuree());
athlete.setTheme(dto.getTheme());
// List
if (dto.getDataActivite() != null) {
athlete.setDataActivite(dto.getDataActivite());
}
// Relationship: sessionId → session
if (dto.getSessionId() != null) {
Session session = sessionDAO.findById(dto.getSessionId())
.orElseThrow(() -> new RuntimeException("Session not found"));
athlete.setSession(session);
}
athleteDAO.save(athlete);
return ResponseEntity.ok(mapToDTO(athlete));
}catch (Exception ex){
return ResponseEntity.noContent().build();
}
}
@DeleteMapping("/{id}")
@PreAuthorize("hasRole('Admin')")
public ResponseEntity<Void> delete(@PathVariable Integer id) {
if (!athleteDAO.existsById(id)) {
return ResponseEntity.notFound().build();
}
athleteDAO.deleteById(id);
return ResponseEntity.noContent().build();
}
private AthleteDTO mapToDTO(Athlete athlete) {
AthleteDTO dto = new AthleteDTO();
dto.setId(athlete.getId());
dto.setName(athlete.getName());
dto.setCategorie(athlete.getCategorie());
dto.setNiveau(athlete.getNiveau());
return dto;
}
@GetMapping("/athlete/{id}/session")
public List<SessionDTO> getSessionsAthlete(@PathVariable Integer athleteId) {

View File

@@ -0,0 +1,74 @@
package hackathon.FrisbYEE.rest;
import hackathon.FrisbYEE.jpa.dto.CoachDTO;
import hackathon.FrisbYEE.jpa.metier.Coach;
import hackathon.FrisbYEE.jpa.service.CoachDAO;
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.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.util.ArrayList;
import java.util.List;
public class CoachResource {
@Autowired
private CoachDAO coachDAO;
@PostMapping("/create")
@PreAuthorize("hasRole('Admin')") // Only admin can create
public ResponseEntity<CoachDTO> create(@RequestBody CoachDTO dto) {
Coach coach = new Coach();
coach.setName(dto.getName());
coachDAO.save(coach);
return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(coach));
}
@GetMapping("/all")
@PreAuthorize("hasRole('Admin') or hasRole('Coach')")
public List<CoachDTO> getAll() {
List<Coach> coaches = coachDAO.findAll();
List<CoachDTO> dtos = new ArrayList<>();
for (Coach coach : coaches) {
dtos.add(mapToDTO(coach));
}
return dtos;
}
@GetMapping("/{id}")
@PreAuthorize("hasRole('Admin') or hasRole('Coach')")
public CoachDTO getById(@PathVariable Integer id) {
Coach coach = coachDAO.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Coach not found"));
return mapToDTO(coach);
}
@PutMapping("/update/{id}")
@PreAuthorize("hasRole('Admin')")
public ResponseEntity<CoachDTO> update(@PathVariable Integer id, @RequestBody CoachDTO dto) {
Coach coach = coachDAO.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Coach not found"));
if (dto.getName() != null) coach.setName(dto.getName());
coachDAO.save(coach);
return ResponseEntity.ok(mapToDTO(coach));
}
@DeleteMapping("/delete/{id}")
@PreAuthorize("hasRole('Admin')")
public ResponseEntity<Void> delete(@PathVariable Integer id) {
Coach coach = coachDAO.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Coach not found"));
coachDAO.delete(coach);
return ResponseEntity.noContent().build();
}
private CoachDTO mapToDTO(Coach coach) {
CoachDTO dto = new CoachDTO();
dto.setId(coach.getId());
dto.setName(coach.getName());
return dto;
}
}

View File

@@ -13,8 +13,8 @@ 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;
import java.util.ArrayList;
import java.util.List;
@@ -32,6 +32,9 @@ public class SessionResource {
@Autowired
private AthleteDAO athleteDAO;
@Autowired
private ActiviteDAO activiteDAO;
@PostMapping("/create")
@ResponseBody
@PreAuthorize("hasRole('Coach')")
@@ -89,6 +92,27 @@ public class SessionResource {
}
}
@PutMapping("/update/{id}")
@PreAuthorize("hasRole('Coach')")
public ResponseEntity<Void> updateSession(@PathVariable Integer id, @RequestBody SessionDTO dto) {
Session session = sessionDAO.findById(id).orElseThrow(() -> new ResponseStatusException(
HttpStatus.NOT_FOUND, "Session not found with id " + id));
if (dto.getDuree() != null) {
session.setDuree(dto.getDuree());
}
if (dto.getAthleteIds() != null) {
List<Athlete> athletes = athleteDAO.findAllById(dto.getAthleteIds());
session.setAthletes(athletes);
}
if (dto.getActiviteIds() != null) {
List<Activite> activites = activiteDAO.findAllById(dto.getActiviteIds());
session.setActivites(activites);
}
sessionDAO.save(session);
return ResponseEntity.noContent().build();
}
private SessionDTO maptoDTO(Session s) {
SessionDTO dto = new SessionDTO();
dto.setId(s.getId());