Skip to content

Commit

Permalink
Validation added
Browse files Browse the repository at this point in the history
  • Loading branch information
viacheslav-torbin committed Oct 19, 2023
1 parent 58ec20b commit 0bdc3d6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
<artifactId>liquibase-core</artifactId>
<version>${liquibase.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>

<build>
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
7 changes: 7 additions & 0 deletions src/main/java/org/bookstore/dto/CreateBookRequestDto.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package org.bookstore.dto;

import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import java.math.BigDecimal;
import lombok.Data;

@Data
public class CreateBookRequestDto {
private Long id;
@NotNull
private String title;
@NotNull
private String author;
@NotNull
private String isbn;
@NotNull
@Min(0)
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 0bdc3d6

Please sign in to comment.