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

Feature/use cases #16

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,7 +1,7 @@
package br.com.fiap.grupo30.fastfood.adapters.in.rest;

import br.com.fiap.grupo30.fastfood.application.dto.CategoryDTO;
import br.com.fiap.grupo30.fastfood.application.services.CategoryService;
import br.com.fiap.grupo30.fastfood.application.useCases.CategoryUseCase;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
Expand All @@ -16,19 +16,19 @@
@Tag(name = "Categories Resource", description = "RESTful API for managing categories.")
public class CategoryResource {

private final CategoryService categoryService;
private final CategoryUseCase categoryUseCase;

@Autowired
public CategoryResource(CategoryService categoryService) {
this.categoryService = categoryService;
public CategoryResource(CategoryUseCase categoryUseCase) {
this.categoryUseCase = categoryUseCase;
}

@GetMapping
@Operation(
summary = "Get all categories",
description = "Retrieve a list of all registered categories")
public ResponseEntity<List<CategoryDTO>> findAll() {
List<CategoryDTO> list = categoryService.findAll();
List<CategoryDTO> list = categoryUseCase.findProducts();
return ResponseEntity.ok().body(list);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package br.com.fiap.grupo30.fastfood.adapters.in.rest;

import br.com.fiap.grupo30.fastfood.application.dto.CustomerDTO;
import br.com.fiap.grupo30.fastfood.application.services.CustomerService;
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 All @@ -12,32 +13,32 @@

@RestController
@RequestMapping(value = "/customers")
@Tag(name = "Customer Resource", description = "RESTful API for managing customers.")
@Tag(name = "Customers Resource", description = "RESTful API for managing customers.")
public class CustomerResource {

private static final String PATH_VARIABLE_ID = "/{id}";

private final CustomerService customerService;
private final CustomerUseCase customerUseCase;

@Autowired
public CustomerResource(CustomerService customerService) {
this.customerService = customerService;
public CustomerResource(CustomerUseCase customerUseCase) {
this.customerUseCase = customerUseCase;
}

@GetMapping
@Operation(summary = "Get a customer", description = "Retrieve a registered customer by cpf")
public ResponseEntity<CustomerDTO> findCustomerByCpf(
@RequestParam(value = "cpf", defaultValue = "0") String cpf) {
CustomerDTO dto = customerService.findCustomerByCpf(cpf);
CustomerDTO dto = customerUseCase.findCustomerByCpf(cpf);
return ResponseEntity.ok().body(dto);
}

@PostMapping
@Operation(
summary = "Create a new customer",
description = "Create a new customer and return the created customer's data")
public ResponseEntity<CustomerDTO> insert(@RequestBody CustomerDTO dto) {
CustomerDTO dtoCreated = customerService.insert(dto);
public ResponseEntity<CustomerDTO> createCustomer(@RequestBody @Valid CustomerDTO dto) {
CustomerDTO dtoCreated = customerUseCase.createCustomer(dto);
URI uri =
ServletUriComponentsBuilder.fromCurrentRequest()
.path(PATH_VARIABLE_ID)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package br.com.fiap.grupo30.fastfood.adapters.in.rest;

import br.com.fiap.grupo30.fastfood.application.dto.ProductDTO;
import br.com.fiap.grupo30.fastfood.application.services.ProductService;
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 All @@ -18,11 +19,11 @@ public class ProductResource {

private static final String PATH_VARIABLE_ID = "/{id}";

private final ProductService productService;
private final ProductUseCase productUseCase;

@Autowired
public ProductResource(ProductService productService) {
this.productService = productService;
public ProductResource(ProductUseCase productUseCase) {
this.productUseCase = productUseCase;
}

@GetMapping
Expand All @@ -31,27 +32,27 @@ public ProductResource(ProductService productService) {
description =
"Retrieve a list of all registered products or by categoryId "
+ "via RequestParam. i.e., ?categoryId=1")
public ResponseEntity<List<ProductDTO>> findAll(
public ResponseEntity<List<ProductDTO>> findProductsByCategoryId(
@RequestParam(value = "categoryId", defaultValue = "0") Long categoryId) {
List<ProductDTO> list = productService.findProductsByCategoryId(categoryId);
List<ProductDTO> list = productUseCase.findProductsByCategoryId(categoryId);
return ResponseEntity.ok().body(list);
}

@GetMapping(value = PATH_VARIABLE_ID)
@Operation(
summary = "Get a product by ID",
description = "Retrieve a specific product based on its ID")
public ResponseEntity<ProductDTO> findById(@PathVariable Long id) {
ProductDTO dto = productService.findById(id);
public ResponseEntity<ProductDTO> findProductById(@PathVariable Long id) {
ProductDTO dto = productUseCase.findProductById(id);
return ResponseEntity.ok().body(dto);
}

@PostMapping
@Operation(
summary = "Create a new product",
description = "Create a new product and return the created product's data")
public ResponseEntity<ProductDTO> insert(@RequestBody ProductDTO dto) {
ProductDTO dtoCreated = productService.insert(dto);
public ResponseEntity<ProductDTO> createProduct(@RequestBody @Valid ProductDTO dto) {
ProductDTO dtoCreated = productUseCase.createProduct(dto);
URI uri =
ServletUriComponentsBuilder.fromCurrentRequest()
.path(PATH_VARIABLE_ID)
Expand All @@ -64,17 +65,18 @@ public ResponseEntity<ProductDTO> insert(@RequestBody ProductDTO dto) {
@Operation(
summary = "Update a product",
description = "Update the data of an existing product based on its ID")
public ResponseEntity<ProductDTO> update(@PathVariable Long id, @RequestBody ProductDTO dto) {
ProductDTO dtoUpdated = productService.update(id, dto);
public ResponseEntity<ProductDTO> updateProduct(
@PathVariable Long id, @RequestBody @Valid ProductDTO dto) {
ProductDTO dtoUpdated = productUseCase.updateProduct(id, dto);
return ResponseEntity.ok().body(dtoUpdated);
}

@DeleteMapping(value = PATH_VARIABLE_ID)
@Operation(
summary = "Delete a product",
description = "Delete an existing product based on its ID")
public ResponseEntity<Void> delete(@PathVariable Long id) {
productService.delete(id);
public ResponseEntity<Void> deleteProduct(@PathVariable Long id) {
productUseCase.deleteProduct(id);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package br.com.fiap.grupo30.fastfood.adapters.in.rest.exceptions;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class FieldMessage {
private String fieldName;
private String message;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
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.*;
import jakarta.servlet.http.HttpServletRequest;
import java.time.Instant;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

Expand Down Expand Up @@ -64,4 +63,22 @@ public ResponseEntity<StandardError> database(DatabaseException e, HttpServletRe
err.setPath(request.getRequestURI());
return ResponseEntity.status(status).body(err);
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ValidationError> validation(
MethodArgumentNotValidException e, HttpServletRequest request) {
HttpStatus status = HttpStatus.UNPROCESSABLE_ENTITY;
ValidationError err = new ValidationError();
err.setTimestamp(Instant.now());
err.setStatus(status.value());
err.setError("Validation exception");
err.setMessage(e.getMessage());
err.setPath(request.getRequestURI());

for (FieldError f : e.getBindingResult().getFieldErrors()) {
err.addError(f.getField(), f.getDefaultMessage());
}

return ResponseEntity.status(status).body(err);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package br.com.fiap.grupo30.fastfood.adapters.in.rest.exceptions;

import java.util.ArrayList;
import java.util.List;
import lombok.Getter;

@Getter
public class ValidationError extends StandardError {
private final List<FieldMessage> errors = new ArrayList<>();

public void addError(String fieldName, String message) {
errors.add(new FieldMessage(fieldName, message));
}
}
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package br.com.fiap.grupo30.fastfood.application.useCases;

import br.com.fiap.grupo30.fastfood.application.dto.CategoryDTO;
import java.util.List;

public interface CategoryUseCase {

List<CategoryDTO> findProducts();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package br.com.fiap.grupo30.fastfood.application.useCases;

import br.com.fiap.grupo30.fastfood.application.dto.CustomerDTO;

public interface CustomerUseCase {

CustomerDTO findCustomerByCpf(String cpf);

CustomerDTO createCustomer(CustomerDTO dto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package br.com.fiap.grupo30.fastfood.application.useCases;

import br.com.fiap.grupo30.fastfood.application.dto.ProductDTO;
import java.util.List;

public interface ProductUseCase {

List<ProductDTO> findProductsByCategoryId(Long categoryId);

ProductDTO findProductById(Long id);

ProductDTO createProduct(ProductDTO productDTO);

ProductDTO updateProduct(Long id, ProductDTO dto);

void deleteProduct(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package br.com.fiap.grupo30.fastfood.application.useCases.impl;

import br.com.fiap.grupo30.fastfood.application.dto.CategoryDTO;
import br.com.fiap.grupo30.fastfood.application.services.CategoryService;
import br.com.fiap.grupo30.fastfood.application.useCases.CategoryUseCase;
import java.util.List;
import org.springframework.stereotype.Service;

@Service
public class CategoryUseCaseImpl implements CategoryUseCase {

private final CategoryService categoryService;

public CategoryUseCaseImpl(CategoryService categoryService) {
this.categoryService = categoryService;
}

@Override
public List<CategoryDTO> findProducts() {
return categoryService.findAll();
}
}
Loading
Loading