Skip to content

Commit

Permalink
Obtendo um usuário por id
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmaugusto committed May 30, 2020
1 parent d3d20fb commit 66ca635
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -30,4 +31,11 @@ public ResponseEntity<List<UserDTO>> findAll(){
return ResponseEntity.ok().body(listDTO);
}

@RequestMapping(value = "/{id}", method=RequestMethod.GET)
public ResponseEntity<UserDTO> findById(@PathVariable String id){

User obj = service.findById(id);

return ResponseEntity.ok().body(new UserDTO(obj));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.miguelaugusto.workshopmongo.resources.exception;

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;

import com.miguelaugusto.workshopmongo.services.ObjectNotFoundException;

@ControllerAdvice
public class ResourceExceptionHandler {

@ExceptionHandler(ObjectNotFoundException.class)
public ResponseEntity<StandardError> objectNotFound(ObjectNotFoundException e, HttpServletRequest request){
HttpStatus status = HttpStatus.NOT_FOUND;
StandardError err = new StandardError(System.currentTimeMillis(),
status.value(), "Não encontrado", e.getMessage(),
request.getRequestURI());
return ResponseEntity.status(status).body(err);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.miguelaugusto.workshopmongo.resources.exception;

import java.io.Serializable;

public class StandardError implements Serializable{

private static final long serialVersionUID = 1L;
private Long timestamp;
private Integer status;
private String error;
private String message;
private String path;

public StandardError() {
}

public StandardError(Long timestamp, Integer status, String error, String message, String path) {
super();
this.timestamp = timestamp;
this.status = status;
this.error = error;
this.message = message;
this.path = path;
}

public Long getTimestamp() {
return timestamp;
}

public void setTimestamp(Long 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 com.miguelaugusto.workshopmongo.services;

public class ObjectNotFoundException extends RuntimeException {

private static final long serialVersionUID = 1L;

public ObjectNotFoundException(String msg) {
super(msg);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.miguelaugusto.workshopmongo.services;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -17,4 +18,10 @@ public class UserService {
public List<User> findAll(){
return repo.findAll();
}

public User findById(String id) {

Optional<User> obj = repo.findById(id);
return obj.orElseThrow(() -> new ObjectNotFoundException("Não foi encontrado nenhum usuário com o id " + id));
}
}

0 comments on commit 66ca635

Please sign in to comment.