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