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

Commit

Permalink
Ajustes na implementação do clean architecture, instanciando o gatewa…
Browse files Browse the repository at this point in the history
…y na controller e passando para o use case utilizar
  • Loading branch information
gabrielgennaro committed Sep 30, 2024
1 parent 5c294b5 commit a2680c0
Show file tree
Hide file tree
Showing 27 changed files with 100 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@

public class ListAllCategoriesInMenuUseCase {

private final CategoryGateway categoryGateway;

public ListAllCategoriesInMenuUseCase(CategoryGateway categoryGateway) {
this.categoryGateway = categoryGateway;
public ListAllCategoriesInMenuUseCase() {
}

public List<CategoryDTO> execute() {
public List<CategoryDTO> execute(CategoryGateway categoryGateway) {
return categoryGateway.findAll().stream().map(Category::toDTO).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@

public class FindCustomerByCpfUseCase {

private final CustomerGateway customerGateway;

public FindCustomerByCpfUseCase(CustomerGateway customerGateway) {
this.customerGateway = customerGateway;
public FindCustomerByCpfUseCase() {
}

public CustomerDTO execute(String cpf) {
public CustomerDTO execute(CustomerGateway customerGateway, String cpf) {
if (!CPF.isValid(cpf)) {
throw new InvalidCpfException(cpf);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

public class RegisterNewCustomerUseCase {

private final CustomerGateway customerGateway;

public RegisterNewCustomerUseCase(CustomerGateway customerGateway) {
this.customerGateway = customerGateway;
public RegisterNewCustomerUseCase() {
}

public CustomerDTO execute(String name, String cpf, String email) {
public CustomerDTO execute(CustomerGateway customerGateway, String name, String cpf, String email) {
if (!CPF.isValid(cpf)) {
throw new InvalidCpfException(cpf);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@

public class AddProductToOrderUseCase {

private final OrderGateway orderGateway;
private final ProductGateway productGateway;

public AddProductToOrderUseCase(OrderGateway orderGateway, ProductGateway productGateway) {
this.orderGateway = orderGateway;
this.productGateway = productGateway;
public AddProductToOrderUseCase() {
}

public OrderDTO execute(Long orderId, Long productId, Long productQuantity) {
public OrderDTO execute(OrderGateway orderGateway, ProductGateway productGateway, Long orderId, Long productId, Long productQuantity) {
Order order = orderGateway.findByIdForUpdate(orderId);
Product product = productGateway.findById(productId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

public class DeliverOrderUseCase {

private final OrderGateway orderGateway;

public DeliverOrderUseCase(OrderGateway orderGateway) {
this.orderGateway = orderGateway;
public DeliverOrderUseCase() {
}

public OrderDTO execute(Long orderId) {
public OrderDTO execute(OrderGateway orderGateway, Long orderId) {
Order order = orderGateway.findById(orderId);

if (order.getStatus() != OrderStatus.READY) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

public class FinishPreparingOrderUseCase {

private final OrderGateway orderGateway;

public FinishPreparingOrderUseCase(OrderGateway orderGateway) {
this.orderGateway = orderGateway;
public FinishPreparingOrderUseCase() {
}

public OrderDTO execute(Long orderId) {
public OrderDTO execute(OrderGateway orderGateway, Long orderId) {
Order order = orderGateway.findById(orderId);

if (order.getStatus() != OrderStatus.PREPARING) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@

public class GetOrderUseCase {

private final OrderGateway orderGateway;

public GetOrderUseCase(OrderGateway orderGateway) {
this.orderGateway = orderGateway;
public GetOrderUseCase() {
}

public OrderDTO execute(Long orderId) {
public OrderDTO execute(OrderGateway orderGateway, Long orderId) {
return orderGateway.findById(orderId).toDTO();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@

public class ListOrdersByStatusUseCase {

private final OrderGateway orderGateway;

public ListOrdersByStatusUseCase(OrderGateway orderGateway) {
this.orderGateway = orderGateway;
public ListOrdersByStatusUseCase() {
}

public List<OrderDTO> execute(String status) {
public List<OrderDTO> execute(OrderGateway orderGateway, String status) {
OrderStatus statusFilter = null;
if (status != null && !status.isEmpty()) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@

public class ListOrdersWithSpecificStatusesUseCase {

private final OrderGateway orderGateway;

public ListOrdersWithSpecificStatusesUseCase(OrderGateway orderGateway) {
this.orderGateway = orderGateway;
public ListOrdersWithSpecificStatusesUseCase() {
}

public List<OrderDTO> execute() {
public List<OrderDTO> execute(OrderGateway orderGateway) {
return orderGateway.findOrdersWithSpecificStatuses().stream().map(Order::toDTO).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@

public class RemoveProductFromOrderUseCase {

private final OrderGateway orderGateway;
private final ProductGateway productGateway;

public RemoveProductFromOrderUseCase(OrderGateway orderGateway, ProductGateway productGateway) {
this.orderGateway = orderGateway;
this.productGateway = productGateway;
public RemoveProductFromOrderUseCase() {
}

public OrderDTO execute(Long orderId, Long productId) {
public OrderDTO execute(OrderGateway orderGateway, ProductGateway productGateway, Long orderId, Long productId) {
Order order = orderGateway.findById(orderId);
Product product = productGateway.findById(productId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,20 @@

public class StartNewOrderUseCase {

private final OrderGateway orderGateway;
private final CustomerGateway customerGateway;

public StartNewOrderUseCase(OrderGateway orderGateway, CustomerGateway customerGateway) {
this.orderGateway = orderGateway;
this.customerGateway = customerGateway;
public StartNewOrderUseCase() {
}

public OrderDTO execute(String customerCpf) {
public OrderDTO execute(OrderGateway orderGateway, CustomerGateway customerGateway, String customerCpf) {
if (!CPF.isValid(customerCpf)) {
throw new InvalidCpfException(customerCpf);
}

Customer customer = findCustomerOrCreateAnonymous(new CPF(customerCpf));
Customer customer = findCustomerOrCreateAnonymous(customerGateway, new CPF(customerCpf));
Order newOrder = Order.createFor(customer);
return orderGateway.save(newOrder).toDTO();
}

private Customer findCustomerOrCreateAnonymous(CPF customerCpf) {
private Customer findCustomerOrCreateAnonymous(CustomerGateway customerGateway, CPF customerCpf) {
if (customerCpf != null) {
return customerGateway.findCustomerByCpf(customerCpf.value());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

public class StartPreparingOrderUseCase {

private final OrderGateway orderGateway;

public StartPreparingOrderUseCase(OrderGateway orderGateway) {
this.orderGateway = orderGateway;
public StartPreparingOrderUseCase() {
}

public OrderDTO execute(Long orderId) {
public OrderDTO execute(OrderGateway orderGateway, Long orderId) {
Order order = orderGateway.findById(orderId);

if (order.getStatus() != OrderStatus.SUBMITTED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

public class SubmitOrderUseCase {

private final OrderGateway orderGateway;

public SubmitOrderUseCase(OrderGateway orderGateway) {
this.orderGateway = orderGateway;
public SubmitOrderUseCase() {
}

public OrderDTO execute(Long orderId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@

@Component
public class CollectOrderPaymentViaCashUseCase {
private final OrderGateway orderGateway;

@Autowired
public CollectOrderPaymentViaCashUseCase(OrderGateway orderGateway) {
this.orderGateway = orderGateway;
public CollectOrderPaymentViaCashUseCase() {
}

public OrderDTO execute(Long orderId, Double paidAmount) {
public OrderDTO execute(OrderGateway orderGateway, Long orderId, Double paidAmount) {
Order order = this.orderGateway.findById(orderId);

order.setPaymentCollected(paidAmount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,12 @@

@Component
public class CollectOrderPaymentViaMercadoPagoUseCase {
private final OrderGateway orderGateway;
private final MercadoPagoGateway mercadoPagoGateway;


@Autowired
public CollectOrderPaymentViaMercadoPagoUseCase(
OrderGateway orderGateway, MercadoPagoGateway mercadoPagoGateway) {
this.orderGateway = orderGateway;
this.mercadoPagoGateway = mercadoPagoGateway;
public CollectOrderPaymentViaMercadoPagoUseCase() {
}

public OrderDTO execute(MercadoPagoActionEventDTO mercadoPagoPaymentEvent) {
public OrderDTO execute(OrderGateway orderGateway, MercadoPagoGateway mercadoPagoGateway, MercadoPagoActionEventDTO mercadoPagoPaymentEvent) {
try {
MercadoPagoPaymentDTO payment =
this.mercadoPagoGateway.getPaymentState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,12 @@

@Component
public class GeneratePaymentQrCodeUseCase {
private final OrderGateway orderGateway;
private final MercadoPagoGateway mercadoPagoGateway;

@Autowired
public GeneratePaymentQrCodeUseCase(
OrderGateway orderGateway, MercadoPagoGateway mercadoPagoGateway) {
this.orderGateway = orderGateway;
this.mercadoPagoGateway = mercadoPagoGateway;
public GeneratePaymentQrCodeUseCase() {
}

public PaymentQrCodeDTO execute(Long orderId) {
public PaymentQrCodeDTO execute(OrderGateway orderGateway, MercadoPagoGateway mercadoPagoGateway, Long orderId) {
Order order = this.orderGateway.findById(orderId);

MercadoPagoQrCodeDTO qrCodeResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
import br.com.fiap.grupo30.fastfood.infrastructure.gateways.ProductGateway;
import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.ProductDTO;

public class CreateProductUseCase {

private final ProductGateway productGateway;
private final CategoryGateway categoryGateway;

public CreateProductUseCase(ProductGateway productGateway, CategoryGateway categoryGateway) {
this.productGateway = productGateway;
this.categoryGateway = categoryGateway;
public class CreateProductUseCase
{
public CreateProductUseCase() {
}

public ProductDTO execute(
String name, String description, Double price, String imgUrl, String category) {
Category categoryEntity = this.categoryGateway.findOne(category);
ProductGateway productGateway, CategoryGateway categoryGateway,
String name, String description, Double price, String imgUrl, String category)
{
Category categoryEntity = categoryGateway.findOne(category);
Product product = Product.create(name, description, price, imgUrl, categoryEntity);
return productGateway.save(product).toDTO();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

public class DeleteProductUseCase {

private final ProductGateway productGateway;

public DeleteProductUseCase(ProductGateway productGateway) {
this.productGateway = productGateway;
public DeleteProductUseCase() {
}

public void execute(Long id) {
public void execute(ProductGateway productGateway, Long id) {
productGateway.delete(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@

public class GetProductUseCase {

private final ProductGateway productGateway;

public GetProductUseCase(ProductGateway productGateway) {
this.productGateway = productGateway;
public GetProductUseCase() {
}

public ProductDTO execute(Long id) {
public ProductDTO execute(ProductGateway productGateway, Long id) {
return productGateway.findById(id).toDTO();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@

public class ListProductsByCategoryUseCase {

private final ProductGateway productGateway;

public ListProductsByCategoryUseCase(ProductGateway productGateway) {
this.productGateway = productGateway;
public ListProductsByCategoryUseCase() {
}

public List<ProductDTO> execute(Long categoryId) {
public List<ProductDTO> execute(ProductGateway productGateway, Long categoryId) {
return productGateway.findProductsByCategoryId(categoryId).stream()
.map(Product::toDTO)
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@

public class UpdateProductUseCase {

private final ProductGateway productGateway;
private final CategoryGateway categoryGateway;

public UpdateProductUseCase(ProductGateway productGateway, CategoryGateway categoryGateway) {
this.productGateway = productGateway;
this.categoryGateway = categoryGateway;
public UpdateProductUseCase() {
}

public ProductDTO execute(
ProductGateway productGateway, CategoryGateway categoryGateway,
Long productId,
String name,
String description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public CategoryController(ListAllCategoriesInMenuUseCase listAllCategoriesInMenu
summary = "Get all categories",
description = "Retrieve a list of all registered categories")
public ResponseEntity<List<CategoryDTO>> findAll() {
List<CategoryDTO> categories = this.listAllCategoriesInMenuUseCase.execute();
CategoryGateway categoryGateway = new CategoryGateway();
List<CategoryDTO> categories = this.listAllCategoriesInMenuUseCase.execute(categoryGateway);
return ResponseEntity.ok().body(categories);
}
}
Loading

0 comments on commit a2680c0

Please sign in to comment.