Merge branch 'main' of https://gitlab2.istic.univ-rennes1.fr/tuvu/hackathon
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
package hackathon.FrisbYEE.jpa.dto;
|
||||
import hackathon.FrisbYEE.jpa.metier.Role;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class AdminDTO {
|
||||
private String id_keycloak;
|
||||
private Integer id;
|
||||
private String id_keycloak;
|
||||
private String name;
|
||||
private String prenom;
|
||||
private Role role;
|
||||
}
|
||||
|
||||
@@ -3,17 +3,12 @@ package hackathon.FrisbYEE.rest;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import hackathon.FrisbYEE.jpa.dto.AdminDTO;
|
||||
import hackathon.FrisbYEE.jpa.metier.Admin;
|
||||
import hackathon.FrisbYEE.jpa.service.AdminDAO;
|
||||
import io.swagger.v3.oas.annotations.parameters.RequestBody;
|
||||
import hackathon.FrisbYEE.jpa.service.UserDAO;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/admin")
|
||||
@@ -22,12 +17,24 @@ public class AdminResource {
|
||||
|
||||
@Autowired
|
||||
private AdminDAO adminDAO;
|
||||
@Autowired
|
||||
private UserDAO userDAO;
|
||||
|
||||
|
||||
@PostMapping("/create")
|
||||
@PreAuthorize("hasRole('Admin')") // Only admin can create
|
||||
@PreAuthorize("hasRole('admin')") // Only admin can create
|
||||
public ResponseEntity<AdminDTO> create(@RequestBody AdminDTO dto) {
|
||||
|
||||
userDAO.findByKeycloakId(dto.getId_keycloak())
|
||||
.ifPresent(existing -> {
|
||||
if (!(existing instanceof Admin)) {
|
||||
userDAO.delete(existing);
|
||||
userDAO.flush();
|
||||
}
|
||||
});
|
||||
|
||||
Admin admin = mapToEntity(dto);
|
||||
|
||||
if(adminDAO.findByKeycloakId(admin.getKeycloakId()).isPresent()) {
|
||||
return ResponseEntity.status(200).body(mapToDTO(adminDAO.findByKeycloakId(admin.getKeycloakId()).get()));
|
||||
}
|
||||
@@ -55,7 +62,6 @@ public class AdminResource {
|
||||
dto.setId_keycloak(admin.getKeycloakId());
|
||||
dto.setName(admin.getName());
|
||||
dto.setPrenom(admin.getPrenom());
|
||||
dto.setRole(admin.getRole());
|
||||
|
||||
return dto;
|
||||
}
|
||||
@@ -65,7 +71,7 @@ public class AdminResource {
|
||||
admin.setKeycloakId(dto.getId_keycloak());
|
||||
admin.setName(dto.getName());
|
||||
admin.setPrenom(dto.getPrenom());
|
||||
admin.setRole(dto.getRole());
|
||||
admin.setRole(hackathon.FrisbYEE.jpa.metier.Role.admin);
|
||||
|
||||
return admin;
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ public class AthleteResource {
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "Récupération effectuée", content = @Content(mediaType = "application/json", schema = @Schema(implementation = SessionDTO.class)))
|
||||
})
|
||||
@GetMapping("/athlete/{athleteId}/session")
|
||||
@GetMapping("/{athleteId}/session")
|
||||
public List<SessionDTO> getSessionsAthlete(@PathVariable Integer athleteId) {
|
||||
Optional<Athlete> athleteOpt = athleteDAO.findById(athleteId);
|
||||
if (athleteOpt.isEmpty()) {
|
||||
@@ -174,30 +174,32 @@ public class AthleteResource {
|
||||
List<SessionDTO> athleteSessions = new ArrayList<>();
|
||||
|
||||
for (Session s : sessions) {
|
||||
SessionDTO dto = new SessionDTO();
|
||||
dto.setId(s.getId());
|
||||
dto.setName(s.getName());
|
||||
dto.setCreneau(s.getCreneau());
|
||||
dto.setDuree(s.getDuree());
|
||||
dto.setGroupe(s.getGroupe());
|
||||
dto.setIsRecurrent(s.getIsRecurrent());
|
||||
dto.setCoachId(s.getCoach() != null ? s.getCoach().getId() : null);
|
||||
if (s.getAthletes().contains(athlete.get())) {
|
||||
SessionDTO dto = new SessionDTO();
|
||||
|
||||
List<Integer> activiteIDs = new ArrayList<>();
|
||||
for (Activite activite : s.getActivites()) {
|
||||
activiteIDs.add(activite.getId());
|
||||
dto.setId(s.getId());
|
||||
dto.setName(s.getName());
|
||||
dto.setCreneau(s.getCreneau());
|
||||
List<Integer> activiteIDs = new ArrayList<>();
|
||||
for (Activite activite : s.getActivites()) {
|
||||
activiteIDs.add(activite.getId());
|
||||
}
|
||||
dto.setActiviteIds(activiteIDs);
|
||||
dto.setCoachId(s.getCoach().getId());
|
||||
dto.setDuree(s.getDuree());
|
||||
dto.setGroupe(s.getGroupe());
|
||||
dto.setIsRecurrent(s.getIsRecurrent());
|
||||
List<Integer> athleteIds = new ArrayList<>();
|
||||
for (Athlete athlete2 : s.getAthletes()) {
|
||||
athleteIds.add(athlete2.getId());
|
||||
}
|
||||
dto.setAthleteIds(athleteIds);
|
||||
|
||||
// Map other fields as necessary
|
||||
athleteSessions.add(dto);
|
||||
}
|
||||
dto.setActiviteIds(activiteIDs);
|
||||
|
||||
List<Integer> athleteIds = new ArrayList<>();
|
||||
for (Athlete a : s.getAthletes()) {
|
||||
athleteIds.add(a.getId());
|
||||
}
|
||||
dto.setAthleteIds(athleteIds);
|
||||
|
||||
athleteSessions.add(dto);
|
||||
}
|
||||
|
||||
System.out.println(athlete);
|
||||
return athleteSessions;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
package hackathon.FrisbYEE.rest;
|
||||
|
||||
import hackathon.FrisbYEE.jpa.dto.CoachDTO;
|
||||
import hackathon.FrisbYEE.jpa.dto.SessionDTO;
|
||||
import hackathon.FrisbYEE.jpa.metier.Activite;
|
||||
import hackathon.FrisbYEE.jpa.metier.Admin;
|
||||
import hackathon.FrisbYEE.jpa.metier.Athlete;
|
||||
import hackathon.FrisbYEE.jpa.metier.Coach;
|
||||
import hackathon.FrisbYEE.jpa.metier.Session;
|
||||
import hackathon.FrisbYEE.jpa.service.CoachDAO;
|
||||
import hackathon.FrisbYEE.jpa.service.SessionDAO;
|
||||
import hackathon.FrisbYEE.jpa.service.UserDAO;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -17,13 +25,28 @@ import java.util.List;
|
||||
@RestController
|
||||
@RequestMapping("/coach")
|
||||
public class CoachResource {
|
||||
|
||||
@Autowired
|
||||
private CoachDAO coachDAO;
|
||||
@Autowired
|
||||
private UserDAO userDAO;
|
||||
@Autowired
|
||||
private SessionDAO sessionDAO;
|
||||
|
||||
@PostMapping("/create")
|
||||
@PreAuthorize("hasRole('Admin')") // Only admin can create
|
||||
@PreAuthorize("hasRole('admin') or hasRole('coach')") // Only admin can create
|
||||
public ResponseEntity<CoachDTO> create(@RequestBody CoachDTO dto) {
|
||||
|
||||
userDAO.findByKeycloakId(dto.getId_keycloak())
|
||||
.ifPresent(existing -> {
|
||||
if (!(existing instanceof Coach)) {
|
||||
userDAO.delete(existing);
|
||||
userDAO.flush();
|
||||
}
|
||||
});
|
||||
|
||||
Coach coach = mapToEntity(dto);
|
||||
|
||||
if(coachDAO.existsByKeycloakId(coach.getKeycloakId())) {
|
||||
return ResponseEntity.status(200).body(mapToDTO(coachDAO.findByKeycloakId(coach.getKeycloakId()).get()));
|
||||
}
|
||||
@@ -72,11 +95,38 @@ public class CoachResource {
|
||||
|
||||
@GetMapping("/{id}/session")
|
||||
@PreAuthorize("hasRole('Admin') or hasRole('Coach')")
|
||||
public ResponseEntity<List<?>> getSessionsForCoach(@PathVariable Integer id) {
|
||||
public ResponseEntity<List<SessionDTO>> getSessionsForCoach(@PathVariable Integer id) {
|
||||
Coach coach = coachDAO.findById(id)
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Coach not found"));
|
||||
List<?> sessions = coach.getSessions();
|
||||
return ResponseEntity.ok(sessions);
|
||||
List<Session> sessions = sessionDAO.findAll();
|
||||
List<SessionDTO> coachSessions = new ArrayList<>();
|
||||
for (Session s : sessions) {
|
||||
if (s.getCoach().equals(coach)) {
|
||||
SessionDTO dto = new SessionDTO();
|
||||
|
||||
dto.setId(s.getId());
|
||||
dto.setName(s.getName());
|
||||
dto.setCreneau(s.getCreneau());
|
||||
List<Integer> activiteIDs = new ArrayList<>();
|
||||
for (Activite activite : s.getActivites()) {
|
||||
activiteIDs.add(activite.getId());
|
||||
}
|
||||
dto.setActiviteIds(activiteIDs);
|
||||
dto.setCoachId(s.getCoach().getId());
|
||||
dto.setDuree(s.getDuree());
|
||||
dto.setGroupe(s.getGroupe());
|
||||
dto.setIsRecurrent(s.getIsRecurrent());
|
||||
List<Integer> athleteIds = new ArrayList<>();
|
||||
for (Athlete athlete : s.getAthletes()) {
|
||||
athleteIds.add(athlete.getId());
|
||||
}
|
||||
dto.setAthleteIds(athleteIds);
|
||||
|
||||
// Map other fields as necessary
|
||||
coachSessions.add(dto);
|
||||
}
|
||||
}
|
||||
return ResponseEntity.ok(coachSessions);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package hackathon.FrisbYEE.rest;
|
||||
|
||||
import hackathon.FrisbYEE.jpa.dto.ActiviteDTO;
|
||||
import hackathon.FrisbYEE.jpa.dto.CoachDTO;
|
||||
import hackathon.FrisbYEE.jpa.dto.SessionDTO;
|
||||
import hackathon.FrisbYEE.jpa.metier.Activite;
|
||||
import hackathon.FrisbYEE.jpa.metier.Athlete;
|
||||
@@ -19,6 +20,7 @@ import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -74,6 +76,30 @@ public class SessionResource {
|
||||
return ResponseEntity.ok(dtos);
|
||||
}
|
||||
|
||||
@GetMapping("/all-between-dates")
|
||||
@PreAuthorize("hasRole('admin') or hasRole('coach') or hasRole('athlete')")
|
||||
public ResponseEntity<List<SessionDTO>> getAllBetweenDates(
|
||||
@RequestParam LocalDate startDate,
|
||||
@RequestParam LocalDate endDate
|
||||
) {
|
||||
List<Session> sessions = sessionDAO.findAll();
|
||||
List<SessionDTO> dtos = new ArrayList<>();
|
||||
System.out.println("date : " + startDate + " " + endDate);
|
||||
for (Session session : sessions) {
|
||||
LocalDate sessionDate = session.getCreneau().toLocalDate();
|
||||
|
||||
boolean isBetween =
|
||||
(!sessionDate.isBefore(startDate) || session.getIsRecurrent()) &&
|
||||
!sessionDate.isAfter(endDate);
|
||||
|
||||
if (isBetween) {
|
||||
dtos.add(maptoDTO(session));
|
||||
}
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(dtos);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@PreAuthorize("hasRole('coach') or hasRole('athlete')")
|
||||
public ResponseEntity<?> getById(@PathVariable Integer id) {
|
||||
@@ -150,6 +176,29 @@ public class SessionResource {
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/coach")
|
||||
@PreAuthorize("hasRole('coach') or hasRole('athlete')")
|
||||
public ResponseEntity<CoachDTO> getCoachBySessionId(@PathVariable Integer id) {
|
||||
try {
|
||||
Session session = sessionDAO.findById(id).orElseThrow();
|
||||
Coach coach = session.getCoach();
|
||||
|
||||
CoachDTO dto = new CoachDTO();
|
||||
dto.setId(coach.getId());
|
||||
dto.setName(coach.getName());
|
||||
dto.setPrenom(coach.getPrenom());
|
||||
List<Integer> listSession = new ArrayList<Integer>();
|
||||
for (Session s : coach.getSessions()) {
|
||||
listSession.add(session.getId());
|
||||
}
|
||||
dto.setSessionIds(listSession);
|
||||
|
||||
return ResponseEntity.ok(dto);
|
||||
} catch (Exception ex) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new CoachDTO());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/{id}/activities")
|
||||
@PreAuthorize("hasRole('coach') or hasRole('athlete')")
|
||||
|
||||
Reference in New Issue
Block a user