brenovambaster / RTCC

Repositório de TCC do IFNMG
MIT License
1 stars 0 forks source link

Checar todos os endpoints. #4

Open brenovambaster opened 3 months ago

brenovambaster commented 3 months ago
  1. Fazer checagem nos endpoits se o corpo da requisição está correto. i.e, se não falta nenhum atributo
  2. checar se os retornos nos endpoist estão padronizados e retornam de forma correta
  3. Fazer classes Services para alguns Controllers .
brenovambaster commented 2 months ago

Antes de tudo, ver as exceções já criadas e usá-las nos controllers e services quando create, update, delete, get

Veja aqui: https://github.com/brenovambaster/RTCC/tree/master/demo/src/main/java/com/rtcc/demo/exception

CREATE

Retonar ResponseEntity.ok().build();

Exemplo:

   @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
        }
    }

GET ALL

Return: ResponseEntity.ok(courseList);

Exemplo:

  @GetMapping
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public ResponseEntity<List<CourseResponseDTO>> getAllCourses() {
        List<CourseResponseDTO> courseList = courseRepository.findAll()
                .stream()
                .map(CourseResponseDTO::new)
                .toList();
        return ResponseEntity.ok(courseList);
    }

GET ID

Retornar: return ResponseEntity.ok(courseResponse) || 404

Exemplo

  @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);
    }

Delete

Retornar: ResponseEntity.noContent().build();

Exemplo


  @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();
    }
brenovambaster commented 2 months ago

@FilipeAbner