Feat: Fin de ActiviteResource + ajout id dans les DTO (erreur de ma part)
This commit is contained in:
@@ -6,6 +6,7 @@ import java.util.List;
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class ActiviteDTO {
|
public class ActiviteDTO {
|
||||||
|
private Integer id;
|
||||||
private String name;
|
private String name;
|
||||||
private String theme;
|
private String theme;
|
||||||
private Long duree; // optional, can be null
|
private Long duree; // optional, can be null
|
||||||
|
|||||||
@@ -3,5 +3,6 @@ import lombok.Data;
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class AdminDTO {
|
public class AdminDTO {
|
||||||
|
private Integer id;
|
||||||
private String name;
|
private String name;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import java.util.List;
|
|||||||
@Data
|
@Data
|
||||||
public class AthleteDTO {
|
public class AthleteDTO {
|
||||||
|
|
||||||
|
private Integer id;
|
||||||
private String nom;
|
private String nom;
|
||||||
private String niveau;
|
private String niveau;
|
||||||
private String categorie;
|
private String categorie;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import lombok.Data;
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class CoachDTO {
|
public class CoachDTO {
|
||||||
|
private Integer id;
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import lombok.Data;
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class UserDTO {
|
public class UserDTO {
|
||||||
|
private Integer id;
|
||||||
private String name;
|
private String name;
|
||||||
private String email;
|
private String email;
|
||||||
private Role role;
|
private Role role;
|
||||||
|
|||||||
@@ -6,9 +6,13 @@ import hackathon.FrisbYEE.jpa.metier.Session;
|
|||||||
import hackathon.FrisbYEE.jpa.service.ActiviteDAO;
|
import hackathon.FrisbYEE.jpa.service.ActiviteDAO;
|
||||||
import hackathon.FrisbYEE.jpa.service.SessionDAO;
|
import hackathon.FrisbYEE.jpa.service.SessionDAO;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/activite")
|
@RequestMapping("/activite")
|
||||||
@@ -21,15 +25,15 @@ public class ActiviteResource {
|
|||||||
private SessionDAO sessionDAO;
|
private SessionDAO sessionDAO;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
POST /activite/create
|
* POST /activite/create
|
||||||
DELETE /activite/delete/{id}
|
* DELETE /activite/delete/{id}
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@PostMapping("/create")
|
@PostMapping("/create")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@PreAuthorize("hasRole('Coach')")
|
@PreAuthorize("hasRole('Coach')")
|
||||||
public String create(@RequestBody ActiviteDTO dto) {
|
public ResponseEntity<String> create(@RequestBody ActiviteDTO dto) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Session session = sessionDAO.findById(dto.getSessionId()).get();
|
Session session = sessionDAO.findById(dto.getSessionId()).get();
|
||||||
@@ -41,21 +45,83 @@ public class ActiviteResource {
|
|||||||
activite.setSession(session);
|
activite.setSession(session);
|
||||||
activiteDAO.save(activite);
|
activiteDAO.save(activite);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
return ex.toString();
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error: " + ex.getMessage());
|
||||||
}
|
}
|
||||||
return "ok";
|
return ResponseEntity.status(HttpStatus.CREATED).body("Activity created");
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/delete/{id}")
|
@DeleteMapping("/delete/{id}")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@PreAuthorize("hasRole('Coach')")
|
@PreAuthorize("hasRole('Coach')")
|
||||||
public String delete(@PathVariable("id") int id) {
|
public ResponseEntity<String> delete(@PathVariable("id") int id) {
|
||||||
try {
|
try {
|
||||||
Activite activite = activiteDAO.findById(id).get();
|
Activite activite = activiteDAO.findById(id).get();
|
||||||
activiteDAO.delete(activite);
|
activiteDAO.delete(activite);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
return ex.toString();
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error: " + ex.getMessage());
|
||||||
|
|
||||||
}
|
}
|
||||||
return "ok";
|
return ResponseEntity.ok("Activity deleted");
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/update/{id}")
|
||||||
|
@ResponseBody
|
||||||
|
@PreAuthorize("hasRole('Coach')")
|
||||||
|
public ResponseEntity<String> modifyById(@PathVariable("id") int id, @RequestBody ActiviteDTO dto) {
|
||||||
|
try {
|
||||||
|
Session session = sessionDAO.findById(dto.getSessionId()).get();
|
||||||
|
Activite activite = activiteDAO.findById(id).get();
|
||||||
|
activite.setName(dto.getName());
|
||||||
|
activite.setTheme(dto.getTheme());
|
||||||
|
activite.setDuree(dto.getDuree() != null ? dto.getDuree() : 0L);
|
||||||
|
activite.setDataActivite(dto.getDataActivite());
|
||||||
|
activite.setSession(session);
|
||||||
|
activiteDAO.save(activite);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error: " + ex.getMessage());
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok("Activity modified");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/read/{id}")
|
||||||
|
@ResponseBody
|
||||||
|
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);
|
||||||
|
|
||||||
|
return ResponseEntity.ok(dto);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/all")
|
||||||
|
@ResponseBody
|
||||||
|
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());
|
||||||
|
return ResponseEntity.ok(dtos);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user