From f338960f3bedc54c1d9a1d28c3993eae2e46b29f Mon Sep 17 00:00:00 2001 From: Jonas Souza Date: Tue, 21 May 2024 22:31:20 -0300 Subject: [PATCH] feat: add basic beans validation --- build.gradle | 1 + .../fastfood/adapters/in/rest/CustomerResource.java | 3 ++- .../fastfood/adapters/in/rest/ProductResource.java | 5 +++-- .../in/rest/exceptions/ResourceExceptionHandler.java | 6 +----- .../fastfood/application/dto/CategoryDTO.java | 3 +++ .../fastfood/application/dto/CustomerDTO.java | 8 ++++++++ .../grupo30/fastfood/application/dto/ProductDTO.java | 12 +++++++++++- 7 files changed, 29 insertions(+), 9 deletions(-) diff --git a/build.gradle b/build.gradle index 49884ef..bf0bc7d 100644 --- a/build.gradle +++ b/build.gradle @@ -55,6 +55,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0' compileOnly 'org.projectlombok:lombok' diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/CustomerResource.java b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/CustomerResource.java index de1fd17..2f0dbca 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/CustomerResource.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/CustomerResource.java @@ -4,6 +4,7 @@ import br.com.fiap.grupo30.fastfood.application.useCases.CustomerUseCase; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; import java.net.URI; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; @@ -36,7 +37,7 @@ public ResponseEntity findCustomerByCpf( @Operation( summary = "Create a new customer", description = "Create a new customer and return the created customer's data") - public ResponseEntity createCustomer(@RequestBody CustomerDTO dto) { + public ResponseEntity createCustomer(@RequestBody @Valid CustomerDTO dto) { CustomerDTO dtoCreated = customerUseCase.createCustomer(dto); URI uri = ServletUriComponentsBuilder.fromCurrentRequest() diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/ProductResource.java b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/ProductResource.java index 53de90e..b17dac8 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/ProductResource.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/ProductResource.java @@ -4,6 +4,7 @@ import br.com.fiap.grupo30.fastfood.application.useCases.ProductUseCase; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; import java.net.URI; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; @@ -50,7 +51,7 @@ public ResponseEntity findProductById(@PathVariable Long id) { @Operation( summary = "Create a new product", description = "Create a new product and return the created product's data") - public ResponseEntity createProduct(@RequestBody ProductDTO dto) { + public ResponseEntity createProduct(@RequestBody @Valid ProductDTO dto) { ProductDTO dtoCreated = productUseCase.createProduct(dto); URI uri = ServletUriComponentsBuilder.fromCurrentRequest() @@ -65,7 +66,7 @@ public ResponseEntity createProduct(@RequestBody ProductDTO dto) { summary = "Update a product", description = "Update the data of an existing product based on its ID") public ResponseEntity updateProduct( - @PathVariable Long id, @RequestBody ProductDTO dto) { + @PathVariable Long id, @RequestBody @Valid ProductDTO dto) { ProductDTO dtoUpdated = productUseCase.updateProduct(id, dto); return ResponseEntity.ok().body(dtoUpdated); } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/ResourceExceptionHandler.java b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/ResourceExceptionHandler.java index 19eae55..4a5a5bd 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/ResourceExceptionHandler.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/ResourceExceptionHandler.java @@ -1,10 +1,6 @@ package br.com.fiap.grupo30.fastfood.adapters.in.rest.exceptions; -import br.com.fiap.grupo30.fastfood.application.services.exceptions.DatabaseException; -import br.com.fiap.grupo30.fastfood.application.services.exceptions.ResourceBadRequestException; -import br.com.fiap.grupo30.fastfood.application.services.exceptions.ResourceConflictException; -import br.com.fiap.grupo30.fastfood.application.services.exceptions.ResourceNotFoundException; -import br.com.fiap.grupo30.fastfood.application.services.exceptions.UserCantChangeOrderAfterSubmitException; +import br.com.fiap.grupo30.fastfood.application.services.exceptions.*; import jakarta.servlet.http.HttpServletRequest; import java.time.Instant; import org.springframework.http.HttpStatus; diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/CategoryDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/CategoryDTO.java index 8e1ca6a..3c933ef 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/CategoryDTO.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/CategoryDTO.java @@ -1,6 +1,7 @@ package br.com.fiap.grupo30.fastfood.application.dto; import br.com.fiap.grupo30.fastfood.domain.Category; +import jakarta.validation.constraints.NotBlank; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -11,6 +12,8 @@ public class CategoryDTO { private Long id; + + @NotBlank(message = "Campo requirido") private String name; public CategoryDTO(Category entity) { diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/CustomerDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/CustomerDTO.java index 10ee3e9..27c1f89 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/CustomerDTO.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/CustomerDTO.java @@ -1,6 +1,8 @@ package br.com.fiap.grupo30.fastfood.application.dto; import br.com.fiap.grupo30.fastfood.domain.Customer; +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.NotBlank; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -11,8 +13,14 @@ public class CustomerDTO { private Long id; + + @NotBlank(message = "Campo obrigatório") private String name; + + @NotBlank(message = "Campo obrigatório") private String cpf; + + @Email(message = "Favor entrar um email válido") private String email; public CustomerDTO(Customer entity) { diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/ProductDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/ProductDTO.java index 15bc000..c1d954f 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/ProductDTO.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/application/dto/ProductDTO.java @@ -1,6 +1,8 @@ package br.com.fiap.grupo30.fastfood.application.dto; import br.com.fiap.grupo30.fastfood.domain.Product; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Positive; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -11,10 +13,18 @@ public class ProductDTO { private Long id; + + @NotBlank(message = "Campo requirido") private String name; + + @NotBlank(message = "Campo requirido") private String description; - private Double price; + + @Positive(message = "Preço deve ser um valor positivo") private Double price; + + @NotBlank(message = "Campo requirido") private String imgUrl; + private CategoryDTO category; public ProductDTO(Product entity) {