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

Commit

Permalink
refactor: make dtos immutable and simplify code that needed mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
MuriloKakazu committed Jul 27, 2024
1 parent 9b8c094 commit 1e933ad
Show file tree
Hide file tree
Showing 66 changed files with 511 additions and 1,135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,19 +16,30 @@ public class Order {
private OrderStatus status;
private Customer customer;
private Payment payment;
private Collection<OrderItem> items = new LinkedList<>();
private Collection<OrderItem> 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<OrderItem>());
}

public Long getId() {
return id;
public Order(
Long id,
OrderStatus status,
Customer customer,
Payment payment,
Collection<OrderItem> 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() {
Expand All @@ -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<OrderItem> getItems() {
Expand All @@ -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();
}
Expand All @@ -76,7 +102,7 @@ public Double getTotalPrice() {
return totalPrice;
}

public void recalculateTotalPrice() {
private void recalculateTotalPrice() {
this.totalPrice = this.items.stream().mapToDouble(OrderItem::getTotalPrice).sum();
}

Expand All @@ -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);
}
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit 1e933ad

Please sign in to comment.