add reponseentity
This commit is contained in:
@@ -10,15 +10,16 @@ import hackathon.FrisbYEE.jpa.service.AthleteDAO;
|
||||
import hackathon.FrisbYEE.jpa.service.CoachDAO;
|
||||
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.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Controller
|
||||
@RestController
|
||||
@RequestMapping("/session")
|
||||
|
||||
public class SessionResource {
|
||||
@@ -34,7 +35,7 @@ public class SessionResource {
|
||||
@PostMapping("/create")
|
||||
@ResponseBody
|
||||
@PreAuthorize("hasRole('Coach')")
|
||||
public String create(@RequestBody SessionDTO dto) {
|
||||
public ResponseEntity<?> create(@RequestBody SessionDTO dto) {
|
||||
try {
|
||||
List<Athlete> athletes = athleteDAO.findAllById(dto.getAthleteIds());
|
||||
Coach coach = coachDAO.findById(dto.getCoachId()).get();
|
||||
@@ -46,45 +47,46 @@ public class SessionResource {
|
||||
session.setGroupe(dto.getGroupe());
|
||||
session.setCoach(coach);
|
||||
session.setAthletes(athletes);
|
||||
sessionDAO.save(session);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(maptoDTO(session));
|
||||
} catch (Exception ex) {
|
||||
return ex.toString();
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
|
||||
}
|
||||
return "ok";
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
@PreAuthorize("hasRole('Coach') or hasRole('Athlete')")
|
||||
public List<SessionDTO> getAll() {
|
||||
public ResponseEntity<List<SessionDTO>> getAll() {
|
||||
List<Session> sessions = sessionDAO.findAll();
|
||||
List<SessionDTO> dtos = new ArrayList<>();
|
||||
for (Session session : sessions) {
|
||||
dtos.add(maptoDTO(session));
|
||||
}
|
||||
return dtos;
|
||||
return ResponseEntity.ok(dtos);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@PreAuthorize("hasRole('Coach') or hasRole('Athlete')")
|
||||
public SessionDTO getById(@PathVariable Integer id) {
|
||||
public ResponseEntity<?> getById(@PathVariable Integer id) {
|
||||
try {
|
||||
Session session = sessionDAO.findById(id).orElseThrow();
|
||||
return maptoDTO(session);
|
||||
return ResponseEntity.ok(maptoDTO(session));
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException("Session not found with id " + id, ex);
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
@ResponseBody
|
||||
@PreAuthorize("hasRole('Coach')")
|
||||
public String delete(@PathVariable("id") int id) {
|
||||
public ResponseEntity<String> delete(@PathVariable("id") int id) {
|
||||
try {
|
||||
Session session = sessionDAO.findById(id).get();
|
||||
sessionDAO.delete(session);
|
||||
return ResponseEntity.ok("Session deleted successfully");
|
||||
} catch (Exception ex) {
|
||||
return ex.toString();
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
|
||||
}
|
||||
return "ok";
|
||||
}
|
||||
|
||||
private SessionDTO maptoDTO(Session s) {
|
||||
|
||||
Reference in New Issue
Block a user