Feat: Ajout méthode sur ActiviteRessource

This commit is contained in:
Amaël Kesteman
2026-01-06 10:07:34 +01:00
parent 4de8e2da22
commit 741d01bcd2
2 changed files with 30 additions and 1 deletions

View File

@@ -3,8 +3,11 @@ package hackathon.FrisbYEE.jpa.service;
import hackathon.FrisbYEE.jpa.metier.Activite;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ActiviteDAO extends JpaRepository<Activite, Integer> {
List<Activite> findByTheme(String theme);
}

View File

@@ -84,7 +84,8 @@ public class ActiviteResource {
}
@GetMapping("/read/{id}")
@GetMapping("/{id}")
@PreAuthorize("hasRole('Coach') or hasRole('Athlete')")
@ResponseBody
public ResponseEntity<ActiviteDTO> getActivityById(@PathVariable("id") int id) {
try {
@@ -104,6 +105,7 @@ public class ActiviteResource {
}
@GetMapping("/all")
@PreAuthorize("hasRole('Coach') or hasRole('Athlete')")
@ResponseBody
public ResponseEntity<List<ActiviteDTO>> getAllActivity() {
try {
@@ -124,4 +126,28 @@ public class ActiviteResource {
}
}
@GetMapping("/theme/{theme}")
@PreAuthorize("hasRole('Coach') or hasRole('Athlete')")
@ResponseBody
public ResponseEntity<List<ActiviteDTO>> getActivityByTheme(@PathVariable("theme") String theme) {
try {
List<Activite> activites = activiteDAO.findByTheme(theme);
List<ActiviteDTO> dtos = activites.stream().map(activite -> {
ActiviteDTO dto = new ActiviteDTO();
dto.setName(activite.getName());
dto.setId(activite.getId());
dto.setTheme(activite.getTheme());
dto.setDuree(activite.getDuree());
dto.setDataActivite(activite.getDataActivite());
dto.setSessionId(activite.getSession() != null ? activite.getSession().getId() : null);
return dto;
}).collect(Collectors.toList());
return ResponseEntity.ok(dtos);
} catch (Exception ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
}
}