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

Commit

Permalink
Merge pull request #50 from 7SOATSquad30/fix-clean-architecture
Browse files Browse the repository at this point in the history
fix: clean architecture
  • Loading branch information
gabrielgennaro authored Oct 5, 2024
2 parents ed7afac + 8fb40fa commit 97060a9
Show file tree
Hide file tree
Showing 31 changed files with 215 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@

public class ListAllCategoriesInMenuUseCase {

private final CategoryGateway categoryGateway;

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

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,7 @@

public class FindCustomerByCpfUseCase {

private final CustomerGateway customerGateway;

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

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,8 @@

public class RegisterNewCustomerUseCase {

private final CustomerGateway customerGateway;

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

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,12 @@

public class AddProductToOrderUseCase {

private final OrderGateway orderGateway;
private final ProductGateway productGateway;

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

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,7 @@

public class DeliverOrderUseCase {

private final OrderGateway orderGateway;

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

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,7 @@

public class FinishPreparingOrderUseCase {

private final OrderGateway orderGateway;

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

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,7 @@

public class GetOrderUseCase {

private final OrderGateway orderGateway;

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

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,7 @@

public class ListOrdersByStatusUseCase {

private final OrderGateway orderGateway;

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

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,7 @@

public class ListOrdersWithSpecificStatusesUseCase {

private final OrderGateway orderGateway;

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

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,11 @@

public class RemoveProductFromOrderUseCase {

private final OrderGateway orderGateway;
private final ProductGateway productGateway;

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

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,19 @@

public class StartNewOrderUseCase {

private final OrderGateway orderGateway;
private final CustomerGateway customerGateway;

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

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,7 @@

public class StartPreparingOrderUseCase {

private final OrderGateway orderGateway;

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

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,13 +7,7 @@

public class SubmitOrderUseCase {

private final OrderGateway orderGateway;

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

public OrderDTO execute(Long orderId) {
public OrderDTO execute(OrderGateway orderGateway, Long orderId) {
Order order = orderGateway.findById(orderId);
order.setStatus(OrderStatus.SUBMITTED);
return orderGateway.save(order).toDTO();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@
import br.com.fiap.grupo30.fastfood.domain.entities.Order;
import br.com.fiap.grupo30.fastfood.infrastructure.gateways.OrderGateway;
import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CollectOrderPaymentViaCashUseCase {
private final OrderGateway orderGateway;

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

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

order.setPaymentCollected(paidAmount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,20 @@
import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoPaymentStatus;
import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.events.MercadoPagoActionEventDTO;
import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.PaymentProcessingFailedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@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 OrderDTO execute(MercadoPagoActionEventDTO mercadoPagoPaymentEvent) {
public OrderDTO execute(
OrderGateway orderGateway,
MercadoPagoGateway mercadoPagoGateway,
MercadoPagoActionEventDTO mercadoPagoPaymentEvent) {
try {
MercadoPagoPaymentDTO payment =
this.mercadoPagoGateway.getPaymentState(
mercadoPagoPaymentEvent.getData().getId());
mercadoPagoGateway.getPaymentState(mercadoPagoPaymentEvent.getData().getId());

Order order =
this.orderGateway.findById(Long.parseLong(payment.getExternalReference()));
Order order = orderGateway.findById(Long.parseLong(payment.getExternalReference()));

if (MercadoPagoPaymentStatus.APPROVED.getValue().equals(payment.getStatus())) {
order.setPaymentCollected(payment.getTransactionAmount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,25 @@
import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.PaymentQrCodeDTO;
import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoQrCodeDTO;
import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.PaymentProcessingFailedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@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 PaymentQrCodeDTO execute(Long orderId) {
Order order = this.orderGateway.findById(orderId);
public PaymentQrCodeDTO execute(
OrderGateway orderGateway, MercadoPagoGateway mercadoPagoGateway, Long orderId) {
Order order = orderGateway.findById(orderId);

MercadoPagoQrCodeDTO qrCodeResponse;
try {
qrCodeResponse =
this.mercadoPagoGateway.createQrCodeForOrderPaymentCollection(order.toDTO());
mercadoPagoGateway.createQrCodeForOrderPaymentCollection(order.toDTO());
} catch (Exception e) {
throw new PaymentProcessingFailedException("Could not start payment processing", e);
}

order.setPaymentProcessing();
this.orderGateway.save(order);
orderGateway.save(order);

return new PaymentQrCodeDTO(qrCodeResponse.getQrData());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@

public class CreateProductUseCase {

private final ProductGateway productGateway;
private final CategoryGateway categoryGateway;

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

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,7 @@

public class DeleteProductUseCase {

private final ProductGateway productGateway;

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

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,7 @@

public class GetProductUseCase {

private final ProductGateway productGateway;

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

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,7 @@

public class ListProductsByCategoryUseCase {

private final ProductGateway productGateway;

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

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
Loading

0 comments on commit 97060a9

Please sign in to comment.