Open brenovambaster opened 3 months ago
Veja aqui: https://github.com/brenovambaster/RTCC/tree/master/demo/src/main/java/com/rtcc/demo/exception
Retonar
ResponseEntity.ok().build();
@PostMapping
@CrossOrigin(origins = "*", allowedHeaders = "*")
public ResponseEntity<Void> saveCourse(@RequestBody CourseRequestDTO data) {
try {
Course course = new Course(data);
courseRepository.save(course);
return ResponseEntity.ok().build();
} catch (EntityAlreadyExistsException e) {
return ResponseEntity.status(409).build(); // Retorna 409 Conflict
}
}
Return:
ResponseEntity.ok(courseList);
@GetMapping
@CrossOrigin(origins = "*", allowedHeaders = "*")
public ResponseEntity<List<CourseResponseDTO>> getAllCourses() {
List<CourseResponseDTO> courseList = courseRepository.findAll()
.stream()
.map(CourseResponseDTO::new)
.toList();
return ResponseEntity.ok(courseList);
}
Retornar: return ResponseEntity.ok(courseResponse) || 404
@GetMapping("/{id}")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public ResponseEntity<CourseResponseDTO> getCourse(@PathVariable String id) {
Optional<Course> courseOpt = courseRepository.findById(id);
if (courseOpt.isEmpty()) {
return ResponseEntity.notFound().build();
}
CourseResponseDTO courseResponse = new CourseResponseDTO(courseOpt.get());
return ResponseEntity.ok(courseResponse);
}
Retornar:
ResponseEntity.noContent().build();
@DeleteMapping("/{id}")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public ResponseEntity<Void> deleteCourse(@PathVariable String id) {
if (!courseRepository.existsById(id)) {
return ResponseEntity.notFound().build();
}
boolean isLinkedToTcc = tccRepository.existsByCourseId(id);
if (isLinkedToTcc) {
throw new EntityDeletionException("Curso", "Não pode deletar um curso vinculado a um ou vários TCC");
}
courseRepository.deleteById(id);
return ResponseEntity.noContent().build();
}
@FilipeAbner