Merge branch 'jpa'

This commit is contained in:
tuanvu
2026-01-06 15:54:27 +01:00
6 changed files with 99 additions and 56 deletions

View File

@@ -49,7 +49,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.thymeleaf.extras</groupId> <groupId>org.thymeleaf.extras</groupId>
@@ -97,6 +97,11 @@
<artifactId>spring-boot-starter-thymeleaf-test</artifactId> <artifactId>spring-boot-starter-thymeleaf-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>

View File

@@ -37,11 +37,7 @@ public class ActiviteResource {
try { try {
Session session = sessionDAO.findById(dto.getSessionId()).get(); Session session = sessionDAO.findById(dto.getSessionId()).get();
Activite activite = new Activite(); Activite activite = mapToEntity(dto);
activite.setName(dto.getName());
activite.setTheme(dto.getTheme());
activite.setDuree(dto.getDuree() != null ? dto.getDuree() : 0L);
activite.setDataActivite(dto.getDataActivite());
activite.setSession(session); activite.setSession(session);
activiteDAO.save(activite); activiteDAO.save(activite);
} catch (Exception ex) { } catch (Exception ex) {
@@ -90,14 +86,7 @@ public class ActiviteResource {
public ResponseEntity<ActiviteDTO> getActivityById(@PathVariable("id") int id) { public ResponseEntity<ActiviteDTO> getActivityById(@PathVariable("id") int id) {
try { try {
Activite activite = activiteDAO.findById(id).get(); Activite activite = activiteDAO.findById(id).get();
ActiviteDTO dto = new ActiviteDTO(); ActiviteDTO dto = mapToDTO(activite);
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 ResponseEntity.ok(dto); return ResponseEntity.ok(dto);
} catch (Exception ex) { } catch (Exception ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
@@ -110,16 +99,7 @@ public class ActiviteResource {
public ResponseEntity<List<ActiviteDTO>> getAllActivity() { public ResponseEntity<List<ActiviteDTO>> getAllActivity() {
try { try {
List<Activite> activites = activiteDAO.findAll(); List<Activite> activites = activiteDAO.findAll();
List<ActiviteDTO> dtos = activites.stream().map(activite -> { List<ActiviteDTO> dtos = activites.stream().map(this::mapToDTO).collect(Collectors.toList());
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); return ResponseEntity.ok(dtos);
} catch (Exception ex) { } catch (Exception ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
@@ -133,16 +113,7 @@ public class ActiviteResource {
public ResponseEntity<List<ActiviteDTO>> getActivityByTheme(@PathVariable("theme") String theme) { public ResponseEntity<List<ActiviteDTO>> getActivityByTheme(@PathVariable("theme") String theme) {
try { try {
List<Activite> activites = activiteDAO.findByTheme(theme); List<Activite> activites = activiteDAO.findByTheme(theme);
List<ActiviteDTO> dtos = activites.stream().map(activite -> { List<ActiviteDTO> dtos = activites.stream().map(this::mapToDTO).collect(Collectors.toList());
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); return ResponseEntity.ok(dtos);
} catch (Exception ex) { } catch (Exception ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
@@ -150,4 +121,24 @@ public class ActiviteResource {
} }
private Activite mapToEntity(ActiviteDTO dto) {
Activite activite = new Activite();
//ID géré par Postgre ?
activite.setName(dto.getName());
activite.setTheme(dto.getTheme());
activite.setDuree(dto.getDuree());
activite.setDataActivite(dto.getDataActivite());
return activite;
}
private ActiviteDTO mapToDTO(Activite activite) {
ActiviteDTO dto = new ActiviteDTO();
dto.setName(activite.getName());
dto.setTheme(activite.getTheme());
dto.setDuree(activite.getDuree());
dto.setDataActivite(activite.getDataActivite());
dto.setSessionId(activite.getSession() != null ? activite.getSession().getId() : null);
return dto;
}
} }

View File

@@ -37,10 +37,8 @@ public class AthleteResource {
@PostMapping("/create") @PostMapping("/create")
@PreAuthorize("hasRole('Admin')") // Only admin can create?? @PreAuthorize("hasRole('Admin')") // Only admin can create??
public ResponseEntity<AthleteDTO> create(@RequestBody AthleteDTO dto) { public ResponseEntity<AthleteDTO> create(@RequestBody AthleteDTO dto) {
Athlete athlete = new Athlete(); Athlete athlete = mapToEntity(dto);
athlete.setName(dto.getName()); athleteDAO.save(athlete);
athlete.setCategorie(dto.getCategorie());
athlete.setNiveau(dto.getNiveau());
return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(athlete)); return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(athlete));
} }
@@ -108,6 +106,16 @@ public class AthleteResource {
dto.setNiveau(athlete.getNiveau()); dto.setNiveau(athlete.getNiveau());
return dto; return dto;
} }
private Athlete mapToEntity(AthleteDTO dto) {
Athlete athlete = new Athlete();
athlete.setId(dto.getId());
athlete.setName(dto.getName());
athlete.setCategorie(dto.getCategorie());
athlete.setNiveau(dto.getNiveau());
return athlete;
}
@GetMapping("/athlete/{id}/session") @GetMapping("/athlete/{id}/session")
public List<SessionDTO> getSessionsAthlete(@PathVariable Integer athleteId) { public List<SessionDTO> getSessionsAthlete(@PathVariable Integer athleteId) {
// return pet // return pet

View File

@@ -20,8 +20,7 @@ public class CoachResource {
@PostMapping("/create") @PostMapping("/create")
@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 = new Coach(); Coach coach = mapToEntity(dto);
coach.setName(dto.getName());
coachDAO.save(coach); coachDAO.save(coach);
return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(coach)); return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(coach));
} }
@@ -52,7 +51,8 @@ public class CoachResource {
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Coach not found")); .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Coach not found"));
if (dto.getName() != null) coach.setName(dto.getName()); if (dto.getName() != null) coach.setName(dto.getName());
coachDAO.save(coach); coachDAO.save(coach);
return ResponseEntity.ok(mapToDTO(coach)); CoachDTO updatedDto = mapToDTO(coach);
return ResponseEntity.ok(updatedDto);
} }
@DeleteMapping("/delete/{id}") @DeleteMapping("/delete/{id}")
@@ -70,5 +70,12 @@ public class CoachResource {
dto.setName(coach.getName()); dto.setName(coach.getName());
return dto; return dto;
} }
private Coach mapToEntity(CoachDTO dto) {
Coach coach = new Coach();
coach.setId(dto.getId());
coach.setName(dto.getName());
return coach;
}
} }

View File

@@ -40,16 +40,7 @@ public class SessionResource {
@PreAuthorize("hasRole('Coach')") @PreAuthorize("hasRole('Coach')")
public ResponseEntity<?> create(@RequestBody SessionDTO dto) { public ResponseEntity<?> create(@RequestBody SessionDTO dto) {
try { try {
List<Athlete> athletes = athleteDAO.findAllById(dto.getAthleteIds()); Session session = maptoEntity(dto);
Coach coach = coachDAO.findById(dto.getCoachId()).get();
Session session = new Session();
session.setName(dto.getName());
session.setIsRecurrent(dto.getIsRecurrent());
session.setCreneau(dto.getCreneau());
session.setDuree(dto.getDuree());
session.setGroupe(dto.getGroupe());
session.setCoach(coach);
session.setAthletes(athletes);
sessionDAO.save(session); sessionDAO.save(session);
return ResponseEntity.status(HttpStatus.CREATED).body(maptoDTO(session)); return ResponseEntity.status(HttpStatus.CREATED).body(maptoDTO(session));
} catch (Exception ex) { } catch (Exception ex) {
@@ -143,4 +134,31 @@ public class SessionResource {
} }
return dto; return dto;
} }
private Session maptoEntity(SessionDTO dto) {
Session session = new Session();
session.setId(dto.getId());
session.setName(dto.getName());
session.setIsRecurrent(dto.getIsRecurrent());
session.setCreneau(dto.getCreneau());
session.setDuree(dto.getDuree());
session.setGroupe(dto.getGroupe());
// Coach
if (dto.getCoachId() != null) {
Coach coach = new Coach();
coach.setId(dto.getCoachId());
session.setCoach(coach);
}
// Athletes
if (dto.getAthleteIds() != null) {
List<Athlete> athletes = athleteDAO.findAllById(dto.getAthleteIds());
session.setAthletes(athletes);
}
// Activites
if (dto.getActiviteIds() != null) {
List<Activite> activites = activiteDAO.findAllById(dto.getActiviteIds());
session.setActivites(activites);
}
return session;
}
} }

View File

@@ -1,13 +1,27 @@
package hackathon.FrisbYEE; package hackathon.FrisbYEE;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.servlet.client.RestTestClient;
@SpringBootTest import hackathon.FrisbYEE.rest.AthleteResource;
class FrisbYeeApplicationTests {
@Test
void contextLoads() { class FrisbYEEApplicationTests {
//Controller
private AthleteResource athleteResource;
private RestTestClient mockMvc;
@BeforeEach
void setUp() {
athleteResource = new AthleteResource();
mockMvc = RestTestClient.bindToController(athleteResource).build();
} }
@Test
void shouldReturnUsers() throws Exception {
//mockMvc.perform(get("/api/users"))
// .andExpect(status().isOk());
}
} }