|
|
|
|
@@ -6,9 +6,13 @@ import hackathon.FrisbYEE.jpa.metier.Session;
|
|
|
|
|
import hackathon.FrisbYEE.jpa.service.ActiviteDAO;
|
|
|
|
|
import hackathon.FrisbYEE.jpa.service.SessionDAO;
|
|
|
|
|
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.stereotype.Controller;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Controller
|
|
|
|
|
@RequestMapping("/activite")
|
|
|
|
|
@@ -21,15 +25,15 @@ public class ActiviteResource {
|
|
|
|
|
private SessionDAO sessionDAO;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
POST /activite/create
|
|
|
|
|
DELETE /activite/delete/{id}
|
|
|
|
|
|
|
|
|
|
* POST /activite/create
|
|
|
|
|
* DELETE /activite/delete/{id}
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
@PostMapping("/create")
|
|
|
|
|
@ResponseBody
|
|
|
|
|
@PreAuthorize("hasRole('Coach')")
|
|
|
|
|
public String create(@RequestBody ActiviteDTO dto) {
|
|
|
|
|
public ResponseEntity<String> create(@RequestBody ActiviteDTO dto) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Session session = sessionDAO.findById(dto.getSessionId()).get();
|
|
|
|
|
@@ -41,21 +45,83 @@ public class ActiviteResource {
|
|
|
|
|
activite.setSession(session);
|
|
|
|
|
activiteDAO.save(activite);
|
|
|
|
|
} 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}")
|
|
|
|
|
@ResponseBody
|
|
|
|
|
@PreAuthorize("hasRole('Coach')")
|
|
|
|
|
public String delete(@PathVariable("id") int id) {
|
|
|
|
|
public ResponseEntity<String> delete(@PathVariable("id") int id) {
|
|
|
|
|
try {
|
|
|
|
|
Activite activite = activiteDAO.findById(id).get();
|
|
|
|
|
activiteDAO.delete(activite);
|
|
|
|
|
} 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|