mapToDTO and mapToEntity
This commit is contained in:
@@ -49,7 +49,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webmvc</artifactId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.thymeleaf.extras</groupId>
|
||||
@@ -97,6 +97,11 @@
|
||||
<artifactId>spring-boot-starter-thymeleaf-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
|
||||
@@ -37,11 +37,7 @@ public class ActiviteResource {
|
||||
|
||||
try {
|
||||
Session session = sessionDAO.findById(dto.getSessionId()).get();
|
||||
Activite activite = new Activite();
|
||||
activite.setName(dto.getName());
|
||||
activite.setTheme(dto.getTheme());
|
||||
activite.setDuree(dto.getDuree() != null ? dto.getDuree() : 0L);
|
||||
activite.setDataActivite(dto.getDataActivite());
|
||||
Activite activite = mapToEntity(dto);
|
||||
activite.setSession(session);
|
||||
activiteDAO.save(activite);
|
||||
} catch (Exception ex) {
|
||||
@@ -90,14 +86,7 @@ public class ActiviteResource {
|
||||
public ResponseEntity<ActiviteDTO> getActivityById(@PathVariable("id") int id) {
|
||||
try {
|
||||
Activite activite = activiteDAO.findById(id).get();
|
||||
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);
|
||||
|
||||
ActiviteDTO dto = mapToDTO(activite);
|
||||
return ResponseEntity.ok(dto);
|
||||
} catch (Exception ex) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
||||
@@ -110,16 +99,7 @@ public class ActiviteResource {
|
||||
public ResponseEntity<List<ActiviteDTO>> getAllActivity() {
|
||||
try {
|
||||
List<Activite> activites = activiteDAO.findAll();
|
||||
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());
|
||||
List<ActiviteDTO> dtos = activites.stream().map(this::mapToDTO).collect(Collectors.toList());
|
||||
return ResponseEntity.ok(dtos);
|
||||
} catch (Exception ex) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
||||
@@ -133,16 +113,7 @@ public class ActiviteResource {
|
||||
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());
|
||||
List<ActiviteDTO> dtos = activites.stream().map(this::mapToDTO).collect(Collectors.toList());
|
||||
return ResponseEntity.ok(dtos);
|
||||
} catch (Exception ex) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,10 +37,8 @@ public class AthleteResource {
|
||||
@PostMapping("/create")
|
||||
@PreAuthorize("hasRole('Admin')") // Only admin can create??
|
||||
public ResponseEntity<AthleteDTO> create(@RequestBody AthleteDTO dto) {
|
||||
Athlete athlete = new Athlete();
|
||||
athlete.setName(dto.getName());
|
||||
athlete.setCategorie(dto.getCategorie());
|
||||
athlete.setNiveau(dto.getNiveau());
|
||||
Athlete athlete = mapToEntity(dto);
|
||||
athleteDAO.save(athlete);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(mapToDTO(athlete));
|
||||
}
|
||||
|
||||
@@ -108,6 +106,16 @@ public class AthleteResource {
|
||||
dto.setNiveau(athlete.getNiveau());
|
||||
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")
|
||||
public List<SessionDTO> getSessionsAthlete(@PathVariable Integer athleteId) {
|
||||
// return pet
|
||||
|
||||
@@ -20,8 +20,7 @@ public class CoachResource {
|
||||
@PostMapping("/create")
|
||||
@PreAuthorize("hasRole('Admin')") // Only admin can create
|
||||
public ResponseEntity<CoachDTO> create(@RequestBody CoachDTO dto) {
|
||||
Coach coach = new Coach();
|
||||
coach.setName(dto.getName());
|
||||
Coach coach = mapToEntity(dto);
|
||||
coachDAO.save(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"));
|
||||
if (dto.getName() != null) coach.setName(dto.getName());
|
||||
coachDAO.save(coach);
|
||||
return ResponseEntity.ok(mapToDTO(coach));
|
||||
CoachDTO updatedDto = mapToDTO(coach);
|
||||
return ResponseEntity.ok(updatedDto);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
@@ -70,5 +70,12 @@ public class CoachResource {
|
||||
dto.setName(coach.getName());
|
||||
return dto;
|
||||
}
|
||||
|
||||
private Coach mapToEntity(CoachDTO dto) {
|
||||
Coach coach = new Coach();
|
||||
coach.setId(dto.getId());
|
||||
coach.setName(dto.getName());
|
||||
return coach;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -40,16 +40,7 @@ public class SessionResource {
|
||||
@PreAuthorize("hasRole('Coach')")
|
||||
public ResponseEntity<?> create(@RequestBody SessionDTO dto) {
|
||||
try {
|
||||
List<Athlete> athletes = athleteDAO.findAllById(dto.getAthleteIds());
|
||||
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);
|
||||
Session session = maptoEntity(dto);
|
||||
sessionDAO.save(session);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(maptoDTO(session));
|
||||
} catch (Exception ex) {
|
||||
@@ -143,4 +134,31 @@ public class SessionResource {
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,27 @@
|
||||
package hackathon.FrisbYEE;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.web.servlet.client.RestTestClient;
|
||||
|
||||
@SpringBootTest
|
||||
class FrisbYeeApplicationTests {
|
||||
import hackathon.FrisbYEE.rest.AthleteResource;
|
||||
|
||||
@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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user