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

refactor: use cases #21

Merged
merged 4 commits into from
May 28, 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
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.useCases.CategoryUseCase;
import br.com.fiap.grupo30.fastfood.domain.usecases.product.ListAllCategoriesInMenuUseCase;
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 CategoryUseCase categoryUseCase;
private final ListAllCategoriesInMenuUseCase listAllCategoriesInMenuUseCase;

@Autowired
public CategoryResource(CategoryUseCase categoryUseCase) {
this.categoryUseCase = categoryUseCase;
public CategoryResource(ListAllCategoriesInMenuUseCase listAllCategoriesInMenuUseCase) {
this.listAllCategoriesInMenuUseCase = listAllCategoriesInMenuUseCase;
}

@GetMapping
@Operation(
summary = "Get all categories",
description = "Retrieve a list of all registered categories")
public ResponseEntity<List<CategoryDTO>> findAll() {
List<CategoryDTO> list = categoryUseCase.findProducts();
List<CategoryDTO> list = this.listAllCategoriesInMenuUseCase.execute();
return ResponseEntity.ok().body(list);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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.useCases.CustomerUseCase;
import br.com.fiap.grupo30.fastfood.domain.usecases.customer.FindCustomerByCpfUseCase;
import br.com.fiap.grupo30.fastfood.domain.usecases.customer.RegisterNewCustomerUseCase;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
Expand All @@ -18,18 +19,22 @@ public class CustomerResource {

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

private final CustomerUseCase customerUseCase;
private final FindCustomerByCpfUseCase findCustomerByCpfUseCase;
private final RegisterNewCustomerUseCase registerNewCustomerUseCase;

@Autowired
public CustomerResource(CustomerUseCase customerUseCase) {
this.customerUseCase = customerUseCase;
public CustomerResource(
FindCustomerByCpfUseCase findCustomerByCpfUseCase,
RegisterNewCustomerUseCase registerNewCustomerUseCase) {
this.findCustomerByCpfUseCase = findCustomerByCpfUseCase;
this.registerNewCustomerUseCase = registerNewCustomerUseCase;
}

@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 = customerUseCase.findCustomerByCpf(cpf);
CustomerDTO dto = this.findCustomerByCpfUseCase.execute(cpf);
return ResponseEntity.ok().body(dto);
}

Expand All @@ -38,7 +43,7 @@ public ResponseEntity<CustomerDTO> findCustomerByCpf(
summary = "Create a new customer",
description = "Create a new customer and return the created customer's data")
public ResponseEntity<CustomerDTO> createCustomer(@RequestBody @Valid CustomerDTO dto) {
CustomerDTO dtoCreated = customerUseCase.createCustomer(dto);
CustomerDTO dtoCreated = this.registerNewCustomerUseCase.execute(dto);
URI uri =
ServletUriComponentsBuilder.fromCurrentRequest()
.path(PATH_VARIABLE_ID)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
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.useCases.ProductUseCase;
import br.com.fiap.grupo30.fastfood.domain.usecases.product.CreateProductUseCase;
import br.com.fiap.grupo30.fastfood.domain.usecases.product.DeleteProductUseCase;
import br.com.fiap.grupo30.fastfood.domain.usecases.product.GetProductUseCase;
import br.com.fiap.grupo30.fastfood.domain.usecases.product.ListProductsByCategoryUseCase;
import br.com.fiap.grupo30.fastfood.domain.usecases.product.UpdateProductUseCase;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
Expand All @@ -19,11 +23,25 @@ public class ProductResource {

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

private final ProductUseCase productUseCase;
private final ListProductsByCategoryUseCase listProductsByCategoryUseCase;
private final GetProductUseCase getProductUseCase;
private final CreateProductUseCase createProductUseCase;
private final UpdateProductUseCase updateProductUseCase;
private final DeleteProductUseCase deleteProductUseCase;

@Autowired
public ProductResource(ProductUseCase productUseCase) {
this.productUseCase = productUseCase;
public ProductResource(
ListProductsByCategoryUseCase listProductsByCategoryUseCase,
GetProductUseCase getProductUseCase,
CreateProductUseCase createProductUseCase,
UpdateProductUseCase updateProductUseCase,
DeleteProductUseCase deleteProductUseCase) {

this.listProductsByCategoryUseCase = listProductsByCategoryUseCase;
this.getProductUseCase = getProductUseCase;
this.createProductUseCase = createProductUseCase;
this.updateProductUseCase = updateProductUseCase;
this.deleteProductUseCase = deleteProductUseCase;
}

@GetMapping
Expand All @@ -34,7 +52,7 @@ public ProductResource(ProductUseCase productUseCase) {
+ "via RequestParam. i.e., ?categoryId=1")
public ResponseEntity<List<ProductDTO>> findProductsByCategoryId(
@RequestParam(value = "categoryId", defaultValue = "0") Long categoryId) {
List<ProductDTO> list = productUseCase.findProductsByCategoryId(categoryId);
List<ProductDTO> list = this.listProductsByCategoryUseCase.execute(categoryId);
return ResponseEntity.ok().body(list);
}

Expand All @@ -43,7 +61,7 @@ public ResponseEntity<List<ProductDTO>> findProductsByCategoryId(
summary = "Get a product by ID",
description = "Retrieve a specific product based on its ID")
public ResponseEntity<ProductDTO> findProductById(@PathVariable Long id) {
ProductDTO dto = productUseCase.findProductById(id);
ProductDTO dto = this.getProductUseCase.execute(id);
return ResponseEntity.ok().body(dto);
}

Expand All @@ -52,7 +70,7 @@ public ResponseEntity<ProductDTO> findProductById(@PathVariable Long id) {
summary = "Create a new product",
description = "Create a new product and return the created product's data")
public ResponseEntity<ProductDTO> createProduct(@RequestBody @Valid ProductDTO dto) {
ProductDTO dtoCreated = productUseCase.createProduct(dto);
ProductDTO dtoCreated = this.createProductUseCase.execute(dto);
URI uri =
ServletUriComponentsBuilder.fromCurrentRequest()
.path(PATH_VARIABLE_ID)
Expand All @@ -67,7 +85,7 @@ public ResponseEntity<ProductDTO> createProduct(@RequestBody @Valid ProductDTO d
description = "Update the data of an existing product based on its ID")
public ResponseEntity<ProductDTO> updateProduct(
@PathVariable Long id, @RequestBody @Valid ProductDTO dto) {
ProductDTO dtoUpdated = productUseCase.updateProduct(id, dto);
ProductDTO dtoUpdated = this.updateProductUseCase.execute(id, dto);
return ResponseEntity.ok().body(dtoUpdated);
}

Expand All @@ -76,7 +94,7 @@ public ResponseEntity<ProductDTO> updateProduct(
summary = "Delete a product",
description = "Delete an existing product based on its ID")
public ResponseEntity<Void> deleteProduct(@PathVariable Long id) {
productUseCase.deleteProduct(id);
this.deleteProductUseCase.execute(id);
return ResponseEntity.noContent().build();
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package br.com.fiap.grupo30.fastfood.domain.usecases.customer;

import br.com.fiap.grupo30.fastfood.application.dto.CustomerDTO;
import br.com.fiap.grupo30.fastfood.application.services.CustomerService;
import org.springframework.stereotype.Component;

@Component
public class FindCustomerByCpfUseCase {

private final CustomerService customerService;

public FindCustomerByCpfUseCase(CustomerService customerService) {
this.customerService = customerService;
}

public CustomerDTO execute(String cpf) {
return customerService.findCustomerByCpf(cpf);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package br.com.fiap.grupo30.fastfood.domain.usecases.customer;

import br.com.fiap.grupo30.fastfood.application.dto.CustomerDTO;
import br.com.fiap.grupo30.fastfood.application.services.CustomerService;
import org.springframework.stereotype.Component;

@Component
public class RegisterNewCustomerUseCase {

private final CustomerService customerService;

public RegisterNewCustomerUseCase(CustomerService customerService) {
this.customerService = customerService;
}

public CustomerDTO execute(CustomerDTO dto) {
return customerService.insert(dto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package br.com.fiap.grupo30.fastfood.domain.usecases.product;

import br.com.fiap.grupo30.fastfood.application.dto.ProductDTO;
import br.com.fiap.grupo30.fastfood.application.services.ProductService;
import org.springframework.stereotype.Component;

@Component
public class CreateProductUseCase {

private final ProductService productService;

public CreateProductUseCase(ProductService productService) {
this.productService = productService;
}

public ProductDTO execute(ProductDTO product) {
return productService.insert(product);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package br.com.fiap.grupo30.fastfood.domain.usecases.product;

import br.com.fiap.grupo30.fastfood.application.services.ProductService;
import org.springframework.stereotype.Component;

@Component
public class DeleteProductUseCase {

private final ProductService productService;

public DeleteProductUseCase(ProductService productService) {
this.productService = productService;
}

public void execute(Long id) {
productService.delete(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package br.com.fiap.grupo30.fastfood.domain.usecases.product;

import br.com.fiap.grupo30.fastfood.application.dto.ProductDTO;
import br.com.fiap.grupo30.fastfood.application.services.ProductService;
import org.springframework.stereotype.Component;

@Component
public class GetProductUseCase {

private final ProductService productService;

public GetProductUseCase(ProductService productService) {
this.productService = productService;
}

public ProductDTO execute(Long id) {
return productService.findById(id);
}
}
Loading
Loading