Addd user retrieval if exists on creation for Coach and Athlete

This commit is contained in:
Alexis Leboeuf
2026-01-09 08:27:52 +01:00
parent cf509d1a7c
commit dc814d4a7b
2 changed files with 6 additions and 0 deletions

View File

@@ -51,6 +51,9 @@ public class AthleteResource {
@PreAuthorize("hasRole('admin') or hasRole('coach') or hasRole('athlete')") @PreAuthorize("hasRole('admin') or hasRole('coach') or hasRole('athlete')")
public ResponseEntity<AthleteDTO> create(@RequestBody AthleteDTO dto) { public ResponseEntity<AthleteDTO> create(@RequestBody AthleteDTO dto) {
Athlete athlete = mapToEntity(dto); Athlete athlete = mapToEntity(dto);
if(athleteDAO.existsByKeycloakId(athlete.getKeycloakId())) {
return ResponseEntity.status(HttpStatus.CONFLICT).body(mapToDTO(athleteDAO.findByKeycloakId(athlete.getKeycloakId()).get()));
}
athleteDAO.save(athlete); athleteDAO.save(athlete);
return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(athlete)); return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(athlete));
} }

View File

@@ -24,6 +24,9 @@ public class CoachResource {
@PreAuthorize("hasRole('Admin')") // Only admin can create @PreAuthorize("hasRole('Admin')") // Only admin can create
public ResponseEntity<CoachDTO> create(@RequestBody CoachDTO dto) { public ResponseEntity<CoachDTO> create(@RequestBody CoachDTO dto) {
Coach coach = mapToEntity(dto); Coach coach = mapToEntity(dto);
if(coachDAO.existsByKeycloakId(coach.getKeycloakId())) {
return ResponseEntity.status(HttpStatus.CONFLICT).body(mapToDTO(coachDAO.findByKeycloakId(coach.getKeycloakId()).get()));
}
coachDAO.save(coach); coachDAO.save(coach);
return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(coach)); return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(coach));
} }