27.2. Modified UserService to validate that user exists when getDefaultProfileImage method is invoked (#27)
This commit is contained in:
@ -15,6 +15,7 @@ import org.springframework.validation.BindException;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
|
||||
@ -65,7 +66,8 @@ public class ExceptionHandling {
|
||||
@ExceptionHandler({
|
||||
EmailExistsException.class, UsernameExistsException.class,
|
||||
EmailNotFoundException.class, UserNotFoundException.class,
|
||||
MaxUploadSizeExceededException.class, NotAnImageFileException.class
|
||||
MaxUploadSizeExceededException.class, NotAnImageFileException.class,
|
||||
IllegalArgumentException.class
|
||||
})
|
||||
public ResponseEntity<HttpResponse> badRequestExceptionHandler(Exception exception) {
|
||||
return createHttpResponse(BAD_REQUEST, exception.getMessage());
|
||||
@ -104,6 +106,11 @@ public class ExceptionHandling {
|
||||
return createHttpResponse(BAD_REQUEST, message);
|
||||
}
|
||||
|
||||
@ExceptionHandler({MethodArgumentTypeMismatchException.class})
|
||||
public ResponseEntity<HttpResponse> validationExceptionHandler(MethodArgumentTypeMismatchException exception) {
|
||||
return createHttpResponse(BAD_REQUEST, exception.getCause().getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(NoResultException.class)
|
||||
public ResponseEntity<HttpResponse> notFoundException(NoResultException exception) {
|
||||
log.error(exception.getMessage());
|
||||
|
||||
@ -16,6 +16,8 @@ public interface UserRepository extends JpaRepository<User, Long> {
|
||||
|
||||
Boolean existsByEmail(String email);
|
||||
|
||||
Boolean existsByUserId(UUID userId);
|
||||
|
||||
Optional<User> findByUserId(UUID userId);
|
||||
|
||||
}
|
||||
|
||||
@ -44,6 +44,7 @@ import static org.springframework.http.MediaType.*;
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
public static final String USERNAME_NOT_FOUND_MSG = "User with username `%s` not found";
|
||||
public static final String USER_NOT_FOUND_MSG = "User not found";
|
||||
public static final String USERNAME_EXISTS_MSG = "Username `%s` is already taken. Please select another one";
|
||||
public static final String EMAIL_NOT_FOUND_MSG = "User with email `%s` not found";
|
||||
public static final String EMAIL_EXISTS_MSG = "User with email `%s` is already registered";
|
||||
@ -278,6 +279,11 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
@Override
|
||||
public byte[] getDefaultProfileImage(UUID userId) {
|
||||
|
||||
if (!userRepository.existsByUserId(userId)) {
|
||||
throw new UserNotFoundException(USER_NOT_FOUND_MSG);
|
||||
}
|
||||
|
||||
// "https://robohash.org/11951691-d373-4126-bef2-84d157a6546b"
|
||||
RequestEntity<Void> requestEntity = RequestEntity
|
||||
.get("/{userId}", userId)
|
||||
|
||||
Reference in New Issue
Block a user