Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from lucas0headshot/lucas
Browse files Browse the repository at this point in the history
Implementar RNs e validações nas Classes
  • Loading branch information
lucas0headshot authored Mar 20, 2024
2 parents 5cd3128 + 16ff682 commit 9927cfe
Show file tree
Hide file tree
Showing 30 changed files with 368 additions and 114 deletions.
12 changes: 12 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.2.Final</version>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.senac.gestaocurso.enterprise.exception;



import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;



@ResponseStatus(HttpStatus.CONFLICT)
public class BusinessException extends RuntimeException {
public BusinessException(String msg) {
super(msg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.senac.gestaocurso.enterprise.exception;



import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;



@ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
public class ValidationException extends RuntimeException {
public ValidationException(String msg) {
super(msg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.senac.gestaocurso.health;



import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
public class HealthCheckController {
@GetMapping("/health")
public String healthCheck() {
return "Aplicação está rodando";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@


import jakarta.persistence.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.time.LocalDate;


Expand All @@ -22,6 +23,8 @@ public class Avaliacao extends EntityID {
private String nome;

@Column
@Min(value = 0, message = "A nota deve ser no mínimo 0")
@Max(value = 10, message = "A nota deve ser no máximo 10")
private Integer nota;

@Column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@



import jakarta.persistence.*;
import org.hibernate.validator.constraints.br.CPF;
import org.hibernate.validator.constraints.br.TituloEleitoral;
import javax.validation.constraints.Email;
import com.senac.gestaocurso.enums.*;
import jakarta.persistence.*;
import java.time.LocalDate;
Expand All @@ -17,6 +21,7 @@ public class Funcionario extends EntityID {
private String nome;

@Column
@CPF(message = "Informe um CPF válido")
private String cpf;

@Column
Expand Down Expand Up @@ -44,6 +49,7 @@ public class Funcionario extends EntityID {
private EstadoCivil estadoCivil;

@Column
@TituloEleitoral(message = "Informe um título de eleitor válido")
private String tituloEleitor;

@Column
Expand All @@ -59,6 +65,7 @@ public class Funcionario extends EntityID {
private String registroProfissional;

@Column
@Email(message = "Informe um e-mail válido")
private String email;

@Column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class Inscricao extends EntityID {
@JoinColumn(name = "turma_id")
private Turma turma;


public Turma getTurma() {
return turma;
}
Expand All @@ -42,6 +43,13 @@ public Inscricao() {
}


public Turma getTurma() {
return turma;
}

public void setTurma(Turma turma) {
this.turma = turma;
}

public Funcionario getInscrito() {
return inscrito;
Expand Down
17 changes: 15 additions & 2 deletions backend/src/main/java/com/senac/gestaocurso/models/Turma.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class Turma extends EntityID {
@OneToMany(mappedBy = "turma", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Inscricao> inscritos = new ArrayList<>();

@Column
private Integer limiteQtdInscricao;


public Turma() {
Expand Down Expand Up @@ -86,15 +88,26 @@ public void setCurso(Curso curso) {
this.curso = curso;
}

public Integer getLimiteQtdInscricao() {
return limiteQtdInscricao;
}

public void setLimiteQtdInscricao(Integer limiteQtdInscricao) {
this.limiteQtdInscricao = limiteQtdInscricao;
}



@Override
public String toString() {
return "Turma{" +
"nomeTurma='" + nome + '\'' +
", inscritos=" + inscritos +
"curso=" + curso +
", nome='" + nome + '\'' +
", dataInicio=" + dataInicio +
", dataFinal=" + dataFinal +
", descricao='" + descricao + '\'' +
", inscritos=" + inscritos +
", limiteQtdInscricao=" + limiteQtdInscricao +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface FuncionarioRepository extends JpaRepository <Funcionario, Long> {
Optional<Funcionario> findByCpf(String cpf);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package com.senac.gestaocurso.repository;



import com.senac.gestaocurso.models.Inscricao;
import com.senac.gestaocurso.models.Turma;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Repository;

import java.util.List;



@Service
@Repository
public interface InscricaoRepository extends JpaRepository<Inscricao, Long> {
}
List<Inscricao> findAllByTurma(Turma turma);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.senac.gestaocurso.resource;



import com.senac.gestaocurso.enterprise.exception.BusinessException;
import com.senac.gestaocurso.enterprise.exception.ValidationException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;



abstract public class AbstractController {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map<String, String> handleValidationExceptions(MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
List<String> collect = ex.getBindingResult()
.getAllErrors().stream()
.map(p -> ((FieldError) p).getField() + " "
+ p.getDefaultMessage())
.collect(Collectors.toList());
errors.put("erro", collect.toString());

return errors;
}



@ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
@ExceptionHandler(ValidationException.class)
public Map<String, String> handleValidationExceptions(ValidationException ex) {
Map<String, String> errors = new HashMap<>();
errors.put("erro", ex.getMessage());

return errors;
}



@ResponseStatus(HttpStatus.CONFLICT)
@ExceptionHandler(BusinessException.class)
public Map<String, String> handleBusinessExceptions(ValidationException ex) {
Map<String, String> errors = new HashMap<>();
errors.put("erro", ex.getMessage());

return errors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@RestController
@RequestMapping("/api/aulas")
public class AulaController {
public class AulaController extends AbstractController {
@Autowired
private AulaService aulaService;

Expand All @@ -26,8 +26,7 @@ public ResponseEntity salvar(@RequestBody Aula aulas){


@GetMapping
public ResponseEntity findAll(@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "0") int size) {
public ResponseEntity findAll(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
Pageable pageable = PageRequest.of(page, size);
Page<Aula> aulas = aulaService.buscaTodos(pageable);
return ResponseEntity.ok(aulas);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,38 @@

@RestController
@RequestMapping("/api/avaliacao")
public class AvaliacaoController {
public class FuncionarioController {
@Autowired
private AvaliacaoService avaliacaoService;

@PostMapping()
public ResponseEntity salvar(@RequestBody Avaliacao avaliacoes) {
Avaliacao save = avaliacaoService.salvar(avaliacoes);
return ResponseEntity.created(URI.create("/api/avaliacao" + avaliacoes.getId())).body(save);
}


@GetMapping
public ResponseEntity findAll(@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "0") int size) {
Pageable pageable = PageRequest.of(page, size);
Page<Avaliacao> avaliacoes = avaliacaoService.buscaTodos(pageable);
return ResponseEntity.ok(avaliacoes);
}

@GetMapping("/{id}")
public ResponseEntity findById(@PathVariable("id") Long id) {
Avaliacao avaliacao = avaliacaoService.buscaPorId(id);
return ResponseEntity.ok().body(avaliacao);
}


@DeleteMapping("{id}")
public ResponseEntity remove(@PathVariable("id") Long id) {
avaliacaoService.remover(id);
return ResponseEntity.noContent().build();
}
public class AvaliacaoController extends AbstractController {
@Autowired
private AvaliacaoService avaliacaoService;


@PostMapping()
public ResponseEntity salvar(@RequestBody Avaliacao avaliacoes) {
Avaliacao save = avaliacaoService.salvar(avaliacoes);
return ResponseEntity.created(URI.create("/api/avaliacao" + avaliacoes.getId())).body(save);
}


@GetMapping
public ResponseEntity findAll(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
Pageable pageable = PageRequest.of(page, size);
Page<Avaliacao> avaliacoes = avaliacaoService.buscaTodos(pageable);
return ResponseEntity.ok(avaliacoes);
}

@GetMapping("/{id}")
public ResponseEntity findById(@PathVariable("id") Long id) {
Avaliacao avaliacao = avaliacaoService.buscaPorId(id);
return ResponseEntity.ok().body(avaliacao);
}


@DeleteMapping("{id}")
public ResponseEntity remove(@PathVariable("id") Long id) {
avaliacaoService.remover(id);
return ResponseEntity.noContent().build();
}

/**
* Reparar classe Avaliacao para alterar o service, mapeamento esta certo
Expand All @@ -56,5 +56,4 @@ public ResponseEntity update(@PathVariable("id") Long id, @RequestBody Avaliacao
Avaliacao avaliacao = avaliacaoService.alterar(id, entity);
*/
}
}

}
Loading

0 comments on commit 9927cfe

Please sign in to comment.