-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2b50ee
commit 92a893a
Showing
8 changed files
with
110 additions
and
20 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/mate/academy/bookstore/config/I18nConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package mate.academy.bookstore.config; | ||
|
||
import org.springframework.context.MessageSource; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.support.ReloadableResourceBundleMessageSource; | ||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; | ||
|
||
@Configuration | ||
public class I18nConfig { | ||
@Bean | ||
public MessageSource messageSource() { | ||
ReloadableResourceBundleMessageSource messageSource | ||
= new ReloadableResourceBundleMessageSource(); | ||
|
||
messageSource.setBasename("classpath:messages"); | ||
messageSource.setDefaultEncoding("UTF-8"); | ||
return messageSource; | ||
} | ||
|
||
@Bean | ||
public LocalValidatorFactoryBean getValidator() { | ||
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean(); | ||
bean.setValidationMessageSource(messageSource()); | ||
return bean; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
src/main/java/mate/academy/bookstore/exception/DataProcessingException.java
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
src/main/java/mate/academy/bookstore/exception/EntityNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/mate/academy/bookstore/handler/BookControllerExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package mate.academy.bookstore.handler; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import mate.academy.bookstore.exception.EntityNotFoundException; | ||
import org.springframework.dao.DataIntegrityViolationException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.validation.ObjectError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
@ControllerAdvice | ||
public class BookControllerExceptionHandler { | ||
@ExceptionHandler(EntityNotFoundException.class) | ||
@ResponseBody | ||
public ResponseEntity<Object> handleEntityNotFoundException(EntityNotFoundException ex) { | ||
return getResponseEntity(HttpStatus.NOT_FOUND, ex.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
@ResponseBody | ||
public ResponseEntity<Object> handleValidationExceptions(MethodArgumentNotValidException ex) { | ||
Map<String, String> errors = new HashMap<>(); | ||
ex.getBindingResult().getAllErrors().forEach(error -> { | ||
Map.Entry<String, String> errorMessage = getErrorMessage(error); | ||
errors.put(errorMessage.getKey(), errorMessage.getValue()); | ||
}); | ||
|
||
return getResponseEntity(HttpStatus.BAD_REQUEST, errors); | ||
} | ||
|
||
@ExceptionHandler(DataIntegrityViolationException.class) | ||
@ResponseBody | ||
public ResponseEntity<Object> handleDataIntegrityViolationException( | ||
DataIntegrityViolationException ex) { | ||
return getResponseEntity(HttpStatus.CONFLICT, ex.getMessage()); | ||
} | ||
|
||
private Map.Entry<String, String> getErrorMessage(ObjectError e) { | ||
if (e instanceof FieldError fieldError) { | ||
String field = fieldError.getField(); | ||
String message = e.getDefaultMessage(); | ||
return Map.entry(field, message); | ||
} | ||
return Map.entry("", e.getDefaultMessage()); | ||
} | ||
|
||
private ResponseEntity<Object> getResponseEntity(HttpStatus status, Object errors) { | ||
Map<String, Object> body = new LinkedHashMap<>(); | ||
body.put("success", status.is2xxSuccessful()); | ||
body.put("errors", errors); | ||
return new ResponseEntity<>(body, status); | ||
} | ||
} | ||
|
2 changes: 2 additions & 0 deletions
2
src/main/java/mate/academy/bookstore/repository/BookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package mate.academy.bookstore.repository; | ||
|
||
import java.util.Optional; | ||
import mate.academy.bookstore.model.Book; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface BookRepository extends JpaRepository<Book, Long> { | ||
Optional<Book> findByIsbn(String isbn); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
validation.title.notempty=Title is required | ||
validation.author.notempty=Author is required | ||
validation.isbn.notempty=The ISBN is required | ||
validation.isbn.valid=The ISBN is not valid | ||
validation.price.valid=The price must be greater than 0 |