diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/out/mercadopago/MercadoPagoRequestBuilder.java b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/out/mercadopago/MercadoPagoRequestBuilder.java index 8028346..88d8e6e 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/out/mercadopago/MercadoPagoRequestBuilder.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/out/mercadopago/MercadoPagoRequestBuilder.java @@ -36,7 +36,7 @@ public class MercadoPagoRequestBuilder { theirOrderItem.setDescription( ourOrderItem.getProduct().getDescription()); theirOrderItem.setCategory( - ourOrderItem.getProduct().getCategory().getName()); + ourOrderItem.getProduct().getCategory()); theirOrderItem.setQuantity(ourOrderItem.getQuantity()); theirOrderItem.setUnitPrice( ourOrderItem.getProduct().getPrice()); diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/PaymentStatus.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/PaymentStatus.java similarity index 56% rename from src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/PaymentStatus.java rename to src/main/java/br/com/fiap/grupo30/fastfood/domain/PaymentStatus.java index c3bf27d..673555e 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/PaymentStatus.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/PaymentStatus.java @@ -1,4 +1,4 @@ -package br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities; +package br.com.fiap.grupo30.fastfood.domain; public enum PaymentStatus { NOT_SUBMITTED, diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Category.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Category.java index 97347e2..0a5df5d 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Category.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Category.java @@ -1,44 +1,41 @@ package br.com.fiap.grupo30.fastfood.domain.entities; +import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.CategoryEntity; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CategoryDTO; import java.util.Objects; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +@Getter +@Setter +@AllArgsConstructor public class Category { private Long id; private String name; - public Category() {} - - public Category(Long id, String name) { - this.id = id; - this.name = name; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; + public static Category create(String name) { + return new Category(null, name); } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Category category)) return false; - return Objects.equals(id, category.id); + return Objects.equals(name, category.name); } @Override public int hashCode() { - return Objects.hash(id); + return Objects.hash(name); + } + + public CategoryDTO toDTO() { + return new CategoryDTO(name); + } + + public CategoryEntity toPersistence() { + return new CategoryEntity(id, name); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Customer.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Customer.java index f82e691..241dc94 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Customer.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Customer.java @@ -1,64 +1,43 @@ package br.com.fiap.grupo30.fastfood.domain.entities; import br.com.fiap.grupo30.fastfood.domain.valueobjects.CPF; +import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.CustomerEntity; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CustomerDTO; import java.util.Objects; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +@Getter +@Setter +@AllArgsConstructor public class Customer { private Long id; private String name; private CPF cpf; private String email; - public Customer() {} - - public Customer(Long id, String name, CPF cpf, String email) { - this.id = id; - this.name = name; - this.cpf = cpf; - this.email = email; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public CPF getCpf() { - return cpf; - } - - public void setCpf(CPF cpf) { - this.cpf = cpf; - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; + public static Customer create(String name, String cpf, String email) { + return new Customer(null, name, new CPF(cpf), email); } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Customer customer)) return false; - return Objects.equals(id, customer.id); + return Objects.equals(cpf.value(), customer.cpf.value()); } @Override public int hashCode() { - return Objects.hash(id); + return Objects.hash(cpf.value()); + } + + public CustomerDTO toDTO() { + return new CustomerDTO(name, cpf.value(), email); + } + + public CustomerEntity toPersistence() { + return new CustomerEntity(id, name, cpf.value(), email); } } 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 33168fa..3a04c58 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 @@ -1,6 +1,10 @@ package br.com.fiap.grupo30.fastfood.domain.entities; import br.com.fiap.grupo30.fastfood.domain.OrderStatus; +import br.com.fiap.grupo30.fastfood.domain.PaymentStatus; +import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderEntity; +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.CompositeDomainValidationException; import java.util.Collection; import java.util.LinkedList; @@ -12,19 +16,30 @@ public class Order { private OrderStatus status; private Customer customer; private Payment payment; - private Collection items = new LinkedList<>(); + private Collection items; private Double totalPrice = 0.0; - public Order() { - this.status = OrderStatus.DRAFT; + public static Order createFor(Customer customer) { + return new Order( + null, OrderStatus.DRAFT, customer, Payment.create(), new LinkedList()); } - public Long getId() { - return id; + public Order( + Long id, + OrderStatus status, + Customer customer, + Payment payment, + Collection items) { + this.id = id; + this.status = status; + this.customer = customer; + this.payment = payment; + this.items = items; + recalculateTotalPrice(); } - public void setId(Long id) { - this.id = id; + public Long getId() { + return id; } public OrderStatus getStatus() { @@ -47,8 +62,19 @@ public Payment getPayment() { return payment; } - public void setPayment(Payment payment) { - this.payment = payment; + public void setPaymentProcessing() { + this.payment.setStatus(PaymentStatus.PROCESSING); + this.payment.setAmount(this.getTotalPrice()); + } + + public void setPaymentCollected(Double paymentCollectedAmount) { + this.payment.setStatus(PaymentStatus.COLLECTED); + this.payment.setAmount(paymentCollectedAmount); + } + + public void setPaymentRejected() { + this.payment.setStatus(PaymentStatus.REJECTED); + this.payment.setAmount(this.getTotalPrice()); } public Collection getItems() { @@ -62,7 +88,7 @@ public void addProduct(Product product, Long quantity) { .ifPresentOrElse( existingItem -> existingItem.setQuantity(existingItem.getQuantity() + quantity), - () -> this.items.add(new OrderItem(this, product, quantity))); + () -> this.items.add(new OrderItem(product, quantity))); this.recalculateTotalPrice(); } @@ -76,7 +102,7 @@ public Double getTotalPrice() { return totalPrice; } - public void recalculateTotalPrice() { + private void recalculateTotalPrice() { this.totalPrice = this.items.stream().mapToDouble(OrderItem::getTotalPrice).sum(); } @@ -87,6 +113,11 @@ public void validate() { errors.add("Cannot submit order without products"); } + if (this.status == OrderStatus.PREPARING + && !PaymentStatus.COLLECTED.equals(this.getPayment().getStatus())) { + errors.add("Can not start peparing order without collecting payment"); + } + if (!errors.isEmpty()) { throw new CompositeDomainValidationException(errors); } @@ -107,4 +138,23 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hash(id); } + + public OrderDTO toDTO() { + return new OrderDTO( + id, + status, + items.stream().map(item -> item.toDTO()).toArray(OrderItemDTO[]::new), + totalPrice, + customer.toDTO(), + payment.toDTO()); + } + + public OrderEntity toPersistence() { + return new OrderEntity( + id, + status, + customer.toPersistence(), + payment.toPersistence(), + items.stream().map(OrderItem::toPersistence).toList()); + } } 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 index 73afd5a..4f4a6a5 100644 --- 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 @@ -1,59 +1,44 @@ package br.com.fiap.grupo30.fastfood.domain.entities; -public class OrderItem { - - private Long id; +import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.OrderItemEntity; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderItemDTO; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; - private Order order; +@Getter +public class OrderItem { private Product product; private Long quantity; - private Double totalPrice; - - public Long getId() { - return id; - } - public void setId(Long id) { - this.id = id; - } + @Setter(AccessLevel.NONE) + private Double totalPrice; - public OrderItem(Order order, Product product, Long quantity) { - this.order = order; + public OrderItem(Product product, Long quantity) { 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; + recalculateTotalPrice(); } public void setProduct(Product product) { this.product = product; - } - - public Long getQuantity() { - return quantity; + recalculateTotalPrice(); } public void setQuantity(Long quantity) { this.quantity = quantity; - this.recalculateTotalPrice(); - } - - public Double getTotalPrice() { - return totalPrice; + recalculateTotalPrice(); } private void recalculateTotalPrice() { this.totalPrice = this.product.getPrice() * this.quantity; } + + public OrderItemDTO toDTO() { + return new OrderItemDTO(product.toDTO(), quantity, totalPrice); + } + + public OrderItemEntity toPersistence() { + return new OrderItemEntity(product.toPersistence(), quantity, totalPrice); + } } 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 index 23e6a5f..61e54ec 100644 --- 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 @@ -1,35 +1,23 @@ package br.com.fiap.grupo30.fastfood.domain.entities; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.PaymentStatus; +import br.com.fiap.grupo30.fastfood.domain.PaymentStatus; +import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.PaymentEntity; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.PaymentDTO; import java.util.Objects; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +@Getter +@Setter +@AllArgsConstructor 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; + public static Payment create() { + return new Payment(null, PaymentStatus.NOT_SUBMITTED, 0.0); } @Override @@ -43,4 +31,12 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hash(id); } + + public PaymentDTO toDTO() { + return new PaymentDTO(status, amount); + } + + public PaymentEntity toPersistence() { + return new PaymentEntity(id, status, amount); + } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Product.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Product.java index d8aa698..ca26f02 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Product.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/entities/Product.java @@ -1,7 +1,15 @@ package br.com.fiap.grupo30.fastfood.domain.entities; +import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.ProductEntity; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.ProductDTO; import java.util.Objects; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +@Getter +@Setter +@AllArgsConstructor public class Product { private Long id; @@ -11,69 +19,9 @@ public class Product { private String imgUrl; private Category category; - public Product() {} - - public Product( - Long id, - String name, - String description, - Double price, - String imgUrl, - Category category) { - this.id = id; - this.name = name; - this.description = description; - this.price = price; - this.imgUrl = imgUrl; - this.category = category; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public Double getPrice() { - return price; - } - - public void setPrice(Double price) { - this.price = price; - } - - public String getImgUrl() { - return imgUrl; - } - - public void setImgUrl(String imgUrl) { - this.imgUrl = imgUrl; - } - - public Category getCategory() { - return category; - } - - public void setCategory(Category category) { - this.category = category; + public static Product create( + String name, String description, Double price, String imgUrl, Category category) { + return new Product(null, name, description, price, imgUrl, category); } @Override @@ -87,4 +35,12 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hash(id); } + + public ProductDTO toDTO() { + return new ProductDTO(id, name, description, price, imgUrl, category.getName()); + } + + public ProductEntity toPersistence() { + return new ProductEntity(id, name, description, price, imgUrl, category.toPersistence()); + } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/CategoryRepository.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/CategoryRepository.java index 55298e2..a7b96af 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/CategoryRepository.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/CategoryRepository.java @@ -5,4 +5,6 @@ public interface CategoryRepository { List findAll(); + + Category findOne(String category); } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/CustomerRepository.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/CustomerRepository.java index 620515a..91760b9 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/CustomerRepository.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/CustomerRepository.java @@ -3,8 +3,7 @@ import br.com.fiap.grupo30.fastfood.domain.entities.Customer; public interface CustomerRepository { - Customer findCustomerByCpf(String cpf); - Customer insert(Customer dto); + Customer save(Customer dto); } 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 index d25fcc5..5d17761 100644 --- 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 @@ -2,26 +2,14 @@ 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 findByIdForUpdate(Long orderId); - Order deliverOrder(Order order); + Order save(Order order); } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/ProductRepository.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/ProductRepository.java index b6c2ce6..61723ba 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/ProductRepository.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/ProductRepository.java @@ -4,14 +4,11 @@ import java.util.List; public interface ProductRepository { - List findProductsByCategoryId(Long categoryId); Product findById(Long id); - Product insert(Product product); - - Product update(Long id, Product product); + Product save(Product product); void delete(Long id); } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/category/ListAllCategoriesInMenuUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/category/ListAllCategoriesInMenuUseCase.java index b09da43..3b737d9 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/category/ListAllCategoriesInMenuUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/category/ListAllCategoriesInMenuUseCase.java @@ -2,6 +2,7 @@ import br.com.fiap.grupo30.fastfood.domain.entities.Category; import br.com.fiap.grupo30.fastfood.infrastructure.gateways.CategoryGateway; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CategoryDTO; import java.util.List; public class ListAllCategoriesInMenuUseCase { @@ -12,7 +13,7 @@ public ListAllCategoriesInMenuUseCase(CategoryGateway categoryGateway) { this.categoryGateway = categoryGateway; } - public List execute() { - return categoryGateway.findAll(); + public List execute() { + return categoryGateway.findAll().stream().map(Category::toDTO).toList(); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/customer/FindCustomerByCpfUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/customer/FindCustomerByCpfUseCase.java index 828486e..734a712 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/customer/FindCustomerByCpfUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/customer/FindCustomerByCpfUseCase.java @@ -1,7 +1,9 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.customer; -import br.com.fiap.grupo30.fastfood.domain.entities.Customer; +import br.com.fiap.grupo30.fastfood.domain.valueobjects.CPF; import br.com.fiap.grupo30.fastfood.infrastructure.gateways.CustomerGateway; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CustomerDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.InvalidCpfException; public class FindCustomerByCpfUseCase { @@ -11,7 +13,11 @@ public FindCustomerByCpfUseCase(CustomerGateway customerGateway) { this.customerGateway = customerGateway; } - public Customer execute(String cpf) { - return customerGateway.findCustomerByCpf(cpf); + public CustomerDTO execute(String cpf) { + if (!CPF.isValid(cpf)) { + throw new InvalidCpfException(cpf); + } + + return customerGateway.findCustomerByCpf(CPF.removeNonDigits(cpf)).toDTO(); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/customer/RegisterNewCustomerUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/customer/RegisterNewCustomerUseCase.java index 36be88f..7a3df2a 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/customer/RegisterNewCustomerUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/customer/RegisterNewCustomerUseCase.java @@ -1,7 +1,10 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.customer; import br.com.fiap.grupo30.fastfood.domain.entities.Customer; +import br.com.fiap.grupo30.fastfood.domain.valueobjects.CPF; import br.com.fiap.grupo30.fastfood.infrastructure.gateways.CustomerGateway; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CustomerDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.InvalidCpfException; public class RegisterNewCustomerUseCase { @@ -11,7 +14,12 @@ public RegisterNewCustomerUseCase(CustomerGateway customerGateway) { this.customerGateway = customerGateway; } - public Customer execute(Customer customer) { - return customerGateway.insert(customer); + public CustomerDTO execute(String name, String cpf, String email) { + if (!CPF.isValid(cpf)) { + throw new InvalidCpfException(cpf); + } + + Customer customer = Customer.create(name, cpf, email); + return customerGateway.save(customer).toDTO(); } } 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 460823e..adc5cd1 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 @@ -5,6 +5,7 @@ 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.dto.OrderDTO; import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.CantChangeOrderProductsAfterSubmitException; public class AddProductToOrderUseCase { @@ -17,14 +18,16 @@ public AddProductToOrderUseCase(OrderGateway orderGateway, ProductGateway produc this.productGateway = productGateway; } - public Order execute(Long orderId, Long productId, Long productQuantity) { - Order order = orderGateway.findById(orderId); + public OrderDTO execute(Long orderId, Long productId, Long productQuantity) { + Order order = orderGateway.findByIdForUpdate(orderId); + Product product = productGateway.findById(productId); if (order.getStatus() != OrderStatus.DRAFT) { throw new CantChangeOrderProductsAfterSubmitException(); } - Product product = productGateway.findById(productId); - return orderGateway.addProductToOrder(order, product, productQuantity); + order.addProduct(product, productQuantity); + + return orderGateway.save(order).toDTO(); } } 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 764820a..95775cd 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 @@ -3,6 +3,7 @@ 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.dto.OrderDTO; import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.CantChangeOrderStatusDeliveredOtherThanReadyException; public class DeliverOrderUseCase { @@ -13,7 +14,7 @@ public DeliverOrderUseCase(OrderGateway orderGateway) { this.orderGateway = orderGateway; } - public Order execute(Long orderId) { + public OrderDTO execute(Long orderId) { Order order = orderGateway.findById(orderId); if (order.getStatus() != OrderStatus.READY) { @@ -21,6 +22,6 @@ public Order execute(Long orderId) { } order.setStatus(OrderStatus.DELIVERED); - return orderGateway.deliverOrder(order); + return orderGateway.save(order).toDTO(); } } 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 a84960c..0599f45 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 @@ -3,6 +3,7 @@ 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.dto.OrderDTO; import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.CantChangeOrderStatusReadyOtherThanPreparingException; public class FinishPreparingOrderUseCase { @@ -13,7 +14,7 @@ public FinishPreparingOrderUseCase(OrderGateway orderGateway) { this.orderGateway = orderGateway; } - public Order execute(Long orderId) { + public OrderDTO execute(Long orderId) { Order order = orderGateway.findById(orderId); if (order.getStatus() != OrderStatus.PREPARING) { @@ -21,6 +22,6 @@ public Order execute(Long orderId) { } order.setStatus(OrderStatus.READY); - return orderGateway.finishPreparingOrder(order); + return orderGateway.save(order).toDTO(); } } 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 5450e9d..e807a15 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,7 +1,7 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.order; -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; public class GetOrderUseCase { @@ -11,7 +11,7 @@ public GetOrderUseCase(OrderGateway orderGateway) { this.orderGateway = orderGateway; } - public Order execute(Long orderId) { - return orderGateway.findById(orderId); + public OrderDTO execute(Long orderId) { + return orderGateway.findById(orderId).toDTO(); } } 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 312e613..3a65973 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 @@ -3,6 +3,7 @@ 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.dto.OrderDTO; import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.InvalidOrderStatusException; import java.util.List; import java.util.Locale; @@ -15,15 +16,15 @@ public ListOrdersUseCase(OrderGateway orderGateway) { this.orderGateway = orderGateway; } - public List execute(String status) { - OrderStatus orderStatus = null; + public List execute(String status) { + OrderStatus statusFilter = null; if (status != null && !status.isEmpty()) { try { - orderStatus = OrderStatus.valueOf(status.toUpperCase(Locale.ENGLISH)); + statusFilter = OrderStatus.valueOf(status.toUpperCase(Locale.ENGLISH)); } catch (IllegalArgumentException e) { throw new InvalidOrderStatusException(status, e); } } - return orderGateway.findOrdersByStatus(orderStatus); + return orderGateway.findOrdersByStatus(statusFilter).stream().map(Order::toDTO).toList(); } } 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 0decef3..3d44ae8 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 @@ -5,6 +5,7 @@ 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.dto.OrderDTO; import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.CantChangeOrderProductsAfterSubmitException; public class RemoveProductFromOrderUseCase { @@ -17,14 +18,16 @@ public RemoveProductFromOrderUseCase(OrderGateway orderGateway, ProductGateway p this.productGateway = productGateway; } - public Order execute(Long orderId, Long productId) { + public OrderDTO execute(Long orderId, Long productId) { Order order = orderGateway.findById(orderId); + Product product = productGateway.findById(productId); if (order.getStatus() != OrderStatus.DRAFT) { throw new CantChangeOrderProductsAfterSubmitException(); } - Product product = productGateway.findById(productId); - return orderGateway.removeProductFromOrder(order, product); + order.removeProduct(product); + + return orderGateway.save(order).toDTO(); } } 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 529fa2d..9129361 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 @@ -6,6 +6,8 @@ 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; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.InvalidCpfException; public class StartNewOrderUseCase { @@ -17,27 +19,28 @@ public StartNewOrderUseCase(OrderGateway orderGateway, CustomerGateway customerG this.customerGateway = customerGateway; } - public Order execute(Customer domainObj) { - Customer customer = findOrCreateAnonymousCustomer(domainObj); - Order newOrder = new Order(); - newOrder.setCustomer(customer); - return orderGateway.startNewOrder(newOrder); + public OrderDTO execute(String customerCpf) { + if (!CPF.isValid(customerCpf)) { + throw new InvalidCpfException(customerCpf); + } + + Customer customer = findCustomerOrCreateAnonymous(new CPF(customerCpf)); + Order newOrder = Order.createFor(customer); + return orderGateway.save(newOrder).toDTO(); } - private Customer findOrCreateAnonymousCustomer(Customer domainObj) { - if (domainObj != null) { - return customerGateway.findCustomerByCpf(domainObj.getCpf().value()); + private Customer findCustomerOrCreateAnonymous(CPF customerCpf) { + if (customerCpf != null) { + return customerGateway.findCustomerByCpf(customerCpf.value()); } else { 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); + Customer newAnonymousCustomer = + Customer.create( + "Anonymous", Constants.ANONYMOUS_CPF, "anonymous@fastfood.com"); + return customerGateway.save(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 4334b01..fbcf014 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 @@ -3,6 +3,7 @@ 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.dto.OrderDTO; import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.CantChangeOrderStatusPreparingOtherThanSubmittedException; public class StartPreparingOrderUseCase { @@ -13,7 +14,7 @@ public StartPreparingOrderUseCase(OrderGateway orderGateway) { this.orderGateway = orderGateway; } - public Order execute(Long orderId) { + public OrderDTO execute(Long orderId) { Order order = orderGateway.findById(orderId); if (order.getStatus() != OrderStatus.SUBMITTED) { @@ -21,6 +22,6 @@ public Order execute(Long orderId) { } order.setStatus(OrderStatus.PREPARING); - return orderGateway.startPreparingOrder(order); + return orderGateway.save(order).toDTO(); } } 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 cd7ea60..975dfd2 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 @@ -3,6 +3,7 @@ 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.dto.OrderDTO; public class SubmitOrderUseCase { @@ -12,9 +13,9 @@ public SubmitOrderUseCase(OrderGateway orderGateway) { this.orderGateway = orderGateway; } - public Order execute(Long orderId) { + public OrderDTO execute(Long orderId) { Order order = orderGateway.findById(orderId); order.setStatus(OrderStatus.SUBMITTED); - return orderGateway.submitOrder(order); + return orderGateway.save(order).toDTO(); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/CollectOrderPaymentViaCashUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/CollectOrderPaymentViaCashUseCase.java index 45d09d5..b1454c3 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/CollectOrderPaymentViaCashUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/CollectOrderPaymentViaCashUseCase.java @@ -1,30 +1,26 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.payment; -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.domain.entities.Order; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.OrderGateway; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CollectPaymentViaCashRequest; 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; @Component public class CollectOrderPaymentViaCashUseCase { - private final JpaOrderRepository orderRepository; + private final OrderGateway orderGateway; @Autowired - public CollectOrderPaymentViaCashUseCase(JpaOrderRepository orderRepository) { - this.orderRepository = orderRepository; + public CollectOrderPaymentViaCashUseCase(OrderGateway orderGateway) { + this.orderGateway = orderGateway; } public OrderDTO execute(Long orderId, CollectPaymentViaCashRequest payment) { - OrderEntity order = - this.orderRepository - .findById(orderId) - .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + Order order = this.orderGateway.findById(orderId); order.setPaymentCollected(payment.getAmount()); - return this.orderRepository.save(order).toDTO(); + return orderGateway.save(order).toDTO(); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/CollectOrderPaymentViaMercadoPagoUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/CollectOrderPaymentViaMercadoPagoUseCase.java index 355f87b..d17dfd3 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/CollectOrderPaymentViaMercadoPagoUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/CollectOrderPaymentViaMercadoPagoUseCase.java @@ -1,37 +1,35 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.payment; import br.com.fiap.grupo30.fastfood.adapters.out.mercadopago.MercadoPagoAdapter; -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.domain.entities.Order; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.OrderGateway; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoPaymentDTO; 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 br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class CollectOrderPaymentViaMercadoPagoUseCase { - private final JpaOrderRepository orderRepository; + private final OrderGateway orderGateway; private final MercadoPagoAdapter mercadoPagoAdapter; @Autowired public CollectOrderPaymentViaMercadoPagoUseCase( - JpaOrderRepository orderRepository, MercadoPagoAdapter mercadoPagoAdapter) { - this.orderRepository = orderRepository; + OrderGateway orderGateway, MercadoPagoAdapter mercadoPagoAdapter) { + this.orderGateway = orderGateway; this.mercadoPagoAdapter = mercadoPagoAdapter; } - public void execute(MercadoPagoActionEventDTO mercadoPagoPaymentEvent) { + public OrderDTO execute(MercadoPagoActionEventDTO mercadoPagoPaymentEvent) { try { MercadoPagoPaymentDTO payment = this.mercadoPagoAdapter.getPayment(mercadoPagoPaymentEvent.getData().getId()); - OrderEntity order = - this.orderRepository - .findById(Long.parseLong(payment.getExternalReference())) - .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + Order order = + this.orderGateway.findById(Long.parseLong(payment.getExternalReference())); if (MercadoPagoPaymentStatus.APPROVED.getValue().equals(payment.getStatus())) { order.setPaymentCollected(payment.getTransactionAmount()); @@ -39,7 +37,7 @@ public void execute(MercadoPagoActionEventDTO mercadoPagoPaymentEvent) { order.setPaymentRejected(); } - this.orderRepository.save(order); + return orderGateway.save(order).toDTO(); } catch (Exception e) { throw new PaymentProcessingFailedException("Could not process payment collection", e); diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/GeneratePaymentQrCodeUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/GeneratePaymentQrCodeUseCase.java index adf86a4..0a84a15 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/GeneratePaymentQrCodeUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/GeneratePaymentQrCodeUseCase.java @@ -1,37 +1,33 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.payment; import br.com.fiap.grupo30.fastfood.adapters.out.mercadopago.MercadoPagoAdapter; -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.domain.entities.Order; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.OrderGateway; 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 br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.PaymentQrCodeDTOMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class GeneratePaymentQrCodeUseCase { - private final JpaOrderRepository orderRepository; + private final OrderGateway orderGateway; private final MercadoPagoAdapter mercadoPagoAdapter; private final PaymentQrCodeDTOMapper qrCodeMapper; @Autowired public GeneratePaymentQrCodeUseCase( - JpaOrderRepository orderRepository, + OrderGateway orderGateway, MercadoPagoAdapter mercadoPagoAdapter, PaymentQrCodeDTOMapper qrCodeMapper) { - this.orderRepository = orderRepository; + this.orderGateway = orderGateway; this.mercadoPagoAdapter = mercadoPagoAdapter; this.qrCodeMapper = qrCodeMapper; } public PaymentQrCodeDTO execute(Long orderId) { - OrderEntity order = - this.orderRepository - .findById(orderId) - .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + Order order = this.orderGateway.findById(orderId); MercadoPagoQrCodeDTO qrCodeResponse; try { @@ -42,7 +38,7 @@ public PaymentQrCodeDTO execute(Long orderId) { } order.setPaymentProcessing(); - this.orderRepository.save(order); + this.orderGateway.save(order); return qrCodeMapper.map(qrCodeResponse); } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/CreateProductUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/CreateProductUseCase.java index 07a4a38..6cb29b2 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/CreateProductUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/CreateProductUseCase.java @@ -1,17 +1,25 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.product; +import br.com.fiap.grupo30.fastfood.domain.entities.Category; import br.com.fiap.grupo30.fastfood.domain.entities.Product; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.CategoryGateway; 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) { + public CreateProductUseCase(ProductGateway productGateway, CategoryGateway categoryGateway) { this.productGateway = productGateway; + this.categoryGateway = categoryGateway; } - public Product execute(Product product) { - return productGateway.insert(product); + public ProductDTO execute( + String name, String description, Double price, String imgUrl, String category) { + Category categoryEntity = this.categoryGateway.findOne(category); + Product product = Product.create(name, description, price, imgUrl, categoryEntity); + return productGateway.save(product).toDTO(); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/GetProductUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/GetProductUseCase.java index 53adb03..e6293af 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/GetProductUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/GetProductUseCase.java @@ -1,7 +1,7 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.product; -import br.com.fiap.grupo30.fastfood.domain.entities.Product; import br.com.fiap.grupo30.fastfood.infrastructure.gateways.ProductGateway; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.ProductDTO; public class GetProductUseCase { @@ -11,7 +11,7 @@ public GetProductUseCase(ProductGateway productGateway) { this.productGateway = productGateway; } - public Product execute(Long id) { - return productGateway.findById(id); + public ProductDTO execute(Long id) { + return productGateway.findById(id).toDTO(); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/ListProductsByCategoryUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/ListProductsByCategoryUseCase.java index ffd6123..19f01e5 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/ListProductsByCategoryUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/ListProductsByCategoryUseCase.java @@ -2,6 +2,7 @@ import br.com.fiap.grupo30.fastfood.domain.entities.Product; import br.com.fiap.grupo30.fastfood.infrastructure.gateways.ProductGateway; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.ProductDTO; import java.util.List; public class ListProductsByCategoryUseCase { @@ -12,7 +13,9 @@ public ListProductsByCategoryUseCase(ProductGateway productGateway) { this.productGateway = productGateway; } - public List execute(Long categoryId) { - return productGateway.findProductsByCategoryId(categoryId); + public List execute(Long categoryId) { + return productGateway.findProductsByCategoryId(categoryId).stream() + .map(Product::toDTO) + .toList(); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/UpdateProductUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/UpdateProductUseCase.java index 937fe80..29ed21a 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/UpdateProductUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/UpdateProductUseCase.java @@ -1,17 +1,37 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.product; +import br.com.fiap.grupo30.fastfood.domain.entities.Category; import br.com.fiap.grupo30.fastfood.domain.entities.Product; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.CategoryGateway; import br.com.fiap.grupo30.fastfood.infrastructure.gateways.ProductGateway; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.ProductDTO; public class UpdateProductUseCase { private final ProductGateway productGateway; + private final CategoryGateway categoryGateway; - public UpdateProductUseCase(ProductGateway productGateway) { + public UpdateProductUseCase(ProductGateway productGateway, CategoryGateway categoryGateway) { this.productGateway = productGateway; + this.categoryGateway = categoryGateway; } - public Product execute(Long id, Product product) { - return productGateway.update(id, product); + public ProductDTO execute( + Long productId, + String name, + String description, + Double price, + String imgUrl, + String category) { + Category categoryEntity = this.categoryGateway.findOne(category); + + Product product = this.productGateway.findById(productId); + product.setName(name); + product.setDescription(description); + product.setPrice(price); + product.setImgUrl(imgUrl); + product.setCategory(categoryEntity); + + return productGateway.save(product).toDTO(); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/valueobjects/CPF.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/valueobjects/CPF.java index 2a3b4b9..a05c78c 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/valueobjects/CPF.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/valueobjects/CPF.java @@ -15,7 +15,7 @@ public record CPF(String value) { @Override public String value() { - return value; + return removeNonDigits(value); } public static boolean isValidLength(String cpf) { diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/CustomerConfiguration.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/CustomerConfiguration.java index 6fd0ecb..b055182 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/CustomerConfiguration.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/CustomerConfiguration.java @@ -5,7 +5,6 @@ import br.com.fiap.grupo30.fastfood.domain.usecases.customer.RegisterNewCustomerUseCase; import br.com.fiap.grupo30.fastfood.infrastructure.gateways.CustomerGateway; import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaCustomerRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.CustomerEntityMapper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -13,10 +12,8 @@ public class CustomerConfiguration { @Bean - public CustomerRepository customerRepository( - JpaCustomerRepository jpaCustomerRepository, - CustomerEntityMapper customerEntityMapper) { - return new CustomerGateway(jpaCustomerRepository, customerEntityMapper); + public CustomerRepository customerRepository(JpaCustomerRepository jpaCustomerRepository) { + return new CustomerGateway(jpaCustomerRepository); } @Bean 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 index de46087..8ce513a 100644 --- 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 @@ -6,7 +6,6 @@ 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; @@ -14,9 +13,8 @@ public class OrderConfiguration { @Bean - public OrderRepository orderRepository( - JpaOrderRepository jpaOrderRepository, OrderEntityMapper orderEntityMapper) { - return new OrderGateway(jpaOrderRepository, orderEntityMapper); + public OrderRepository orderRepository(JpaOrderRepository jpaOrderRepository) { + return new OrderGateway(jpaOrderRepository); } @Bean diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/ProductConfiguration.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/ProductConfiguration.java index 56c5900..9b1b0db 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/ProductConfiguration.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/ProductConfiguration.java @@ -2,9 +2,9 @@ import br.com.fiap.grupo30.fastfood.domain.repositories.ProductRepository; import br.com.fiap.grupo30.fastfood.domain.usecases.product.*; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.CategoryGateway; import br.com.fiap.grupo30.fastfood.infrastructure.gateways.ProductGateway; import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaProductRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.ProductEntityMapper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -12,9 +12,8 @@ public class ProductConfiguration { @Bean - public ProductRepository productRepository( - JpaProductRepository jpaProductRepository, ProductEntityMapper productEntityMapper) { - return new ProductGateway(jpaProductRepository, productEntityMapper); + public ProductRepository productRepository(JpaProductRepository jpaProductRepository) { + return new ProductGateway(jpaProductRepository); } @Bean @@ -29,13 +28,15 @@ public GetProductUseCase getProductUseCase(ProductGateway productGateway) { } @Bean - public CreateProductUseCase createProductUseCase(ProductGateway productGateway) { - return new CreateProductUseCase(productGateway); + public CreateProductUseCase createProductUseCase( + ProductGateway productGateway, CategoryGateway categoryGateway) { + return new CreateProductUseCase(productGateway, categoryGateway); } @Bean - public UpdateProductUseCase updateProductUseCase(ProductGateway productGateway) { - return new UpdateProductUseCase(productGateway); + public UpdateProductUseCase updateProductUseCase( + ProductGateway productGateway, CategoryGateway categoryGateway) { + return new UpdateProductUseCase(productGateway, categoryGateway); } @Bean diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/CategoryGateway.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/CategoryGateway.java index 22b2f9e..9474b25 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/CategoryGateway.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/CategoryGateway.java @@ -2,7 +2,9 @@ import br.com.fiap.grupo30.fastfood.domain.entities.Category; import br.com.fiap.grupo30.fastfood.domain.repositories.CategoryRepository; +import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.CategoryEntity; import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaCategoryRepository; +import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -22,7 +24,16 @@ public CategoryGateway(JpaCategoryRepository jpaCategoryRepository) { @Transactional(readOnly = true) public List findAll() { return jpaCategoryRepository.findAll().stream() - .map(entity -> new Category(entity.getId(), entity.getName())) + .map(CategoryEntity::toDomainEntity) .toList(); } + + @Override + @Transactional(readOnly = true) + public Category findOne(String category) { + return jpaCategoryRepository + .findCategory(category) + .map(CategoryEntity::toDomainEntity) + .orElseThrow(() -> new ResourceNotFoundException("Category not found")); + } } 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 feebc3f..e1dfdb9 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 @@ -8,7 +8,6 @@ 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; @@ -19,37 +18,31 @@ public class CustomerGateway implements CustomerRepository { public final JpaCustomerRepository jpaCustomerRepository; - public final CustomerEntityMapper customerEntityMapper; @Autowired - public CustomerGateway( - JpaCustomerRepository jpaCustomerRepository, - CustomerEntityMapper customerEntityMapper) { + public CustomerGateway(JpaCustomerRepository jpaCustomerRepository) { this.jpaCustomerRepository = jpaCustomerRepository; - this.customerEntityMapper = customerEntityMapper; } @Override @Transactional(readOnly = true) public Customer findCustomerByCpf(String cpf) { String cpfStr = CPF.removeNonDigits(cpf); - Optional obj; - CustomerEntity entity; + Optional customer = jpaCustomerRepository.findCustomerByCpf(cpfStr); + if (Constants.ANONYMOUS_CPF.equals(cpf)) { - obj = jpaCustomerRepository.findCustomerByCpf(cpfStr); - return obj.map(this.customerEntityMapper::mapFrom).orElse(null); + return customer.map(CustomerEntity::toDomainEntity).orElse(null); } - obj = jpaCustomerRepository.findCustomerByCpf(cpfStr); - entity = obj.orElseThrow(() -> new ResourceNotFoundException("Customer not found")); - return this.customerEntityMapper.mapFrom(entity); + + return customer.map(CustomerEntity::toDomainEntity) + .orElseThrow(() -> new ResourceNotFoundException("Customer not found")); } @Override - public Customer insert(Customer customer) { + public Customer save(Customer customer) { try { - CustomerEntity entity = - jpaCustomerRepository.save(customerEntityMapper.mapTo(customer)); - return this.customerEntityMapper.mapFrom(entity); + CustomerEntity entity = jpaCustomerRepository.save(customer.toPersistence()); + return entity.toDomainEntity(); } catch (DataIntegrityViolationException e) { throw new ResourceConflictException( "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 index 7b4943e..9622137 100644 --- 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 @@ -2,13 +2,10 @@ 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; @@ -19,20 +16,17 @@ public class OrderGateway implements OrderRepository { private final JpaOrderRepository jpaOrderRepository; - private final OrderEntityMapper orderEntityMapper; @Autowired - public OrderGateway( - JpaOrderRepository jpaOrderRepository, OrderEntityMapper orderEntityMapper) { + public OrderGateway(JpaOrderRepository jpaOrderRepository) { 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) + .map(OrderEntity::toDomainEntity) .toList(); } @@ -43,62 +37,23 @@ public Order findById(Long orderId) { jpaOrderRepository .findById(orderId) .orElseThrow(() -> new ResourceNotFoundException("Order not found")); - return orderEntityMapper.mapFrom(entity); + return entity.toDomainEntity(); } @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); + @Transactional() + public Order findByIdForUpdate(Long orderId) { + OrderEntity entity = + jpaOrderRepository + .findById(orderId) + .orElseThrow(() -> new ResourceNotFoundException("Order not found")); + return entity.toDomainEntity(); } @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); - } + public Order save(Order order) { + OrderEntity entity = jpaOrderRepository.save(order.toPersistence()); + return entity.toDomainEntity(); } } 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 78554b9..dfff01d 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 @@ -6,8 +6,6 @@ import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaProductRepository; import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.DatabaseException; import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.ProductEntityMapper; -import jakarta.persistence.EntityNotFoundException; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; @@ -20,13 +18,10 @@ public class ProductGateway implements ProductRepository { private final JpaProductRepository jpaProductRepository; - private final ProductEntityMapper productEntityMapper; @Autowired - public ProductGateway( - JpaProductRepository jpaProductRepository, ProductEntityMapper productEntityMapper) { + public ProductGateway(JpaProductRepository jpaProductRepository) { this.jpaProductRepository = jpaProductRepository; - this.productEntityMapper = productEntityMapper; } @Override @@ -34,7 +29,7 @@ public ProductGateway( public List findProductsByCategoryId(Long categoryId) { Long category = categoryId == 0 ? null : categoryId; return jpaProductRepository.findProductsByCategoryId(category).stream() - .map(this.productEntityMapper::mapFrom) + .map(ProductEntity::toDomainEntity) .toList(); } @@ -44,27 +39,14 @@ public Product findById(Long id) { Optional obj = jpaProductRepository.findById(id); ProductEntity entity = obj.orElseThrow(() -> new ResourceNotFoundException("Product not found")); - return this.productEntityMapper.mapFrom(entity); + return entity.toDomainEntity(); } @Override @Transactional - public Product insert(Product product) { - ProductEntity entity = jpaProductRepository.save(productEntityMapper.mapTo(product)); - return this.productEntityMapper.mapFrom(entity); - } - - @Override - @Transactional - public Product update(Long id, Product product) { - try { - ProductEntity entity = jpaProductRepository.getReferenceById(id); - productEntityMapper.updateEntityFromProduct(entity, product); - entity = jpaProductRepository.save(entity); - return this.productEntityMapper.mapFrom(entity); - } catch (EntityNotFoundException e) { - throw new ResourceNotFoundException("Id not found " + id, e); - } + public Product save(Product product) { + ProductEntity entity = jpaProductRepository.save(product.toPersistence()); + return entity.toDomainEntity(); } @Override diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/CategoryEntity.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/CategoryEntity.java index b32f385..035e4c0 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/CategoryEntity.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/CategoryEntity.java @@ -1,16 +1,15 @@ package br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CategoryDTO; +import br.com.fiap.grupo30.fastfood.domain.entities.Category; import jakarta.persistence.*; import java.time.Instant; -import lombok.AllArgsConstructor; +import lombok.AccessLevel; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; @Getter -@NoArgsConstructor -@AllArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) @EqualsAndHashCode @Entity @Table(name = "tb_category") @@ -32,11 +31,8 @@ public class CategoryEntity { @Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE") private Instant deletedAt; - public void setId(Long id) { - this.id = id; - } - - public void setName(String name) { + public CategoryEntity(Long categoryId, String name) { + this.id = categoryId; this.name = name; } @@ -55,8 +51,7 @@ public void preRemove() { deletedAt = Instant.now(); } - public CategoryDTO toDTO() { - CategoryDTO categoryDto = new CategoryDTO(this.id, this.name); - return categoryDto; + public Category toDomainEntity() { + return new Category(id, name); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/CustomerEntity.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/CustomerEntity.java index 3033d3c..fdf581a 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/CustomerEntity.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/CustomerEntity.java @@ -1,18 +1,18 @@ package br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CustomerDTO; +import br.com.fiap.grupo30.fastfood.domain.entities.Customer; +import br.com.fiap.grupo30.fastfood.domain.valueobjects.CPF; import jakarta.persistence.*; import java.time.Instant; import java.util.ArrayList; import java.util.List; -import lombok.AllArgsConstructor; +import lombok.AccessLevel; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; @Getter -@NoArgsConstructor -@AllArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) @EqualsAndHashCode @Entity @Table(name = "tb_customer") @@ -43,19 +43,10 @@ public class CustomerEntity { @Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE") private Instant deletedAt; - public void setId(Long id) { - this.id = id; - } - - public void setName(String name) { + public CustomerEntity(Long customerId, String name, String cpf, String email) { + this.id = customerId; this.name = name; - } - - public void setCpf(String cpf) { this.cpf = cpf; - } - - public void setEmail(String email) { this.email = email; } @@ -74,8 +65,7 @@ public void preRemove() { deletedAt = Instant.now(); } - public CustomerDTO toDTO() { - CustomerDTO customerDto = new CustomerDTO(this.id, this.name, this.cpf, this.email); - return customerDto; + public Customer toDomainEntity() { + return new Customer(id, name, new CPF(cpf), email); } } 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 4ae5df8..f6c6a02 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,19 +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.CompositeDomainValidationException; +import br.com.fiap.grupo30.fastfood.domain.entities.Order; import jakarta.persistence.*; import java.time.Instant; +import java.util.ArrayList; import java.util.Collection; -import java.util.LinkedList; +import java.util.stream.Collectors; +import lombok.AccessLevel; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; @Getter -@NoArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) @EqualsAndHashCode @Entity @Table(name = "tb_order") @@ -42,9 +42,7 @@ public class OrderEntity { fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) - private Collection items = new LinkedList(); - - @Transient private Double totalPrice = 0.0; + private Collection items; @Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE") private Instant createdAt; @@ -55,75 +53,32 @@ public class OrderEntity { @Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE") private Instant deletedAt; - public void setId(Long id) { - this.id = id; - } - - public void setStatus(OrderStatus status) { + public OrderEntity( + Long orderId, + OrderStatus status, + CustomerEntity customer, + PaymentEntity payment, + Collection orderItems) { + this.id = orderId; 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() - .ifPresentOrElse( - existingItem -> - existingItem.setQuantity(existingItem.getQuantity() + quantity), - () -> this.items.add(OrderItemEntity.create(this, product, quantity))); - - this.recalculateTotalPrice(); - } - - public void removeProduct(ProductEntity product) { - this.items.removeIf(orderItem -> orderItem.getProduct().equals(product)); - this.recalculateTotalPrice(); - } - - public void recalculateTotalPrice() { - this.totalPrice = this.items.stream().mapToDouble(OrderItemEntity::getTotalPrice).sum(); - } - - private Boolean hasProducts() { - return !this.items.isEmpty(); - } + this.payment = payment; + this.items = orderItems; - public void validate() { - var errors = new LinkedList(); - - if (this.status == OrderStatus.SUBMITTED && !this.hasProducts()) { - errors.add("Can not submit order without products"); - } - - if (this.status == OrderStatus.PREPARING - && !PaymentStatus.COLLECTED.equals(this.getPayment().getStatus())) { - errors.add("Can not start peparing order without collecting payment"); - } - - if (!errors.isEmpty()) { - throw new CompositeDomainValidationException(errors); + this.payment.setParentRelation(this); + for (OrderItemEntity orderItem : this.items) { + orderItem.setParentRelation(this); } } - @PostLoad - protected void postLoad() { - recalculateTotalPrice(); - } - @PrePersist protected void prePersist() { createdAt = Instant.now(); - this.validate(); } @PreUpdate protected void preUpdate() { updatedAt = Instant.now(); - this.validate(); } @PreRemove @@ -131,37 +86,14 @@ protected void preRemove() { deletedAt = Instant.now(); } - public static OrderEntity create() { - OrderEntity order = new OrderEntity(); - order.status = OrderStatus.DRAFT; - order.payment = PaymentEntity.create(order); - return order; - } - - public void setPaymentProcessing() { - this.payment.setStatus(PaymentStatus.PROCESSING); - this.payment.setAmount(this.getTotalPrice()); - } - - public void setPaymentCollected(Double paymentCollectedAmount) { - this.payment.setStatus(PaymentStatus.COLLECTED); - this.payment.setAmount(paymentCollectedAmount); - } - - public void setPaymentRejected() { - this.payment.setStatus(PaymentStatus.REJECTED); - this.payment.setAmount(this.getTotalPrice()); - } - - public OrderDTO toDTO() { - OrderDTO orderDto = - new OrderDTO( - this.id, - this.status, - this.items.stream().map(item -> item.toDTO()).toArray(OrderItemDTO[]::new), - this.getTotalPrice(), - this.customer.toDTO(), - this.payment.toDTO()); - return orderDto; + public Order toDomainEntity() { + return new Order( + id, + status, + customer.toDomainEntity(), + payment.toDomainEntity(), + items.stream() + .map(OrderItemEntity::toDomainEntity) + .collect(Collectors.toCollection(ArrayList::new))); } } 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 e4395dd..4af3a9c 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 @@ -1,14 +1,15 @@ package br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderItemDTO; +import br.com.fiap.grupo30.fastfood.domain.entities.OrderItem; import jakarta.persistence.*; import jakarta.validation.constraints.Min; +import lombok.AccessLevel; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; @Getter -@NoArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) @EqualsAndHashCode @Entity @Table(name = "tb_order_item") @@ -28,40 +29,23 @@ public class OrderItemEntity { @Column(nullable = false) @Min(1L) - private Long quantity = 1L; + private Long quantity; @Column(nullable = false) @Min(0) - private Double totalPrice = 0.0; + private Double totalPrice; - public void setId(Long id) { - this.id = id; - } - - public void setQuantity(Long quantity) { - this.quantity = quantity; - this.recalculateTotalPrice(); - } - - public void setProduct(ProductEntity product) { + public OrderItemEntity(ProductEntity product, Long quantity, Double totalPrice) { this.product = product; + this.quantity = quantity; + this.totalPrice = totalPrice; } - private void recalculateTotalPrice() { - this.totalPrice = this.product.getPrice() * this.quantity; - } - - public static OrderItemEntity create(OrderEntity order, ProductEntity product, Long quantity) { - OrderItemEntity orderItem = new OrderItemEntity(); - orderItem.order = order; - orderItem.product = product; - orderItem.setQuantity(quantity); - return orderItem; + public void setParentRelation(OrderEntity order) { + this.order = order; } - public OrderItemDTO toDTO() { - OrderItemDTO orderItemDto = - new OrderItemDTO(this.quantity, this.totalPrice, this.product.toDTO()); - return orderItemDto; + public OrderItem toDomainEntity() { + return new OrderItem(product.toDomainEntity(), quantity); } } 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 88a6732..a4a844c 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 @@ -1,6 +1,7 @@ package br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.PaymentDTO; +import br.com.fiap.grupo30.fastfood.domain.PaymentStatus; +import br.com.fiap.grupo30.fastfood.domain.entities.Payment; import jakarta.persistence.*; import jakarta.validation.constraints.Min; import java.time.Instant; @@ -39,6 +40,16 @@ public class PaymentEntity { @Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE") private Instant deletedAt; + public PaymentEntity(Long paymentId, PaymentStatus status, Double amount) { + this.id = paymentId; + this.status = status; + this.amount = amount; + } + + public void setParentRelation(OrderEntity order) { + this.order = order; + } + @PrePersist protected void prePersist() { createdAt = Instant.now(); @@ -54,32 +65,7 @@ protected void preRemove() { deletedAt = Instant.now(); } - public PaymentStatus getStatus() { - return this.status; - } - - public void setStatus(PaymentStatus status) { - this.status = status; - } - - public Double getAmount() { - return this.amount; - } - - public void setAmount(Double amount) { - this.amount = amount; - } - - public static PaymentEntity create(OrderEntity order) { - PaymentEntity payment = new PaymentEntity(); - payment.order = order; - payment.status = PaymentStatus.NOT_SUBMITTED; - payment.amount = 0.0; - return payment; - } - - public PaymentDTO toDTO() { - PaymentDTO dto = new PaymentDTO(this.id, this.status, this.amount); - return dto; + public Payment toDomainEntity() { + return new Payment(id, status, amount); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/ProductEntity.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/ProductEntity.java index 52cdaac..a4d6ea2 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/ProductEntity.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/entities/ProductEntity.java @@ -1,16 +1,15 @@ package br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.ProductDTO; +import br.com.fiap.grupo30.fastfood.domain.entities.Product; import jakarta.persistence.*; import java.time.Instant; -import lombok.AllArgsConstructor; +import lombok.AccessLevel; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; @Getter -@NoArgsConstructor -@AllArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) @EqualsAndHashCode @Entity @Table(name = "tb_product") @@ -45,27 +44,18 @@ public class ProductEntity { @Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE") private Instant deletedAt; - public void setId(Long id) { - this.id = id; - } - - public void setName(String name) { + public ProductEntity( + Long productId, + String name, + String description, + Double price, + String imgUrl, + CategoryEntity category) { + this.id = productId; this.name = name; - } - - public void setDescription(String description) { this.description = description; - } - - public void setPrice(Double price) { this.price = price; - } - - public void setImgUrl(String imgUrl) { this.imgUrl = imgUrl; - } - - public void setCategory(CategoryEntity category) { this.category = category; } @@ -84,15 +74,7 @@ public void preRemove() { deletedAt = Instant.now(); } - public ProductDTO toDTO() { - ProductDTO productDto = - new ProductDTO( - this.id, - this.name, - this.description, - this.price, - this.imgUrl, - this.category.toDTO()); - return productDto; + public Product toDomainEntity() { + return new Product(id, name, description, price, imgUrl, category.toDomainEntity()); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/repositories/JpaCategoryRepository.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/repositories/JpaCategoryRepository.java index d1158d6..0adf816 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/repositories/JpaCategoryRepository.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/persistence/repositories/JpaCategoryRepository.java @@ -1,8 +1,13 @@ package br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories; import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.CategoryEntity; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; @Repository -public interface JpaCategoryRepository extends JpaRepository {} +public interface JpaCategoryRepository extends JpaRepository { + @Query("SELECT obj FROM CategoryEntity obj " + "WHERE obj.name = :category") + Optional findCategory(String category); +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/CategoryController.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/CategoryController.java index dd0681a..30e0150 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/CategoryController.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/CategoryController.java @@ -2,7 +2,6 @@ import br.com.fiap.grupo30.fastfood.domain.usecases.category.ListAllCategoriesInMenuUseCase; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CategoryDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.CategoryDTOMapper; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import java.util.List; @@ -18,14 +17,10 @@ public class CategoryController { private final ListAllCategoriesInMenuUseCase listAllCategoriesInMenuUseCase; - private final CategoryDTOMapper categoryDTOMapper; @Autowired - public CategoryController( - ListAllCategoriesInMenuUseCase listAllCategoriesInMenuUseCase, - CategoryDTOMapper categoryDTOMapper) { + public CategoryController(ListAllCategoriesInMenuUseCase listAllCategoriesInMenuUseCase) { this.listAllCategoriesInMenuUseCase = listAllCategoriesInMenuUseCase; - this.categoryDTOMapper = categoryDTOMapper; } @GetMapping @@ -33,10 +28,7 @@ public CategoryController( summary = "Get all categories", description = "Retrieve a list of all registered categories") public ResponseEntity> findAll() { - List list = - this.listAllCategoriesInMenuUseCase.execute().stream() - .map(this.categoryDTOMapper::mapTo) - .toList(); - return ResponseEntity.ok().body(list); + List categories = this.listAllCategoriesInMenuUseCase.execute(); + return ResponseEntity.ok().body(categories); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/CustomerController.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/CustomerController.java index 7e1523c..2243036 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/CustomerController.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/CustomerController.java @@ -1,12 +1,8 @@ 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.customer.RegisterNewCustomerUseCase; -import br.com.fiap.grupo30.fastfood.domain.valueobjects.CPF; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CustomerDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.InvalidCpfException; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.CustomerDTOMapper; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; @@ -21,28 +17,23 @@ @Tag(name = "Customers Controller", description = "RESTful API for managing customers.") public class CustomerController { - private static final String PATH_VARIABLE_ID = "/{id}"; - private final FindCustomerByCpfUseCase findCustomerByCpfUseCase; private final RegisterNewCustomerUseCase registerNewCustomerUseCase; - private final CustomerDTOMapper customerDTOMapper; @Autowired public CustomerController( FindCustomerByCpfUseCase findCustomerByCpfUseCase, - RegisterNewCustomerUseCase registerNewCustomerUseCase, - CustomerDTOMapper customerDTOMapper) { + RegisterNewCustomerUseCase registerNewCustomerUseCase) { this.findCustomerByCpfUseCase = findCustomerByCpfUseCase; this.registerNewCustomerUseCase = registerNewCustomerUseCase; - this.customerDTOMapper = customerDTOMapper; } @GetMapping @Operation(summary = "Get a customer", description = "Retrieve a registered customer by cpf") public ResponseEntity findCustomerByCpf( @RequestParam(value = "cpf", defaultValue = "0") String cpf) { - CustomerDTO dto = this.customerDTOMapper.mapTo(this.findCustomerByCpfUseCase.execute(cpf)); - return ResponseEntity.ok().body(dto); + CustomerDTO customer = this.findCustomerByCpfUseCase.execute(cpf); + return ResponseEntity.ok().body(customer); } @PostMapping @@ -50,19 +41,14 @@ public ResponseEntity findCustomerByCpf( summary = "Create a new customer", description = "Create a new customer and return the created customer's data") public ResponseEntity createCustomer(@RequestBody @Valid CustomerDTO dto) { - String cpf = dto.getCpf(); - if (!CPF.isValid(cpf)) { - throw new InvalidCpfException(cpf); - } - dto.setCpf(CPF.removeNonDigits(cpf)); - Customer domainObj = this.customerDTOMapper.mapFrom(dto); - CustomerDTO dtoCreated = - this.customerDTOMapper.mapTo(this.registerNewCustomerUseCase.execute(domainObj)); + CustomerDTO createdCustomer = + this.registerNewCustomerUseCase.execute( + dto.getName(), dto.getCpf(), dto.getEmail()); URI uri = ServletUriComponentsBuilder.fromCurrentRequest() - .path(PATH_VARIABLE_ID) - .buildAndExpand(dto.getId()) + .path("/{cpf}") + .buildAndExpand(dto.getCpf()) .toUri(); - return ResponseEntity.created(uri).body(dtoCreated); + return ResponseEntity.created(uri).body(createdCustomer); } } 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 dd3c219..0009b91 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,9 @@ 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.OrderDTO; -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; @@ -30,8 +27,6 @@ public class OrderController { private final StartPreparingOrderUseCase startPreparingOrderUseCase; private final FinishPreparingOrderUseCase finishPreparingOrderUseCase; private final DeliverOrderUseCase deliverOrderUseCase; - private final FindCustomerByCpfUseCase findCustomerByCpfUseCase; - private final OrderDTOMapper orderDTOMapper; @Autowired public OrderController( @@ -43,9 +38,7 @@ public OrderController( ListOrdersUseCase listAllOrdersUseCase, StartPreparingOrderUseCase startPreparingOrderUseCase, FinishPreparingOrderUseCase finishPreparingOrderUseCase, - DeliverOrderUseCase deliverOrderUseCase, - FindCustomerByCpfUseCase findCustomerByCpfUseCase, - OrderDTOMapper orderDTOMapper) { + DeliverOrderUseCase deliverOrderUseCase) { this.startNewOrderUseCase = startNewOrderUseCase; this.addProductToOrderUseCase = addProductToOrderUseCase; this.removeProductFromOrderUseCase = removeProductFromOrderUseCase; @@ -55,8 +48,6 @@ public OrderController( this.startPreparingOrderUseCase = startPreparingOrderUseCase; this.finishPreparingOrderUseCase = finishPreparingOrderUseCase; this.deliverOrderUseCase = deliverOrderUseCase; - this.findCustomerByCpfUseCase = findCustomerByCpfUseCase; - this.orderDTOMapper = orderDTOMapper; } @GetMapping @@ -67,9 +58,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).stream().map(orderDTOMapper::mapTo).toList(); - return ResponseEntity.ok().body(list); + List orders = listAllOrdersUseCase.execute(status); + return ResponseEntity.ok().body(orders); } @GetMapping(value = "/{orderId}") @@ -77,7 +67,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 = orderDTOMapper.mapTo(getOrderUseCase.execute(orderId)); + OrderDTO order = getOrderUseCase.execute(orderId); return ResponseEntity.ok().body(order); } @@ -87,11 +77,7 @@ 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) { - Customer customer = null; - if (request != null && request.getCpf() != null && !request.getCpf().isEmpty()) { - customer = findCustomerByCpfUseCase.execute(request.getCpf()); - } - OrderDTO order = orderDTOMapper.mapTo(startNewOrderUseCase.execute(customer)); + OrderDTO order = startNewOrderUseCase.execute(request.getCpf()); URI uri = ServletUriComponentsBuilder.fromCurrentRequest() .path("/{orderId}") @@ -105,9 +91,8 @@ public ResponseEntity startNewOrder( public ResponseEntity addProduct( @PathVariable Long orderId, @RequestBody AddOrderProductRequest request) { OrderDTO order = - orderDTOMapper.mapTo( - addProductToOrderUseCase.execute( - orderId, request.getProductId(), request.getQuantity())); + addProductToOrderUseCase.execute( + orderId, request.getProductId(), request.getQuantity()); return ResponseEntity.ok().body(order); } @@ -117,8 +102,7 @@ public ResponseEntity addProduct( description = "Removes a product from an order") public ResponseEntity removeProduct( @PathVariable Long orderId, @PathVariable Long productId) { - OrderDTO order = - orderDTOMapper.mapTo(removeProductFromOrderUseCase.execute(orderId, productId)); + OrderDTO order = removeProductFromOrderUseCase.execute(orderId, productId); return ResponseEntity.ok().body(order); } @@ -127,7 +111,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 = orderDTOMapper.mapTo(submitOrderUseCase.execute(orderId)); + OrderDTO order = submitOrderUseCase.execute(orderId); return ResponseEntity.ok().body(order); } @@ -136,7 +120,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 = orderDTOMapper.mapTo(startPreparingOrderUseCase.execute(orderId)); + OrderDTO order = startPreparingOrderUseCase.execute(orderId); return ResponseEntity.ok().body(order); } @@ -145,7 +129,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 = orderDTOMapper.mapTo(finishPreparingOrderUseCase.execute(orderId)); + OrderDTO order = finishPreparingOrderUseCase.execute(orderId); return ResponseEntity.ok().body(order); } @@ -154,7 +138,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 = orderDTOMapper.mapTo(deliverOrderUseCase.execute(orderId)); + OrderDTO order = deliverOrderUseCase.execute(orderId); return ResponseEntity.ok().body(order); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/PaymentResource.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/PaymentResource.java index d5e7e8c..456ac26 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/PaymentResource.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/PaymentResource.java @@ -43,7 +43,7 @@ public ResponseEntity generateQrCodeForPaymentCollection( @Operation(summary = "Collect payment by cash") public ResponseEntity collectPaymentByBash( @PathVariable Long orderId, @RequestBody CollectPaymentViaCashRequest request) { - var order = this.collectOrderPaymentViaCashUseCase.execute(orderId, request); + OrderDTO order = this.collectOrderPaymentViaCashUseCase.execute(orderId, request); return ResponseEntity.ok().body(order); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/ProductController.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/ProductController.java index ee3c54f..afb7784 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/ProductController.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/ProductController.java @@ -1,9 +1,7 @@ package br.com.fiap.grupo30.fastfood.presentation.controllers; -import br.com.fiap.grupo30.fastfood.domain.entities.Product; import br.com.fiap.grupo30.fastfood.domain.usecases.product.*; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.ProductDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.ProductDTOMapper; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; @@ -27,23 +25,19 @@ public class ProductController { private final UpdateProductUseCase updateProductUseCase; private final DeleteProductUseCase deleteProductUseCase; - private final ProductDTOMapper productDTOMapper; - @Autowired public ProductController( ListProductsByCategoryUseCase listProductsByCategoryUseCase, GetProductUseCase getProductUseCase, CreateProductUseCase createProductUseCase, UpdateProductUseCase updateProductUseCase, - DeleteProductUseCase deleteProductUseCase, - ProductDTOMapper productDTOMapper) { + DeleteProductUseCase deleteProductUseCase) { this.listProductsByCategoryUseCase = listProductsByCategoryUseCase; this.getProductUseCase = getProductUseCase; this.createProductUseCase = createProductUseCase; this.updateProductUseCase = updateProductUseCase; this.deleteProductUseCase = deleteProductUseCase; - this.productDTOMapper = productDTOMapper; } @GetMapping @@ -54,11 +48,8 @@ public ProductController( + "via RequestParam. i.e., ?categoryId=1") public ResponseEntity> findProductsByCategoryId( @RequestParam(value = "categoryId", defaultValue = "0") Long categoryId) { - List list = - this.listProductsByCategoryUseCase.execute(categoryId).stream() - .map(this.productDTOMapper::mapTo) - .toList(); - return ResponseEntity.ok().body(list); + List products = this.listProductsByCategoryUseCase.execute(categoryId); + return ResponseEntity.ok().body(products); } @GetMapping(value = PATH_VARIABLE_ID) @@ -66,7 +57,7 @@ public ResponseEntity> findProductsByCategoryId( summary = "Get a product by ID", description = "Retrieve a specific product based on its ID") public ResponseEntity findProductById(@PathVariable Long id) { - ProductDTO dto = this.productDTOMapper.mapTo(this.getProductUseCase.execute(id)); + ProductDTO dto = this.getProductUseCase.execute(id); return ResponseEntity.ok().body(dto); } @@ -75,13 +66,17 @@ public ResponseEntity findProductById(@PathVariable Long id) { summary = "Create a new product", description = "Create a new product and return the created product's data") public ResponseEntity createProduct(@RequestBody @Valid ProductDTO dto) { - Product domainObj = this.productDTOMapper.mapFrom(dto); ProductDTO dtoCreated = - this.productDTOMapper.mapTo(this.createProductUseCase.execute(domainObj)); + this.createProductUseCase.execute( + dto.getName(), + dto.getDescription(), + dto.getPrice(), + dto.getImgUrl(), + dto.getCategory()); URI uri = ServletUriComponentsBuilder.fromCurrentRequest() .path(PATH_VARIABLE_ID) - .buildAndExpand(dto.getId()) + .buildAndExpand(dto.getProductId()) .toUri(); return ResponseEntity.created(uri).body(dtoCreated); } @@ -92,9 +87,14 @@ public ResponseEntity createProduct(@RequestBody @Valid ProductDTO d description = "Update the data of an existing product based on its ID") public ResponseEntity updateProduct( @PathVariable Long id, @RequestBody @Valid ProductDTO dto) { - Product domainObj = this.productDTOMapper.mapFrom(dto); ProductDTO dtoUpdated = - this.productDTOMapper.mapTo(this.updateProductUseCase.execute(id, domainObj)); + this.updateProductUseCase.execute( + id, + dto.getName(), + dto.getDescription(), + dto.getPrice(), + dto.getImgUrl(), + dto.getCategory()); return ResponseEntity.ok().body(dtoUpdated); } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/CategoryDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/CategoryDTO.java index 91ff1df..25b35ea 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/CategoryDTO.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/CategoryDTO.java @@ -1,23 +1,12 @@ package br.com.fiap.grupo30.fastfood.presentation.presenters.dto; -import br.com.fiap.grupo30.fastfood.domain.entities.Category; import jakarta.validation.constraints.NotBlank; import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.Getter; -@Data -@NoArgsConstructor +@Getter @AllArgsConstructor public class CategoryDTO { - - private Long id; - @NotBlank(message = "Campo requirido") private String name; - - public CategoryDTO(Category entity) { - this.id = entity.getId(); - this.name = entity.getName(); - } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/CustomerDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/CustomerDTO.java index 75c20bc..b9d4220 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/CustomerDTO.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/CustomerDTO.java @@ -1,19 +1,13 @@ package br.com.fiap.grupo30.fastfood.presentation.presenters.dto; -import br.com.fiap.grupo30.fastfood.domain.entities.Customer; import jakarta.validation.constraints.Email; import jakarta.validation.constraints.NotBlank; import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.Getter; -@Data -@NoArgsConstructor +@Getter @AllArgsConstructor public class CustomerDTO { - - private Long id; - @NotBlank(message = "Campo obrigatório") private String name; @@ -22,11 +16,4 @@ public class CustomerDTO { @Email(message = "Favor entrar um email válido") private String email; - - public CustomerDTO(Customer entity) { - this.id = entity.getId(); - this.name = entity.getName(); - this.cpf = String.valueOf(entity.getCpf().value()); - this.email = entity.getEmail(); - } } 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 29e1bcf..fe0aa7d 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,13 +1,17 @@ package br.com.fiap.grupo30.fastfood.presentation.presenters.dto; import br.com.fiap.grupo30.fastfood.domain.OrderStatus; +import lombok.AccessLevel; import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.Getter; +import lombok.Setter; -@Data +@Getter @AllArgsConstructor public class OrderDTO { + @Setter(AccessLevel.NONE) private Long orderId; + private OrderStatus status; private OrderItemDTO[] items; private Double totalPrice; diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/OrderItemDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/OrderItemDTO.java index 3de3a1c..573cbe3 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/OrderItemDTO.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/OrderItemDTO.java @@ -1,12 +1,12 @@ package br.com.fiap.grupo30.fastfood.presentation.presenters.dto; import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.Getter; -@Data +@Getter @AllArgsConstructor public class OrderItemDTO { + private ProductDTO product; private Long quantity; private Double totalPrice; - private ProductDTO product; } 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 535852f..e011773 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 @@ -1,15 +1,12 @@ package br.com.fiap.grupo30.fastfood.presentation.presenters.dto; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.PaymentStatus; +import br.com.fiap.grupo30.fastfood.domain.PaymentStatus; import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.Getter; -@Data +@Getter @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/dto/ProductDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/ProductDTO.java index 93034f2..c0fa623 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/ProductDTO.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/ProductDTO.java @@ -1,18 +1,15 @@ package br.com.fiap.grupo30.fastfood.presentation.presenters.dto; -import br.com.fiap.grupo30.fastfood.domain.entities.Product; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Positive; import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.Getter; -@Data -@NoArgsConstructor +@Getter @AllArgsConstructor public class ProductDTO { - private Long id; + private Long productId; @NotBlank(message = "Campo requirido") private String name; @@ -25,14 +22,5 @@ public class ProductDTO { @NotBlank(message = "Campo requirido") private String imgUrl; - private CategoryDTO category; - - public ProductDTO(Product entity) { - id = entity.getId(); - name = entity.getName(); - description = entity.getDescription(); - price = entity.getPrice(); - imgUrl = entity.getImgUrl(); - category = new CategoryDTO(entity.getCategory()); - } + private String category; } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryDTOMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryDTOMapper.java deleted file mode 100644 index 10cc5b0..0000000 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryDTOMapper.java +++ /dev/null @@ -1,26 +0,0 @@ -package br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl; - -import br.com.fiap.grupo30.fastfood.domain.entities.Category; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CategoryDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.BiDirectionalMapper; -import org.springframework.stereotype.Component; - -@Component -public class CategoryDTOMapper implements BiDirectionalMapper { - - @Override - public CategoryDTO mapTo(Category category) { - CategoryDTO dto = new CategoryDTO(); - dto.setId(category.getId()); - dto.setName(category.getName()); - return dto; - } - - @Override - public Category mapFrom(CategoryDTO dto) { - Category category = new Category(); - category.setId(dto.getId()); - category.setName(dto.getName()); - return category; - } -} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryEntityMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryEntityMapper.java deleted file mode 100644 index bc643d1..0000000 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryEntityMapper.java +++ /dev/null @@ -1,26 +0,0 @@ -package br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl; - -import br.com.fiap.grupo30.fastfood.domain.entities.Category; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.CategoryEntity; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.BiDirectionalMapper; -import org.springframework.stereotype.Component; - -@Component -public class CategoryEntityMapper implements BiDirectionalMapper { - - @Override - public CategoryEntity mapTo(Category category) { - CategoryEntity entity = new CategoryEntity(); - entity.setId(category.getId()); - entity.setName(category.getName()); - return entity; - } - - @Override - public Category mapFrom(CategoryEntity entity) { - Category category = new Category(); - category.setId(entity.getId()); - category.setName(entity.getName()); - return category; - } -} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CustomerDTOMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CustomerDTOMapper.java deleted file mode 100644 index 58f78a9..0000000 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CustomerDTOMapper.java +++ /dev/null @@ -1,31 +0,0 @@ -package br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl; - -import br.com.fiap.grupo30.fastfood.domain.entities.Customer; -import br.com.fiap.grupo30.fastfood.domain.valueobjects.CPF; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CustomerDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.BiDirectionalMapper; -import org.springframework.stereotype.Component; - -@Component -public final class CustomerDTOMapper implements BiDirectionalMapper { - - @Override - public CustomerDTO mapTo(Customer customer) { - CustomerDTO dto = new CustomerDTO(); - dto.setId(customer.getId()); - dto.setName(customer.getName()); - dto.setCpf(customer.getCpf().value()); - dto.setEmail(customer.getEmail()); - return dto; - } - - @Override - public Customer mapFrom(CustomerDTO dto) { - Customer customer = new Customer(); - customer.setId(dto.getId()); - customer.setName(dto.getName()); - customer.setCpf(new CPF(dto.getCpf())); - customer.setEmail(dto.getEmail()); - return customer; - } -} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CustomerEntityMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CustomerEntityMapper.java deleted file mode 100644 index fa3734c..0000000 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CustomerEntityMapper.java +++ /dev/null @@ -1,37 +0,0 @@ -package br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl; - -import br.com.fiap.grupo30.fastfood.domain.entities.Customer; -import br.com.fiap.grupo30.fastfood.domain.valueobjects.CPF; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.CustomerEntity; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.BiDirectionalMapper; -import org.springframework.stereotype.Component; - -@Component -public final class CustomerEntityMapper implements BiDirectionalMapper { - - @Override - public CustomerEntity mapTo(Customer customer) { - CustomerEntity entity = new CustomerEntity(); - entity.setId(customer.getId()); - entity.setName(customer.getName()); - entity.setCpf(String.valueOf(customer.getCpf().value())); - entity.setEmail(customer.getEmail()); - return entity; - } - - @Override - public Customer mapFrom(CustomerEntity entity) { - Customer customer = new Customer(); - customer.setId(entity.getId()); - customer.setName(entity.getName()); - customer.setCpf(new CPF(entity.getCpf())); - customer.setEmail(entity.getEmail()); - return customer; - } - - public void updateEntityFromCustomer(CustomerEntity entity, Customer customer) { - entity.setName(customer.getName()); - entity.setCpf(customer.getCpf().value()); - entity.setEmail(customer.getEmail()); - } -} 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 deleted file mode 100644 index 91d3ab9..0000000 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderDTOMapper.java +++ /dev/null @@ -1,59 +0,0 @@ -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.BiDirectionalMapper; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -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, - PaymentDTOMapper paymentDTOMapper) { - this.customerDTOMapper = customerDTOMapper; - this.productDTOMapper = productDTOMapper; - this.paymentDTOMapper = paymentDTOMapper; - } - - @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()), - paymentDTOMapper.mapTo(order.getPayment())); - } - - @Override - public Order mapFrom(OrderDTO dto) { - Order order = new Order(); - 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()), - 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 deleted file mode 100644 index 2d3e47b..0000000 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/OrderEntityMapper.java +++ /dev/null @@ -1,56 +0,0 @@ -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.BiDirectionalMapper; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public final class OrderEntityMapper implements BiDirectionalMapper { - 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())); - } -} 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 deleted file mode 100644 index be8a52d..0000000 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/PaymentDTOMapper.java +++ /dev/null @@ -1,27 +0,0 @@ -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; - } -} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/ProductDTOMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/ProductDTOMapper.java deleted file mode 100644 index 5ea93ed..0000000 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/ProductDTOMapper.java +++ /dev/null @@ -1,41 +0,0 @@ -package br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl; - -import br.com.fiap.grupo30.fastfood.domain.entities.Product; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.ProductDTO; -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 ProductDTOMapper implements BiDirectionalMapper { - private final CategoryDTOMapper categoryDTOMapper; - - @Autowired - public ProductDTOMapper(CategoryDTOMapper categoryDTOMapper) { - this.categoryDTOMapper = categoryDTOMapper; - } - - @Override - public ProductDTO mapTo(Product product) { - ProductDTO dto = new ProductDTO(); - dto.setId(product.getId()); - dto.setName(product.getName()); - dto.setDescription(product.getDescription()); - dto.setPrice(product.getPrice()); - dto.setImgUrl(product.getImgUrl()); - dto.setCategory(categoryDTOMapper.mapTo(product.getCategory())); - return dto; - } - - @Override - public Product mapFrom(ProductDTO dto) { - Product product = new Product(); - product.setId(dto.getId()); - product.setName(dto.getName()); - product.setDescription(dto.getDescription()); - product.setImgUrl(dto.getImgUrl()); - product.setPrice(dto.getPrice()); - product.setCategory(categoryDTOMapper.mapFrom(dto.getCategory())); - return product; - } -} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/ProductEntityMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/ProductEntityMapper.java deleted file mode 100644 index b497044..0000000 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/ProductEntityMapper.java +++ /dev/null @@ -1,58 +0,0 @@ -package br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl; - -import br.com.fiap.grupo30.fastfood.domain.entities.Product; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.CategoryEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.ProductEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaCategoryRepository; -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 ProductEntityMapper implements BiDirectionalMapper { - - private final CategoryEntityMapper categoryEntityMapper; - private final JpaCategoryRepository jpaCategoryRepository; - - @Autowired - public ProductEntityMapper( - CategoryEntityMapper categoryEntityMapper, - JpaCategoryRepository jpaCategoryRepository) { - this.categoryEntityMapper = categoryEntityMapper; - this.jpaCategoryRepository = jpaCategoryRepository; - } - - @Override - public ProductEntity mapTo(Product product) { - ProductEntity entity = new ProductEntity(); - entity.setId(product.getId()); - entity.setName(product.getName()); - entity.setDescription(product.getDescription()); - entity.setPrice(product.getPrice()); - entity.setImgUrl(product.getImgUrl()); - entity.setCategory(categoryEntityMapper.mapTo(product.getCategory())); - return entity; - } - - @Override - public Product mapFrom(ProductEntity entity) { - Product product = new Product(); - product.setId(entity.getId()); - product.setName(entity.getName()); - product.setDescription(entity.getDescription()); - product.setPrice(entity.getPrice()); - product.setImgUrl(entity.getImgUrl()); - product.setCategory(categoryEntityMapper.mapFrom(entity.getCategory())); - return product; - } - - public void updateEntityFromProduct(ProductEntity entity, Product product) { - entity.setName(product.getName()); - entity.setDescription(product.getDescription()); - entity.setImgUrl(product.getImgUrl()); - entity.setPrice(product.getPrice()); - CategoryEntity category = - jpaCategoryRepository.getReferenceById(product.getCategory().getId()); - entity.setCategory(category); - } -}