push activite athlete coach

This commit is contained in:
tuanvu
2026-01-06 09:39:09 +01:00
parent 4de8e2da22
commit 9494bb3458
6 changed files with 186 additions and 29 deletions

View File

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

View File

@@ -1,16 +1,16 @@
package hackathon.FrisbYEE.jpa.dto; package hackathon.FrisbYEE.jpa.dto;
import lombok.Data; import lombok.Data;
import java.util.List; import java.util.List;
@Data @Data
public class AthleteDTO { public class AthleteDTO {
private Integer id; private Integer id;
private String nom; private String name;
private String niveau;
private String categorie; 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; import jakarta.persistence.Entity;
@Entity @Entity
@Getter @Setter @NoArgsConstructor @Data @NoArgsConstructor
@Access(AccessType.FIELD) @Access(AccessType.FIELD)
public class Athlete { public class Athlete {

View File

@@ -3,33 +3,93 @@ package hackathon.FrisbYEE.rest;
import org.apache.el.stream.Optional; import org.apache.el.stream.Optional;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import hackathon.FrisbYEE.jpa.dto.AthleteDTO; import hackathon.FrisbYEE.jpa.dto.AthleteDTO;
import hackathon.FrisbYEE.jpa.metier.Athlete; import hackathon.FrisbYEE.jpa.metier.Athlete;
import hackathon.FrisbYEE.jpa.service.AthleteDAO; import hackathon.FrisbYEE.jpa.service.AthleteDAO;
@RestController
@RequestMapping("/athletes")
public class AthleteResource { public class AthleteResource {
@Autowired
private AthleteDAO athleteDAO; private AthleteDAO athleteDAO;
@Operation(summary = "Récupère tous les utilisateurs") @PostMapping("/create")
@ApiResponses(value = { @PreAuthorize("hasRole('Admin')") // Only admin can create??
@ApiResponse(responseCode = "200", description = "Récupère le Joueur ayant l'identifiant correspondant", public ResponseEntity<AthleteDTO> create(@RequestBody AthleteDTO dto) {
content = @Content(mediaType = "application/json", Athlete ahtlete = new Athlete();
schema = @Schema(implementation = AthleteDTO.class))) athlete.setName(dto.getName())
}) athlete.setCategorie(dto.getCategorie())
@GetMapping("/joueur/{id}") athlete.setNiveau(dto.getNiveau())
public AthleteDTO getJoueurById(@PathVariable Integer joueurId) { return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(athlete));
// return pet }
System.out.println("ID A CHERCHER" + joueurId);
java.util.Optional<Athlete> j = athleteDAO.findById(joueurId); @PostMapping("/all")
AthleteDTO jDTO = new AthleteDTO(); @PreAuthorize("hasRole('Admin') or hasRole('Coach') or hasRole('Athlete')")
System.out.println(j); public ReponseEntity<List<AthleteDTO>> all() {
return jDTO; 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;
} }
} }

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.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -32,6 +32,9 @@ public class SessionResource {
@Autowired @Autowired
private AthleteDAO athleteDAO; private AthleteDAO athleteDAO;
@Autowired
private ActiviteDAO activiteDAO;
@PostMapping("/create") @PostMapping("/create")
@ResponseBody @ResponseBody
@PreAuthorize("hasRole('Coach')") @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) { private SessionDTO maptoDTO(Session s) {
SessionDTO dto = new SessionDTO(); SessionDTO dto = new SessionDTO();
dto.setId(s.getId()); dto.setId(s.getId());