Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adicionando exception para o found by id e adicionando idade para o client #15

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Repositório com as implementações dos padrões de projeto explorados no Lab "
- Singleton
- Strategy/Repository
- Facade
- adcionando exceptions
10 changes: 10 additions & 0 deletions src/main/java/one/digitalinnovation/gof/model/Cliente.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class Cliente {
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String nome;
private int idade;

@ManyToOne
private Endereco endereco;

Expand All @@ -39,5 +41,13 @@ public Endereco getEndereco() {
public void setEndereco(Endereco endereco) {
this.endereco = endereco;
}
public int getIdade() {
return idade;
}

public void setIdade(int idade) {
this.idade = idade;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package one.digitalinnovation.gof.model.exceptions;

import java.time.Instant;

import javax.persistence.EntityNotFoundException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class ResourceExceptionHandler {
@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity<Standarderror> entityNotFound(EntityNotFoundException e, HttpServletRequest request){
Standarderror err = new Standarderror();
err.setTimestamp(Instant.now());
err.setStatus(HttpStatus.NOT_FOUND.value());
err.setError("Resource not found ");
err.setPath(request.getRequestURI());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(err);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package one.digitalinnovation.gof.model.exceptions;

import java.io.Serializable;
import java.time.Instant;

public class Standarderror implements Serializable {
private Instant timestamp;
private Integer status;
private String error;
private String path;

public Standarderror (){

}

public Instant getTimestamp() {
return timestamp;
}

public void setTimestamp(Instant timestamp) {
this.timestamp = timestamp;
}

public Integer getStatus() {
return status;
}

public void setStatus(Integer status) {
this.status = status;
}

public String getError() {
return error;
}

public void setError(String error) {
this.error = error;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package one.digitalinnovation.gof.service.exception;

public class EntityNotFoundException extends RuntimeException{
/**
* @param msg
*/
public EntityNotFoundException (String msg ){
super(msg);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import one.digitalinnovation.gof.model.EnderecoRepository;
import one.digitalinnovation.gof.service.ClienteService;
import one.digitalinnovation.gof.service.ViaCepService;
import one.digitalinnovation.gof.service.exception.EntityNotFoundException;

/**
* Implementação da <b>Strategy</b> {@link ClienteService}, a qual pode ser
Expand Down Expand Up @@ -42,8 +43,9 @@ public Iterable<Cliente> buscarTodos() {
@Override
public Cliente buscarPorId(Long id) {
// Buscar Cliente por ID.
Optional<Cliente> cliente = clienteRepository.findById(id);
return cliente.get();
return clienteRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Id not found " + id));

}

@Override
Expand Down