Skip to content

Commit

Permalink
get user with id
Browse files Browse the repository at this point in the history
  • Loading branch information
SisouDev committed Oct 22, 2023
1 parent 7b8b863 commit 1c7adbb
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/restful/app/resources/UserResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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 @@ -27,4 +28,10 @@ public ResponseEntity<List<UserDTO>> findAll(){
List<UserDTO> listDto = list.stream().map(x -> new UserDTO(x)).collect(Collectors.toList());
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,21 @@
package restful.app.resources.exception;

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 jakarta.servlet.http.HttpServletRequest;
import restful.app.services.exception.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(), "Not found", e.getMessage(), request.getRequestURI());
return ResponseEntity.status(status).body(err);
}
}
64 changes: 64 additions & 0 deletions src/main/java/restful/app/resources/exception/StandardError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package restful.app.resources.exception;

import java.io.Serializable;

public class StandardError implements Serializable{
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) {
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 getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getPath() {
return path;
}

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


}
7 changes: 7 additions & 0 deletions src/main/java/restful/app/services/UserService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package restful.app.services;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import restful.app.domain.User;
import restful.app.repository.UserRepository;
import restful.app.services.exception.ObjectNotFoundException;

@Service
public class UserService {
Expand All @@ -16,4 +18,9 @@ public class UserService {
public List<User> findAll(){
return repository.findAll();
}

public User findById(String id){
Optional<User> obj = repository.findById(id);
return obj.orElseThrow(() -> new ObjectNotFoundException("Object not found!"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package restful.app.services.exception;

public class ObjectNotFoundException extends RuntimeException{

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

0 comments on commit 1c7adbb

Please sign in to comment.