Merge branch 'jpa' of https://gitlab2.istic.univ-rennes1.fr/tuvu/hackathon into jpa
This commit is contained in:
@@ -12,5 +12,4 @@ public class ActiviteDTO {
|
||||
private Long duree; // optional, can be null
|
||||
private List<String> dataActivite;
|
||||
private Integer sessionId;
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import jakarta.persistence.ElementCollection;
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@Getter @Setter @NoArgsConstructor
|
||||
@Data @NoArgsConstructor
|
||||
@Access(AccessType.FIELD)
|
||||
|
||||
public class Athlete {
|
||||
|
||||
@@ -3,33 +3,93 @@ package hackathon.FrisbYEE.rest;
|
||||
import org.apache.el.stream.Optional;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
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.metier.Athlete;
|
||||
import hackathon.FrisbYEE.jpa.service.AthleteDAO;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/athletes")
|
||||
public class AthleteResource {
|
||||
|
||||
@Autowired
|
||||
private AthleteDAO athleteDAO;
|
||||
|
||||
@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("/joueur/{id}")
|
||||
public AthleteDTO getJoueurById(@PathVariable Integer joueurId) {
|
||||
// return pet
|
||||
System.out.println("ID A CHERCHER" + joueurId);
|
||||
java.util.Optional<Athlete> j = athleteDAO.findById(joueurId);
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user