From 887ace875212d34fa610558fb41ece09ecfa809d Mon Sep 17 00:00:00 2001 From: Jonas Souza Date: Tue, 23 Jul 2024 09:28:19 -0300 Subject: [PATCH 1/3] refactor: switch the order classes to clean architecture --- .../entities => domain}/OrderStatus.java | 2 +- .../fastfood/domain/entities/Order.java | 101 +++++++++++++++++ .../fastfood/domain/entities/OrderItem.java | 59 ++++++++++ .../domain/repositories/OrderRepository.java | 27 +++++ .../order/AddProductToOrderUseCase.java | 48 +++----- .../usecases/order/DeliverOrderUseCase.java | 27 ++--- .../order/FinishPreparingOrderUseCase.java | 27 ++--- .../usecases/order/GetOrderUseCase.java | 25 ++--- .../usecases/order/ListOrdersUseCase.java | 25 ++--- .../order/RemoveProductFromOrderUseCase.java | 45 +++----- .../usecases/order/StartNewOrderUseCase.java | 85 +++++--------- .../order/StartPreparingOrderUseCase.java | 27 ++--- .../usecases/order/SubmitOrderUseCase.java | 28 ++--- .../configuration/Constants.java | 5 + .../configuration/OrderConfiguration.java | 69 ++++++++++++ .../gateways/CustomerGateway.java | 25 +++-- .../infrastructure/gateways/OrderGateway.java | 104 ++++++++++++++++++ .../gateways/ProductGateway.java | 2 +- .../persistence/entities/OrderEntity.java | 46 +++----- .../persistence/entities/OrderItemEntity.java | 17 ++- .../repositories/JpaOrderRepository.java | 2 +- .../controllers/OrderController.java | 39 +++---- .../presentation/presenters/dto/OrderDTO.java | 2 +- .../mapper/impl/OrderDTOMapper.java | 52 +++++++++ .../mapper/impl/OrderEntityMapper.java | 56 ++++++++++ 25 files changed, 654 insertions(+), 291 deletions(-) rename src/main/java/br/com/fiap/grupo30/fastfood/{infrastructure/persistence/entities => domain}/OrderStatus.java (59%) create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Order.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/OrderItem.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/OrderRepository.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/Constants.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/OrderConfiguration.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/OrderGateway.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderDTOMapper.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderEntityMapper.java diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/OrderStatus.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/OrderStatus.java similarity index 59% rename from src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/OrderStatus.java rename to src/main/java/br/com/fiap/grupo30/fastfood/domain/OrderStatus.java index 8f115c6..5aa0cd5 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/OrderStatus.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/OrderStatus.java @@ -1,4 +1,4 @@ -package br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities; +package br.com.fiap.grupo30.fastfood.domain; public enum OrderStatus { DRAFT, diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Order.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Order.java new file mode 100644 index 0000000..b6c8e70 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Order.java @@ -0,0 +1,101 @@ +package br.com.fiap.grupo30.fastfood.domain.entities; + +import br.com.fiap.grupo30.fastfood.domain.OrderStatus; +import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.CompositeDomainValidationException; +import java.util.Collection; +import java.util.LinkedList; +import java.util.Objects; + +public class Order { + + private Long id; + private OrderStatus status; + private Customer customer; + private Collection items = new LinkedList<>(); + private Double totalPrice = 0.0; + + public Order() { + this.status = OrderStatus.DRAFT; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public OrderStatus getStatus() { + return status; + } + + public void setStatus(OrderStatus status) { + this.status = status; + } + + public Customer getCustomer() { + return customer; + } + + public void setCustomer(Customer customer) { + this.customer = customer; + } + + public Collection getItems() { + return items; + } + + public void addProduct(Product product, Long quantity) { + this.items.stream() + .filter(orderItem -> orderItem.getProduct().equals(product)) + .findFirst() + .ifPresentOrElse( + existingItem -> + existingItem.setQuantity(existingItem.getQuantity() + quantity), + () -> this.items.add(new OrderItem(this, product, quantity))); + + this.recalculateTotalPrice(); + } + + public void removeProduct(Product product) { + this.items.removeIf(orderItem -> orderItem.getProduct().equals(product)); + this.recalculateTotalPrice(); + } + + public Double getTotalPrice() { + return totalPrice; + } + + public void recalculateTotalPrice() { + this.totalPrice = this.items.stream().mapToDouble(OrderItem::getTotalPrice).sum(); + } + + public void validate() { + var errors = new LinkedList(); + + if (this.status == OrderStatus.SUBMITTED && !this.hasProducts()) { + errors.add("Cannot submit order without products"); + } + + if (!errors.isEmpty()) { + throw new CompositeDomainValidationException(errors); + } + } + + private boolean hasProducts() { + return !this.items.isEmpty(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Order order)) return false; + return Objects.equals(id, order.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/OrderItem.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/OrderItem.java new file mode 100644 index 0000000..73afd5a --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/OrderItem.java @@ -0,0 +1,59 @@ +package br.com.fiap.grupo30.fastfood.domain.entities; + +public class OrderItem { + + private Long id; + + private Order order; + private Product product; + private Long quantity; + private Double totalPrice; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public OrderItem(Order order, Product product, Long quantity) { + this.order = order; + this.product = product; + this.quantity = quantity; + this.recalculateTotalPrice(); + } + + public Order getOrder() { + return order; + } + + public void setOrder(Order order) { + this.order = order; + } + + public Product getProduct() { + return product; + } + + public void setProduct(Product product) { + this.product = product; + } + + public Long getQuantity() { + return quantity; + } + + public void setQuantity(Long quantity) { + this.quantity = quantity; + this.recalculateTotalPrice(); + } + + public Double getTotalPrice() { + return totalPrice; + } + + private void recalculateTotalPrice() { + this.totalPrice = this.product.getPrice() * this.quantity; + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/OrderRepository.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/OrderRepository.java new file mode 100644 index 0000000..d25fcc5 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/OrderRepository.java @@ -0,0 +1,27 @@ +package br.com.fiap.grupo30.fastfood.domain.repositories; + +import br.com.fiap.grupo30.fastfood.domain.OrderStatus; +import br.com.fiap.grupo30.fastfood.domain.entities.Order; +import br.com.fiap.grupo30.fastfood.domain.entities.Product; +import java.util.List; + +public interface OrderRepository { + + List findOrdersByStatus(OrderStatus status); + + Order findById(Long orderId); + + Order startNewOrder(Order order); + + Order addProductToOrder(Order order, Product product, Long productQuantity); + + Order removeProductFromOrder(Order order, Product product); + + Order submitOrder(Order order); + + Order startPreparingOrder(Order order); + + Order finishPreparingOrder(Order order); + + Order deliverOrder(Order order); +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/AddProductToOrderUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/AddProductToOrderUseCase.java index aa9fee3..460823e 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/AddProductToOrderUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/AddProductToOrderUseCase.java @@ -1,42 +1,30 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.order; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.ProductEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaOrderRepository; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaProductRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; -import jakarta.transaction.Transactional; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import br.com.fiap.grupo30.fastfood.domain.OrderStatus; +import br.com.fiap.grupo30.fastfood.domain.entities.Order; +import br.com.fiap.grupo30.fastfood.domain.entities.Product; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.OrderGateway; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.ProductGateway; +import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.CantChangeOrderProductsAfterSubmitException; -@Component public class AddProductToOrderUseCase { - private final JpaOrderRepository jpaOrderRepository; - private final JpaProductRepository jpaProductRepository; + private final OrderGateway orderGateway; + private final ProductGateway productGateway; - @Autowired - public AddProductToOrderUseCase( - JpaOrderRepository jpaOrderRepository, JpaProductRepository jpaProductRepository) { - this.jpaOrderRepository = jpaOrderRepository; - this.jpaProductRepository = jpaProductRepository; + public AddProductToOrderUseCase(OrderGateway orderGateway, ProductGateway productGateway) { + this.orderGateway = orderGateway; + this.productGateway = productGateway; } - @Transactional - public OrderDTO execute(Long orderId, Long productId, Long productQuantity) { - OrderEntity order = - this.jpaOrderRepository - .findById(orderId) - .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + public Order execute(Long orderId, Long productId, Long productQuantity) { + Order order = orderGateway.findById(orderId); - ProductEntity product = - this.jpaProductRepository - .findById(productId) - .orElseThrow(() -> new ResourceNotFoundException("Product not found")); + if (order.getStatus() != OrderStatus.DRAFT) { + throw new CantChangeOrderProductsAfterSubmitException(); + } - order.addProduct(product, productQuantity); - - return this.jpaOrderRepository.save(order).toDTO(); + Product product = productGateway.findById(productId); + return orderGateway.addProductToOrder(order, product, productQuantity); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/DeliverOrderUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/DeliverOrderUseCase.java index 3fdc636..764820a 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/DeliverOrderUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/DeliverOrderUseCase.java @@ -1,35 +1,26 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.order; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderStatus; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaOrderRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; +import br.com.fiap.grupo30.fastfood.domain.OrderStatus; +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.exceptions.CantChangeOrderStatusDeliveredOtherThanReadyException; -import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -@Component public class DeliverOrderUseCase { - private final JpaOrderRepository jpaOrderRepository; + private final OrderGateway orderGateway; - @Autowired - public DeliverOrderUseCase(JpaOrderRepository jpaOrderRepository) { - this.jpaOrderRepository = jpaOrderRepository; + public DeliverOrderUseCase(OrderGateway orderGateway) { + this.orderGateway = orderGateway; } - public OrderDTO execute(Long orderId) { - OrderEntity order = - this.jpaOrderRepository - .findById(orderId) - .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + public Order execute(Long orderId) { + Order order = orderGateway.findById(orderId); if (order.getStatus() != OrderStatus.READY) { throw new CantChangeOrderStatusDeliveredOtherThanReadyException(); } order.setStatus(OrderStatus.DELIVERED); - return this.jpaOrderRepository.save(order).toDTO(); + return orderGateway.deliverOrder(order); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/FinishPreparingOrderUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/FinishPreparingOrderUseCase.java index df3dffa..a84960c 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/FinishPreparingOrderUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/FinishPreparingOrderUseCase.java @@ -1,35 +1,26 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.order; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderStatus; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaOrderRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; +import br.com.fiap.grupo30.fastfood.domain.OrderStatus; +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.exceptions.CantChangeOrderStatusReadyOtherThanPreparingException; -import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -@Component public class FinishPreparingOrderUseCase { - private final JpaOrderRepository jpaOrderRepository; + private final OrderGateway orderGateway; - @Autowired - public FinishPreparingOrderUseCase(JpaOrderRepository jpaOrderRepository) { - this.jpaOrderRepository = jpaOrderRepository; + public FinishPreparingOrderUseCase(OrderGateway orderGateway) { + this.orderGateway = orderGateway; } - public OrderDTO execute(Long orderId) { - OrderEntity order = - this.jpaOrderRepository - .findById(orderId) - .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + public Order execute(Long orderId) { + Order order = orderGateway.findById(orderId); if (order.getStatus() != OrderStatus.PREPARING) { throw new CantChangeOrderStatusReadyOtherThanPreparingException(); } order.setStatus(OrderStatus.READY); - return this.jpaOrderRepository.save(order).toDTO(); + return orderGateway.finishPreparingOrder(order); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/GetOrderUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/GetOrderUseCase.java index 799e0da..5450e9d 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/GetOrderUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/GetOrderUseCase.java @@ -1,28 +1,17 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.order; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaOrderRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import br.com.fiap.grupo30.fastfood.domain.entities.Order; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.OrderGateway; -@Component public class GetOrderUseCase { - private final JpaOrderRepository jpaOrderRepository; + private final OrderGateway orderGateway; - @Autowired - public GetOrderUseCase(JpaOrderRepository jpaOrderRepository) { - this.jpaOrderRepository = jpaOrderRepository; + public GetOrderUseCase(OrderGateway orderGateway) { + this.orderGateway = orderGateway; } - public OrderDTO execute(Long orderId) { - OrderEntity order = - this.jpaOrderRepository - .findById(orderId) - .orElseThrow(() -> new ResourceNotFoundException("Order not found")); - - return order.toDTO(); + public Order execute(Long orderId) { + return orderGateway.findById(orderId); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/ListOrdersUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/ListOrdersUseCase.java index b0aa538..312e613 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/ListOrdersUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/ListOrdersUseCase.java @@ -1,27 +1,21 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.order; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderStatus; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaOrderRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; +import br.com.fiap.grupo30.fastfood.domain.OrderStatus; +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.exceptions.InvalidOrderStatusException; -import java.util.Comparator; import java.util.List; import java.util.Locale; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -@Component public class ListOrdersUseCase { - private final JpaOrderRepository jpaOrderRepository; + private final OrderGateway orderGateway; - @Autowired - public ListOrdersUseCase(JpaOrderRepository jpaOrderRepository) { - this.jpaOrderRepository = jpaOrderRepository; + public ListOrdersUseCase(OrderGateway orderGateway) { + this.orderGateway = orderGateway; } - public List execute(String status) { + public List execute(String status) { OrderStatus orderStatus = null; if (status != null && !status.isEmpty()) { try { @@ -30,9 +24,6 @@ public List execute(String status) { throw new InvalidOrderStatusException(status, e); } } - return jpaOrderRepository.findOrdersByStatus(orderStatus).stream() - .sorted(Comparator.comparing(OrderEntity::getCreatedAt)) - .map(OrderEntity::toDTO) - .toList(); + return orderGateway.findOrdersByStatus(orderStatus); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/RemoveProductFromOrderUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/RemoveProductFromOrderUseCase.java index 1320bb4..0decef3 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/RemoveProductFromOrderUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/RemoveProductFromOrderUseCase.java @@ -1,39 +1,30 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.order; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.ProductEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaOrderRepository; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaProductRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import br.com.fiap.grupo30.fastfood.domain.OrderStatus; +import br.com.fiap.grupo30.fastfood.domain.entities.Order; +import br.com.fiap.grupo30.fastfood.domain.entities.Product; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.OrderGateway; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.ProductGateway; +import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.CantChangeOrderProductsAfterSubmitException; -@Component public class RemoveProductFromOrderUseCase { - private final JpaOrderRepository jpaOrderRepository; - private final JpaProductRepository jpaProductRepository; + private final OrderGateway orderGateway; + private final ProductGateway productGateway; - @Autowired - public RemoveProductFromOrderUseCase( - JpaOrderRepository jpaOrderRepository, JpaProductRepository jpaProductRepository) { - this.jpaOrderRepository = jpaOrderRepository; - this.jpaProductRepository = jpaProductRepository; + public RemoveProductFromOrderUseCase(OrderGateway orderGateway, ProductGateway productGateway) { + this.orderGateway = orderGateway; + this.productGateway = productGateway; } - public OrderDTO execute(Long orderId, Long productId) { - OrderEntity order = - this.jpaOrderRepository - .findById(orderId) - .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + public Order execute(Long orderId, Long productId) { + Order order = orderGateway.findById(orderId); - ProductEntity product = - this.jpaProductRepository - .findById(productId) - .orElseThrow(() -> new ResourceNotFoundException("Product not found")); + if (order.getStatus() != OrderStatus.DRAFT) { + throw new CantChangeOrderProductsAfterSubmitException(); + } - order.removeProduct(product); - return this.jpaOrderRepository.save(order).toDTO(); + Product product = productGateway.findById(productId); + return orderGateway.removeProductFromOrder(order, product); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/StartNewOrderUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/StartNewOrderUseCase.java index 51199b0..529fa2d 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/StartNewOrderUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/StartNewOrderUseCase.java @@ -1,69 +1,44 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.order; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.CustomerEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaCustomerRepository; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaOrderRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CustomerDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.CustomerEntityMapper; -import jakarta.transaction.Transactional; -import java.util.Optional; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import br.com.fiap.grupo30.fastfood.domain.entities.Customer; +import br.com.fiap.grupo30.fastfood.domain.entities.Order; +import br.com.fiap.grupo30.fastfood.domain.valueobjects.CPF; +import br.com.fiap.grupo30.fastfood.infrastructure.configuration.Constants; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.CustomerGateway; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.OrderGateway; -@Component public class StartNewOrderUseCase { - private final JpaOrderRepository jpaOrderRepository; - private final JpaCustomerRepository jpaCustomerRepository; - public final CustomerEntityMapper customerEntityMapper; + private final OrderGateway orderGateway; + private final CustomerGateway customerGateway; - @Autowired - public StartNewOrderUseCase( - JpaOrderRepository jpaOrderRepository, - JpaCustomerRepository jpaCustomerRepository, - CustomerEntityMapper customerEntityMapper) { - this.jpaOrderRepository = jpaOrderRepository; - this.jpaCustomerRepository = jpaCustomerRepository; - this.customerEntityMapper = customerEntityMapper; + public StartNewOrderUseCase(OrderGateway orderGateway, CustomerGateway customerGateway) { + this.orderGateway = orderGateway; + this.customerGateway = customerGateway; } - @Transactional - public OrderDTO execute(CustomerDTO dto) { - CustomerDTO customerDTO = dto; - if (customerDTO == null) { - customerDTO = findOrCreateAnonymousCustomer(); - } - - CustomerEntity customerEntity = - jpaCustomerRepository - .findById(customerDTO.getId()) - .orElseThrow(() -> new ResourceNotFoundException("Customer not found")); - - OrderEntity newOrder = OrderEntity.create(); - newOrder.setCustomer(customerEntity); - - newOrder = this.jpaOrderRepository.save(newOrder); - return newOrder.toDTO(); + public Order execute(Customer domainObj) { + Customer customer = findOrCreateAnonymousCustomer(domainObj); + Order newOrder = new Order(); + newOrder.setCustomer(customer); + return orderGateway.startNewOrder(newOrder); } - private CustomerDTO findOrCreateAnonymousCustomer() { - String anonymousCpf = "970.410.008-69"; - Optional anonymousCustomer = - jpaCustomerRepository.findCustomerByCpf(anonymousCpf); - - if (anonymousCustomer.isPresent()) { - return new CustomerDTO(customerEntityMapper.mapFrom(anonymousCustomer.get())); + private Customer findOrCreateAnonymousCustomer(Customer domainObj) { + if (domainObj != null) { + return customerGateway.findCustomerByCpf(domainObj.getCpf().value()); } else { - CustomerEntity newAnonymousCustomer = new CustomerEntity(); - newAnonymousCustomer.setCpf(anonymousCpf); - newAnonymousCustomer.setName("Anonymous"); - newAnonymousCustomer.setEmail("anonymous@fastfood.com"); - CustomerEntity savedAnonymousCustomer = - jpaCustomerRepository.save(newAnonymousCustomer); - return new CustomerDTO(customerEntityMapper.mapFrom(savedAnonymousCustomer)); + Customer anonymousCustomer = customerGateway.findCustomerByCpf(Constants.ANONYMOUS_CPF); + if (anonymousCustomer != null) { + return anonymousCustomer; + } else { + Customer newAnonymousCustomer = new Customer(); + String anonymousCpf = CPF.removeNonDigits(Constants.ANONYMOUS_CPF); + newAnonymousCustomer.setCpf(new CPF(anonymousCpf)); + newAnonymousCustomer.setName("Anonymous"); + newAnonymousCustomer.setEmail("anonymous@fastfood.com"); + return customerGateway.insert(newAnonymousCustomer); + } } } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/StartPreparingOrderUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/StartPreparingOrderUseCase.java index 5665a6b..4334b01 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/StartPreparingOrderUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/StartPreparingOrderUseCase.java @@ -1,35 +1,26 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.order; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderStatus; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaOrderRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; +import br.com.fiap.grupo30.fastfood.domain.OrderStatus; +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.exceptions.CantChangeOrderStatusPreparingOtherThanSubmittedException; -import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -@Component public class StartPreparingOrderUseCase { - private final JpaOrderRepository jpaOrderRepository; + private final OrderGateway orderGateway; - @Autowired - public StartPreparingOrderUseCase(JpaOrderRepository jpaOrderRepository) { - this.jpaOrderRepository = jpaOrderRepository; + public StartPreparingOrderUseCase(OrderGateway orderGateway) { + this.orderGateway = orderGateway; } - public OrderDTO execute(Long orderId) { - OrderEntity order = - this.jpaOrderRepository - .findById(orderId) - .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + public Order execute(Long orderId) { + Order order = orderGateway.findById(orderId); if (order.getStatus() != OrderStatus.SUBMITTED) { throw new CantChangeOrderStatusPreparingOtherThanSubmittedException(); } order.setStatus(OrderStatus.PREPARING); - return this.jpaOrderRepository.save(order).toDTO(); + return orderGateway.startPreparingOrder(order); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/SubmitOrderUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/SubmitOrderUseCase.java index dc23256..cd7ea60 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/SubmitOrderUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/SubmitOrderUseCase.java @@ -1,30 +1,20 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.order; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderStatus; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaOrderRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import br.com.fiap.grupo30.fastfood.domain.OrderStatus; +import br.com.fiap.grupo30.fastfood.domain.entities.Order; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.OrderGateway; -@Component public class SubmitOrderUseCase { - private final JpaOrderRepository jpaOrderRepository; + private final OrderGateway orderGateway; - @Autowired - public SubmitOrderUseCase(JpaOrderRepository jpaOrderRepository) { - this.jpaOrderRepository = jpaOrderRepository; + public SubmitOrderUseCase(OrderGateway orderGateway) { + this.orderGateway = orderGateway; } - public OrderDTO execute(Long orderId) { - OrderEntity order = - this.jpaOrderRepository - .findById(orderId) - .orElseThrow(() -> new ResourceNotFoundException("Order not found")); - + public Order execute(Long orderId) { + Order order = orderGateway.findById(orderId); order.setStatus(OrderStatus.SUBMITTED); - return this.jpaOrderRepository.save(order).toDTO(); + return orderGateway.submitOrder(order); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/Constants.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/Constants.java new file mode 100644 index 0000000..9d50847 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/Constants.java @@ -0,0 +1,5 @@ +package br.com.fiap.grupo30.fastfood.infrastructure.configuration; + +public class Constants { + public static final String ANONYMOUS_CPF = "970.410.008-69"; +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/OrderConfiguration.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/OrderConfiguration.java new file mode 100644 index 0000000..de46087 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/OrderConfiguration.java @@ -0,0 +1,69 @@ +package br.com.fiap.grupo30.fastfood.infrastructure.configuration; + +import br.com.fiap.grupo30.fastfood.domain.repositories.OrderRepository; +import br.com.fiap.grupo30.fastfood.domain.usecases.order.*; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.CustomerGateway; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.OrderGateway; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.ProductGateway; +import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaOrderRepository; +import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.OrderEntityMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class OrderConfiguration { + + @Bean + public OrderRepository orderRepository( + JpaOrderRepository jpaOrderRepository, OrderEntityMapper orderEntityMapper) { + return new OrderGateway(jpaOrderRepository, orderEntityMapper); + } + + @Bean + public ListOrdersUseCase listOrdersUseCase(OrderGateway orderGateway) { + return new ListOrdersUseCase(orderGateway); + } + + @Bean + public GetOrderUseCase getOrderUseCase(OrderGateway orderGateway) { + return new GetOrderUseCase(orderGateway); + } + + @Bean + public StartNewOrderUseCase startNewOrderUseCase( + OrderGateway orderGateway, CustomerGateway customerGateway) { + return new StartNewOrderUseCase(orderGateway, customerGateway); + } + + @Bean + public AddProductToOrderUseCase addProductToOrderUseCase( + OrderGateway orderGateway, ProductGateway productGateway) { + return new AddProductToOrderUseCase(orderGateway, productGateway); + } + + @Bean + public RemoveProductFromOrderUseCase removeProductFromOrderUseCase( + OrderGateway orderGateway, ProductGateway productGateway) { + return new RemoveProductFromOrderUseCase(orderGateway, productGateway); + } + + @Bean + public SubmitOrderUseCase submitOrderUseCase(OrderGateway orderGateway) { + return new SubmitOrderUseCase(orderGateway); + } + + @Bean + public StartPreparingOrderUseCase startPreparingOrderUseCase(OrderGateway orderGateway) { + return new StartPreparingOrderUseCase(orderGateway); + } + + @Bean + public FinishPreparingOrderUseCase finishPreparingOrderUseCase(OrderGateway orderGateway) { + return new FinishPreparingOrderUseCase(orderGateway); + } + + @Bean + public DeliverOrderUseCase deliverOrderUseCase(OrderGateway orderGateway) { + return new DeliverOrderUseCase(orderGateway); + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/CustomerGateway.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/CustomerGateway.java index 87bec53..d463f6d 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/CustomerGateway.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/CustomerGateway.java @@ -3,15 +3,18 @@ import br.com.fiap.grupo30.fastfood.domain.entities.Customer; import br.com.fiap.grupo30.fastfood.domain.repositories.CustomerRepository; import br.com.fiap.grupo30.fastfood.domain.valueobjects.CPF; +import br.com.fiap.grupo30.fastfood.infrastructure.configuration.Constants; import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.CustomerEntity; import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaCustomerRepository; import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceConflictException; import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.CustomerEntityMapper; -import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Optional; @Service public class CustomerGateway implements CustomerRepository { @@ -21,18 +24,24 @@ public class CustomerGateway implements CustomerRepository { @Autowired public CustomerGateway( - JpaCustomerRepository jpaCustomerRepository, - CustomerEntityMapper customerEntityMapper) { + JpaCustomerRepository jpaCustomerRepository, + CustomerEntityMapper customerEntityMapper) { this.jpaCustomerRepository = jpaCustomerRepository; this.customerEntityMapper = customerEntityMapper; } @Override + @Transactional(readOnly = true) public Customer findCustomerByCpf(String cpf) { String cpfStr = CPF.removeNonDigits(cpf); - Optional obj = jpaCustomerRepository.findCustomerByCpf(cpfStr); - CustomerEntity entity = - obj.orElseThrow(() -> new ResourceNotFoundException("Entity not found")); + Optional obj; + CustomerEntity entity; + if (Constants.ANONYMOUS_CPF.equals(cpf)) { + obj = jpaCustomerRepository.findCustomerByCpf(cpfStr); + return obj.map(this.customerEntityMapper::mapFrom).orElse(null); + } + obj = jpaCustomerRepository.findCustomerByCpf(cpfStr); + entity = obj.orElseThrow(() -> new ResourceNotFoundException("Customer not found")); return this.customerEntityMapper.mapFrom(entity); } @@ -40,11 +49,11 @@ public Customer findCustomerByCpf(String cpf) { public Customer insert(Customer customer) { try { CustomerEntity entity = - jpaCustomerRepository.save(customerEntityMapper.mapTo(customer)); + jpaCustomerRepository.save(customerEntityMapper.mapTo(customer)); return this.customerEntityMapper.mapFrom(entity); } catch (DataIntegrityViolationException e) { throw new ResourceConflictException( - "CPF already exists: " + customer.getCpf().value(), e); + "CPF already exists: " + customer.getCpf().value(), e); } } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/OrderGateway.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/OrderGateway.java new file mode 100644 index 0000000..7b4943e --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/OrderGateway.java @@ -0,0 +1,104 @@ +package br.com.fiap.grupo30.fastfood.infrastructure.gateways; + +import br.com.fiap.grupo30.fastfood.domain.OrderStatus; +import br.com.fiap.grupo30.fastfood.domain.entities.Order; +import br.com.fiap.grupo30.fastfood.domain.entities.Product; +import br.com.fiap.grupo30.fastfood.domain.repositories.OrderRepository; +import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderEntity; +import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaOrderRepository; +import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; +import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.OrderEntityMapper; +import jakarta.persistence.EntityNotFoundException; +import java.util.Comparator; +import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class OrderGateway implements OrderRepository { + + private final JpaOrderRepository jpaOrderRepository; + private final OrderEntityMapper orderEntityMapper; + + @Autowired + public OrderGateway( + JpaOrderRepository jpaOrderRepository, OrderEntityMapper orderEntityMapper) { + this.jpaOrderRepository = jpaOrderRepository; + this.orderEntityMapper = orderEntityMapper; + } + + @Override + public List findOrdersByStatus(OrderStatus orderStatus) { + return jpaOrderRepository.findOrdersByStatus(orderStatus).stream() + .sorted(Comparator.comparing(OrderEntity::getCreatedAt)) + .map(orderEntityMapper::mapFrom) + .toList(); + } + + @Override + @Transactional(readOnly = true) + public Order findById(Long orderId) { + OrderEntity entity = + jpaOrderRepository + .findById(orderId) + .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + return orderEntityMapper.mapFrom(entity); + } + + @Override + @Transactional + public Order startNewOrder(Order order) { + OrderEntity entity = jpaOrderRepository.save(orderEntityMapper.mapTo(order)); + return orderEntityMapper.mapFrom(entity); + } + + @Override + @Transactional + public Order addProductToOrder(Order order, Product product, Long productQuantity) { + order.addProduct(product, productQuantity); + return saveUpdatedOrder(order); + } + + @Override + @Transactional + public Order removeProductFromOrder(Order order, Product product) { + order.removeProduct(product); + return saveUpdatedOrder(order); + } + + @Override + @Transactional + public Order submitOrder(Order order) { + return saveUpdatedOrder(order); + } + + @Override + @Transactional + public Order startPreparingOrder(Order order) { + return saveUpdatedOrder(order); + } + + @Override + @Transactional + public Order finishPreparingOrder(Order order) { + return saveUpdatedOrder(order); + } + + @Override + @Transactional + public Order deliverOrder(Order order) { + return saveUpdatedOrder(order); + } + + private Order saveUpdatedOrder(Order order) { + try { + OrderEntity entity = jpaOrderRepository.getReferenceById(order.getId()); + orderEntityMapper.updateEntityFromOrder(entity, order); + entity = jpaOrderRepository.save(entity); + return orderEntityMapper.mapFrom(entity); + } catch (EntityNotFoundException e) { + throw new ResourceNotFoundException("Id not found " + order.getId(), e); + } + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/ProductGateway.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/ProductGateway.java index c638ffa..78554b9 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/ProductGateway.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/ProductGateway.java @@ -43,7 +43,7 @@ public List findProductsByCategoryId(Long categoryId) { public Product findById(Long id) { Optional obj = jpaProductRepository.findById(id); ProductEntity entity = - obj.orElseThrow(() -> new ResourceNotFoundException("Entity not found")); + obj.orElseThrow(() -> new ResourceNotFoundException("Product not found")); return this.productEntityMapper.mapFrom(entity); } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/OrderEntity.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/OrderEntity.java index bb3425e..4ae5df8 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/OrderEntity.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/OrderEntity.java @@ -1,20 +1,19 @@ package br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities; +import br.com.fiap.grupo30.fastfood.domain.OrderStatus; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderItemDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.CantChangeOrderProductsAfterSubmitException; import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.CompositeDomainValidationException; import jakarta.persistence.*; import java.time.Instant; import java.util.Collection; import java.util.LinkedList; -import lombok.AccessLevel; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; @Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) +@NoArgsConstructor @EqualsAndHashCode @Entity @Table(name = "tb_order") @@ -56,11 +55,19 @@ public class OrderEntity { @Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE") private Instant deletedAt; - public void addProduct(ProductEntity product, Long quantity) { - if (!this.isDraft()) { - throw new CantChangeOrderProductsAfterSubmitException(); - } + public void setId(Long id) { + this.id = id; + } + + public void setStatus(OrderStatus status) { + this.status = status; + } + public void setCustomer(CustomerEntity customer) { + this.customer = customer; + } + + public void addProduct(ProductEntity product, Long quantity) { this.items.stream() .filter(orderItem -> orderItem.getProduct().equals(product)) .findFirst() @@ -73,35 +80,18 @@ public void addProduct(ProductEntity product, Long quantity) { } public void removeProduct(ProductEntity product) { - if (!this.isDraft()) { - throw new CantChangeOrderProductsAfterSubmitException(); - } - this.items.removeIf(orderItem -> orderItem.getProduct().equals(product)); - this.recalculateTotalPrice(); } - public Double getTotalPrice() { - return this.totalPrice; - } - - private void recalculateTotalPrice() { - this.totalPrice = this.items.stream().mapToDouble(item -> item.getTotalPrice()).sum(); - } - - private Boolean isDraft() { - return OrderStatus.DRAFT.equals(this.status); + public void recalculateTotalPrice() { + this.totalPrice = this.items.stream().mapToDouble(OrderItemEntity::getTotalPrice).sum(); } private Boolean hasProducts() { return !this.items.isEmpty(); } - public void setStatus(OrderStatus status) { - this.status = status; - } - public void validate() { var errors = new LinkedList(); @@ -148,10 +138,6 @@ public static OrderEntity create() { return order; } - public void setCustomer(CustomerEntity customer) { - this.customer = customer; - } - public void setPaymentProcessing() { this.payment.setStatus(PaymentStatus.PROCESSING); this.payment.setAmount(this.getTotalPrice()); diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/OrderItemEntity.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/OrderItemEntity.java index e783040..e4395dd 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/OrderItemEntity.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/OrderItemEntity.java @@ -3,11 +3,12 @@ import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderItemDTO; import jakarta.persistence.*; import jakarta.validation.constraints.Min; -import lombok.AccessLevel; import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; -@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Getter +@NoArgsConstructor @EqualsAndHashCode @Entity @Table(name = "tb_order_item") @@ -33,8 +34,8 @@ public class OrderItemEntity { @Min(0) private Double totalPrice = 0.0; - public Long getQuantity() { - return this.quantity; + public void setId(Long id) { + this.id = id; } public void setQuantity(Long quantity) { @@ -42,12 +43,8 @@ public void setQuantity(Long quantity) { this.recalculateTotalPrice(); } - public Double getTotalPrice() { - return this.totalPrice; - } - - public ProductEntity getProduct() { - return this.product; + public void setProduct(ProductEntity product) { + this.product = product; } private void recalculateTotalPrice() { diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/repositories/JpaOrderRepository.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/repositories/JpaOrderRepository.java index c06933d..c3cd185 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/repositories/JpaOrderRepository.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/repositories/JpaOrderRepository.java @@ -1,7 +1,7 @@ package br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories; +import br.com.fiap.grupo30.fastfood.domain.OrderStatus; import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderStatus; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/OrderController.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/OrderController.java index b2a4f25..dd3c219 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/OrderController.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/OrderController.java @@ -1,12 +1,12 @@ package br.com.fiap.grupo30.fastfood.presentation.controllers; +import br.com.fiap.grupo30.fastfood.domain.entities.Customer; import br.com.fiap.grupo30.fastfood.domain.usecases.customer.FindCustomerByCpfUseCase; import br.com.fiap.grupo30.fastfood.domain.usecases.order.*; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.AddCustomerCpfRequest; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.AddOrderProductRequest; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CustomerDTO; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.CustomerDTOMapper; +import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.OrderDTOMapper; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import java.net.URI; @@ -31,7 +31,7 @@ public class OrderController { private final FinishPreparingOrderUseCase finishPreparingOrderUseCase; private final DeliverOrderUseCase deliverOrderUseCase; private final FindCustomerByCpfUseCase findCustomerByCpfUseCase; - private final CustomerDTOMapper customerDTOMapper; + private final OrderDTOMapper orderDTOMapper; @Autowired public OrderController( @@ -45,7 +45,7 @@ public OrderController( FinishPreparingOrderUseCase finishPreparingOrderUseCase, DeliverOrderUseCase deliverOrderUseCase, FindCustomerByCpfUseCase findCustomerByCpfUseCase, - CustomerDTOMapper customerDTOMapper) { + OrderDTOMapper orderDTOMapper) { this.startNewOrderUseCase = startNewOrderUseCase; this.addProductToOrderUseCase = addProductToOrderUseCase; this.removeProductFromOrderUseCase = removeProductFromOrderUseCase; @@ -56,7 +56,7 @@ public OrderController( this.finishPreparingOrderUseCase = finishPreparingOrderUseCase; this.deliverOrderUseCase = deliverOrderUseCase; this.findCustomerByCpfUseCase = findCustomerByCpfUseCase; - this.customerDTOMapper = customerDTOMapper; + this.orderDTOMapper = orderDTOMapper; } @GetMapping @@ -67,7 +67,8 @@ public OrderController( + " sorted by date via RequestParam. i.e., ?status=") public ResponseEntity> findOrdersByStatus( @RequestParam(value = "status", required = false) String status) { - List list = listAllOrdersUseCase.execute(status); + List list = + listAllOrdersUseCase.execute(status).stream().map(orderDTOMapper::mapTo).toList(); return ResponseEntity.ok().body(list); } @@ -76,7 +77,7 @@ public ResponseEntity> findOrdersByStatus( summary = "Get an order by ID", description = "Retrieve a specific order based on its ID") public ResponseEntity findById(@PathVariable Long orderId) { - OrderDTO order = this.getOrderUseCase.execute(orderId); + OrderDTO order = orderDTOMapper.mapTo(getOrderUseCase.execute(orderId)); return ResponseEntity.ok().body(order); } @@ -86,13 +87,11 @@ public ResponseEntity findById(@PathVariable Long orderId) { description = "Create a new order and return the new order's data") public ResponseEntity startNewOrder( @RequestBody(required = false) AddCustomerCpfRequest request) { - CustomerDTO customerDTO = null; + Customer customer = null; if (request != null && request.getCpf() != null && !request.getCpf().isEmpty()) { - customerDTO = - this.customerDTOMapper.mapTo( - findCustomerByCpfUseCase.execute(request.getCpf())); + customer = findCustomerByCpfUseCase.execute(request.getCpf()); } - OrderDTO order = this.startNewOrderUseCase.execute(customerDTO); + OrderDTO order = orderDTOMapper.mapTo(startNewOrderUseCase.execute(customer)); URI uri = ServletUriComponentsBuilder.fromCurrentRequest() .path("/{orderId}") @@ -106,8 +105,9 @@ public ResponseEntity startNewOrder( public ResponseEntity addProduct( @PathVariable Long orderId, @RequestBody AddOrderProductRequest request) { OrderDTO order = - this.addProductToOrderUseCase.execute( - orderId, request.getProductId(), request.getQuantity()); + orderDTOMapper.mapTo( + addProductToOrderUseCase.execute( + orderId, request.getProductId(), request.getQuantity())); return ResponseEntity.ok().body(order); } @@ -117,7 +117,8 @@ public ResponseEntity addProduct( description = "Removes a product from an order") public ResponseEntity removeProduct( @PathVariable Long orderId, @PathVariable Long productId) { - OrderDTO order = this.removeProductFromOrderUseCase.execute(orderId, productId); + OrderDTO order = + orderDTOMapper.mapTo(removeProductFromOrderUseCase.execute(orderId, productId)); return ResponseEntity.ok().body(order); } @@ -126,7 +127,7 @@ public ResponseEntity removeProduct( summary = "Submit an order for preparation", description = "Submits an order for preparation and return the order's data") public ResponseEntity submitOrder(@PathVariable Long orderId) { - OrderDTO order = this.submitOrderUseCase.execute(orderId); + OrderDTO order = orderDTOMapper.mapTo(submitOrderUseCase.execute(orderId)); return ResponseEntity.ok().body(order); } @@ -135,7 +136,7 @@ public ResponseEntity submitOrder(@PathVariable Long orderId) { summary = "Start preparing an order", description = "Start preparing an order and return the order's data") public ResponseEntity startPreparingOrder(@PathVariable Long orderId) { - OrderDTO order = this.startPreparingOrderUseCase.execute(orderId); + OrderDTO order = orderDTOMapper.mapTo(startPreparingOrderUseCase.execute(orderId)); return ResponseEntity.ok().body(order); } @@ -144,7 +145,7 @@ public ResponseEntity startPreparingOrder(@PathVariable Long orderId) summary = "Finish preparing an order", description = "Finish preparing an order and return the order's data") public ResponseEntity finishPreparingOrder(@PathVariable Long orderId) { - OrderDTO order = this.finishPreparingOrderUseCase.execute(orderId); + OrderDTO order = orderDTOMapper.mapTo(finishPreparingOrderUseCase.execute(orderId)); return ResponseEntity.ok().body(order); } @@ -153,7 +154,7 @@ public ResponseEntity finishPreparingOrder(@PathVariable Long orderId) summary = "Deliver an order", description = "Deliver an order and return the order's data") public ResponseEntity deliverOrder(@PathVariable Long orderId) { - OrderDTO order = this.deliverOrderUseCase.execute(orderId); + OrderDTO order = orderDTOMapper.mapTo(deliverOrderUseCase.execute(orderId)); return ResponseEntity.ok().body(order); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/OrderDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/OrderDTO.java index 9471f67..29e1bcf 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/OrderDTO.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/OrderDTO.java @@ -1,6 +1,6 @@ package br.com.fiap.grupo30.fastfood.presentation.presenters.dto; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderStatus; +import br.com.fiap.grupo30.fastfood.domain.OrderStatus; import lombok.AllArgsConstructor; import lombok.Data; diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderDTOMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderDTOMapper.java new file mode 100644 index 0000000..7bc4900 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderDTOMapper.java @@ -0,0 +1,52 @@ +package br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl; + +import br.com.fiap.grupo30.fastfood.domain.entities.Order; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderItemDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.Mapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public final class OrderDTOMapper implements Mapper { + + private final CustomerDTOMapper customerDTOMapper; + private final ProductDTOMapper productDTOMapper; + + @Autowired + public OrderDTOMapper(CustomerDTOMapper customerDTOMapper, ProductDTOMapper productDTOMapper) { + this.customerDTOMapper = customerDTOMapper; + this.productDTOMapper = productDTOMapper; + } + + @Override + public OrderDTO mapTo(Order order) { + return new OrderDTO( + order.getId(), + order.getStatus(), + order.getItems().stream() + .map( + orderItem -> + new OrderItemDTO( + orderItem.getQuantity(), + orderItem.getTotalPrice(), + productDTOMapper.mapTo(orderItem.getProduct()))) + .toArray(OrderItemDTO[]::new), + order.getTotalPrice(), + customerDTOMapper.mapTo(order.getCustomer())); + } + + @Override + public Order mapFrom(OrderDTO dto) { + Order order = new Order(); + order.setId(dto.getOrderId()); + order.setStatus(dto.getStatus()); + order.setCustomer(customerDTOMapper.mapFrom(dto.getCustomer())); + for (OrderItemDTO orderItemDTO : dto.getItems()) { + order.addProduct( + productDTOMapper.mapFrom(orderItemDTO.getProduct()), + orderItemDTO.getQuantity()); + } + return order; + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderEntityMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderEntityMapper.java new file mode 100644 index 0000000..0fac523 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderEntityMapper.java @@ -0,0 +1,56 @@ +package br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl; + +import br.com.fiap.grupo30.fastfood.domain.entities.Order; +import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderEntity; +import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.Mapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public final class OrderEntityMapper implements Mapper { + private final CustomerEntityMapper customerEntityMapper; + private final ProductEntityMapper productEntityMapper; + + @Autowired + public OrderEntityMapper( + CustomerEntityMapper customerEntityMapper, ProductEntityMapper productEntityMapper) { + this.customerEntityMapper = customerEntityMapper; + this.productEntityMapper = productEntityMapper; + } + + @Override + public OrderEntity mapTo(Order order) { + OrderEntity entity = new OrderEntity(); + entity.setId(order.getId()); + updateEntityFromOrder(entity, order); + return entity; + } + + @Override + public Order mapFrom(OrderEntity entity) { + Order order = new Order(); + order.setId(entity.getId()); + order.setStatus(entity.getStatus()); + order.setCustomer(customerEntityMapper.mapFrom(entity.getCustomer())); + entity.getItems() + .forEach( + itemEntity -> + order.addProduct( + productEntityMapper.mapFrom(itemEntity.getProduct()), + itemEntity.getQuantity())); + return order; + } + + public void updateEntityFromOrder(OrderEntity entity, Order order) { + entity.setStatus(order.getStatus()); + entity.setCustomer(customerEntityMapper.mapTo(order.getCustomer())); + entity.setStatus(order.getStatus()); + entity.getItems().clear(); + order.getItems() + .forEach( + item -> + entity.addProduct( + productEntityMapper.mapTo(item.getProduct()), + item.getQuantity())); + } +} From 2dee6c9ed3341245bdb5e39ecd0b4049181405b8 Mon Sep 17 00:00:00 2001 From: Jonas Souza Date: Tue, 23 Jul 2024 11:11:17 -0300 Subject: [PATCH 2/3] fix: code lint --- .../infrastructure/gateways/CustomerGateway.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/CustomerGateway.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/CustomerGateway.java index d463f6d..feebc3f 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/CustomerGateway.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/CustomerGateway.java @@ -9,13 +9,12 @@ import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceConflictException; import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.CustomerEntityMapper; +import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.Optional; - @Service public class CustomerGateway implements CustomerRepository { @@ -24,8 +23,8 @@ public class CustomerGateway implements CustomerRepository { @Autowired public CustomerGateway( - JpaCustomerRepository jpaCustomerRepository, - CustomerEntityMapper customerEntityMapper) { + JpaCustomerRepository jpaCustomerRepository, + CustomerEntityMapper customerEntityMapper) { this.jpaCustomerRepository = jpaCustomerRepository; this.customerEntityMapper = customerEntityMapper; } @@ -49,11 +48,11 @@ public Customer findCustomerByCpf(String cpf) { public Customer insert(Customer customer) { try { CustomerEntity entity = - jpaCustomerRepository.save(customerEntityMapper.mapTo(customer)); + jpaCustomerRepository.save(customerEntityMapper.mapTo(customer)); return this.customerEntityMapper.mapFrom(entity); } catch (DataIntegrityViolationException e) { throw new ResourceConflictException( - "CPF already exists: " + customer.getCpf().value(), e); + "CPF already exists: " + customer.getCpf().value(), e); } } } From 3a1148a4187f4e0160f7935d8b44667828a6f9d7 Mon Sep 17 00:00:00 2001 From: Murilo Kakazu Date: Sat, 27 Jul 2024 00:33:26 -0300 Subject: [PATCH 3/3] refactor: payment mappers, domain entity and dto --- .../fastfood/domain/entities/Order.java | 9 ++++ .../fastfood/domain/entities/Payment.java | 46 +++++++++++++++++++ .../persistence/entities/PaymentEntity.java | 2 +- .../presenters/dto/PaymentDTO.java | 3 ++ .../mapper/impl/OrderDTOMapper.java | 15 ++++-- .../mapper/impl/OrderEntityMapper.java | 4 +- .../mapper/impl/PaymentDTOMapper.java | 27 +++++++++++ 7 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Payment.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/PaymentDTOMapper.java diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Order.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Order.java index b6c8e70..33168fa 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Order.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Order.java @@ -11,6 +11,7 @@ public class Order { private Long id; private OrderStatus status; private Customer customer; + private Payment payment; private Collection items = new LinkedList<>(); private Double totalPrice = 0.0; @@ -42,6 +43,14 @@ public void setCustomer(Customer customer) { this.customer = customer; } + public Payment getPayment() { + return payment; + } + + public void setPayment(Payment payment) { + this.payment = payment; + } + public Collection getItems() { return items; } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Payment.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Payment.java new file mode 100644 index 0000000..23e6a5f --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Payment.java @@ -0,0 +1,46 @@ +package br.com.fiap.grupo30.fastfood.domain.entities; + +import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.PaymentStatus; +import java.util.Objects; + +public class Payment { + private Long id; + private PaymentStatus status; + private Double amount; + + public Long getId() { + return id; + } + + public PaymentStatus getStatus() { + return status; + } + + public Double getAmount() { + return amount; + } + + public void setId(Long newId) { + this.id = newId; + } + + public void setStatus(PaymentStatus newStatus) { + this.status = newStatus; + } + + public void setAmount(Double newAmount) { + this.amount = newAmount; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Payment payment)) return false; + return Objects.equals(id, payment.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/PaymentEntity.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/PaymentEntity.java index c9ab645..88a6732 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/PaymentEntity.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/PaymentEntity.java @@ -79,7 +79,7 @@ public static PaymentEntity create(OrderEntity order) { } public PaymentDTO toDTO() { - PaymentDTO dto = new PaymentDTO(this.status, this.amount); + PaymentDTO dto = new PaymentDTO(this.id, this.status, this.amount); return dto; } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/PaymentDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/PaymentDTO.java index 77443b9..535852f 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/PaymentDTO.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/PaymentDTO.java @@ -3,10 +3,13 @@ import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.PaymentStatus; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; @Data @AllArgsConstructor +@NoArgsConstructor public class PaymentDTO { + private Long id; private PaymentStatus status; private Double amount; } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderDTOMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderDTOMapper.java index 7bc4900..91d3ab9 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderDTOMapper.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderDTOMapper.java @@ -3,20 +3,25 @@ import br.com.fiap.grupo30.fastfood.domain.entities.Order; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderItemDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.Mapper; +import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.BiDirectionalMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component -public final class OrderDTOMapper implements Mapper { +public final class OrderDTOMapper implements BiDirectionalMapper { private final CustomerDTOMapper customerDTOMapper; private final ProductDTOMapper productDTOMapper; + private final PaymentDTOMapper paymentDTOMapper; @Autowired - public OrderDTOMapper(CustomerDTOMapper customerDTOMapper, ProductDTOMapper productDTOMapper) { + public OrderDTOMapper( + CustomerDTOMapper customerDTOMapper, + ProductDTOMapper productDTOMapper, + PaymentDTOMapper paymentDTOMapper) { this.customerDTOMapper = customerDTOMapper; this.productDTOMapper = productDTOMapper; + this.paymentDTOMapper = paymentDTOMapper; } @Override @@ -33,7 +38,8 @@ public OrderDTO mapTo(Order order) { productDTOMapper.mapTo(orderItem.getProduct()))) .toArray(OrderItemDTO[]::new), order.getTotalPrice(), - customerDTOMapper.mapTo(order.getCustomer())); + customerDTOMapper.mapTo(order.getCustomer()), + paymentDTOMapper.mapTo(order.getPayment())); } @Override @@ -42,6 +48,7 @@ public Order mapFrom(OrderDTO dto) { order.setId(dto.getOrderId()); order.setStatus(dto.getStatus()); order.setCustomer(customerDTOMapper.mapFrom(dto.getCustomer())); + order.setPayment(paymentDTOMapper.mapFrom(dto.getPayment())); for (OrderItemDTO orderItemDTO : dto.getItems()) { order.addProduct( productDTOMapper.mapFrom(orderItemDTO.getProduct()), diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderEntityMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderEntityMapper.java index 0fac523..2d3e47b 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderEntityMapper.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderEntityMapper.java @@ -2,12 +2,12 @@ import br.com.fiap.grupo30.fastfood.domain.entities.Order; import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderEntity; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.Mapper; +import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.BiDirectionalMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component -public final class OrderEntityMapper implements Mapper { +public final class OrderEntityMapper implements BiDirectionalMapper { private final CustomerEntityMapper customerEntityMapper; private final ProductEntityMapper productEntityMapper; diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/PaymentDTOMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/PaymentDTOMapper.java new file mode 100644 index 0000000..be8a52d --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/PaymentDTOMapper.java @@ -0,0 +1,27 @@ +package br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl; + +import br.com.fiap.grupo30.fastfood.domain.entities.Payment; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.PaymentDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.BiDirectionalMapper; +import org.springframework.stereotype.Component; + +@Component +public final class PaymentDTOMapper implements BiDirectionalMapper { + @Override + public PaymentDTO mapTo(Payment payment) { + PaymentDTO dto = new PaymentDTO(); + dto.setId(payment.getId()); + dto.setStatus(payment.getStatus()); + dto.setAmount(payment.getAmount()); + return dto; + } + + @Override + public Payment mapFrom(PaymentDTO dto) { + Payment payment = new Payment(); + payment.setId(dto.getId()); + payment.setStatus(dto.getStatus()); + payment.setAmount(dto.getAmount()); + return payment; + } +}