Skip to content

Commit

Permalink
Merge pull request #4 from viacheslav-torbin/book-validation
Browse files Browse the repository at this point in the history
Book validation added
  • Loading branch information
viacheslav-torbin authored Oct 21, 2023
2 parents d0ad03e + 29e2ce8 commit ed78cc0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<version.mapstruct-lombok>0.2.0</version.mapstruct-lombok>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/bookstore/controller/BookController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bookstore.controller;

import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.bookstore.dto.BookDto;
Expand Down Expand Up @@ -31,13 +32,13 @@ public BookDto getBookById(@PathVariable Long id) {
}

@PostMapping
public BookDto createBook(@RequestBody CreateBookRequestDto bookRequestDto) {
public BookDto createBook(@RequestBody @Valid CreateBookRequestDto bookRequestDto) {
return bookService.save(bookRequestDto);
}

@PutMapping("/{id}")
public void updateBookById(@PathVariable Long id,
@RequestBody CreateBookRequestDto bookRequestDto) {
@RequestBody @Valid CreateBookRequestDto bookRequestDto) {
bookService.updateBookById(id, bookRequestDto);
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/bookstore/dto/CreateBookRequestDto.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package org.bookstore.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.PositiveOrZero;
import java.math.BigDecimal;
import lombok.Data;

@Data
public class CreateBookRequestDto {
private Long id;
@NotBlank
private String title;
@NotBlank
private String author;
@NotBlank
private String isbn;
@PositiveOrZero
private BigDecimal price;
private String description;
private String coverImage;
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/org/bookstore/exceptions/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.bookstore.exceptions;

import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
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.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex,
HttpHeaders headers,
HttpStatusCode status,
WebRequest request) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("time", LocalDateTime.now());
body.put("status", HttpStatus.BAD_REQUEST);
List<String> error = ex.getBindingResult().getAllErrors()
.stream()
.map(this::getErrorMessage)
.collect(Collectors.toList());
body.put("errors", error);
return new ResponseEntity<>(body, headers, status);
}

private String getErrorMessage(ObjectError objectError) {
if (objectError instanceof FieldError) {
String field = ((FieldError) objectError).getField();
String message = objectError.getDefaultMessage();
return field + " " + message;
}
return objectError.getDefaultMessage();
}
}

0 comments on commit ed78cc0

Please sign in to comment.