Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
feat: add basic beans validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasmzsouza committed May 22, 2024
1 parent 47b8768 commit f338960
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,7 +37,7 @@ public ResponseEntity<CustomerDTO> findCustomerByCpf(
@Operation(
summary = "Create a new customer",
description = "Create a new customer and return the created customer's data")
public ResponseEntity<CustomerDTO> createCustomer(@RequestBody CustomerDTO dto) {
public ResponseEntity<CustomerDTO> createCustomer(@RequestBody @Valid CustomerDTO dto) {
CustomerDTO dtoCreated = customerUseCase.createCustomer(dto);
URI uri =
ServletUriComponentsBuilder.fromCurrentRequest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,7 +51,7 @@ public ResponseEntity<ProductDTO> findProductById(@PathVariable Long id) {
@Operation(
summary = "Create a new product",
description = "Create a new product and return the created product's data")
public ResponseEntity<ProductDTO> createProduct(@RequestBody ProductDTO dto) {
public ResponseEntity<ProductDTO> createProduct(@RequestBody @Valid ProductDTO dto) {
ProductDTO dtoCreated = productUseCase.createProduct(dto);
URI uri =
ServletUriComponentsBuilder.fromCurrentRequest()
Expand All @@ -65,7 +66,7 @@ public ResponseEntity<ProductDTO> createProduct(@RequestBody ProductDTO dto) {
summary = "Update a product",
description = "Update the data of an existing product based on its ID")
public ResponseEntity<ProductDTO> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,6 +12,8 @@
public class CategoryDTO {

private Long id;

@NotBlank(message = "Campo requirido")
private String name;

public CategoryDTO(Category entity) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit f338960

Please sign in to comment.