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

Commit

Permalink
Merge pull request #15 from 7SOATSquad30/refactor/move-order-invariants
Browse files Browse the repository at this point in the history
refactor: move order invariants to the entity
  • Loading branch information
MuriloKakazu authored May 20, 2024
2 parents 9b24d05 + 0afc62f commit 1a979bb
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 33 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package br.com.fiap.grupo30.fastfood.application.services.exceptions;

import java.io.Serial;
import java.util.Collection;

public class CompositeDomainValidationException extends ResourceBadRequestException {

@Serial private static final long serialVersionUID = 1L;

public CompositeDomainValidationException(Collection<String> domainErrors) {
super(String.join(", ", domainErrors));
}

public CompositeDomainValidationException(
Collection<String> domainErrors, Throwable exception) {
super(String.join(", ", domainErrors), exception);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package br.com.fiap.grupo30.fastfood.domain.usecases.order;

import br.com.fiap.grupo30.fastfood.application.dto.OrderDTO;
import br.com.fiap.grupo30.fastfood.application.services.exceptions.CantChangeOrderProductsAfterSubmitException;
import br.com.fiap.grupo30.fastfood.application.services.exceptions.ResourceNotFoundException;
import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.OrderEntity;
import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.ProductEntity;
Expand Down Expand Up @@ -31,10 +30,6 @@ public OrderDTO execute(Long orderId, Long productId, Long productQuantity) {
.findById(orderId)
.orElseThrow(() -> new ResourceNotFoundException("Order not found"));

if (!order.isDraft()) {
throw new CantChangeOrderProductsAfterSubmitException();
}

ProductEntity product =
this.productRepository
.findById(productId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package br.com.fiap.grupo30.fastfood.domain.usecases.order;

import br.com.fiap.grupo30.fastfood.application.dto.OrderDTO;
import br.com.fiap.grupo30.fastfood.application.services.exceptions.CantChangeOrderProductsAfterSubmitException;
import br.com.fiap.grupo30.fastfood.application.services.exceptions.ResourceNotFoundException;
import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.OrderEntity;
import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.ProductEntity;
Expand Down Expand Up @@ -29,10 +28,6 @@ public OrderDTO execute(Long orderId, Long productId) {
.findById(orderId)
.orElseThrow(() -> new ResourceNotFoundException("Order not found"));

if (!order.isDraft()) {
throw new CantChangeOrderProductsAfterSubmitException();
}

ProductEntity product =
this.productRepository
.findById(productId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package br.com.fiap.grupo30.fastfood.domain.usecases.order;

import br.com.fiap.grupo30.fastfood.application.dto.OrderDTO;
import br.com.fiap.grupo30.fastfood.application.services.exceptions.CantSubmitOrderWithoutProductsException;
import br.com.fiap.grupo30.fastfood.application.services.exceptions.ResourceNotFoundException;
import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.OrderEntity;
import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.OrderStatus;
Expand All @@ -25,10 +24,6 @@ public OrderDTO execute(Long orderId) {
.findById(orderId)
.orElseThrow(() -> new ResourceNotFoundException("Order not found"));

if (!order.hasProducts()) {
throw new CantSubmitOrderWithoutProductsException();
}

order.setStatus(OrderStatus.SUBMITTED);
return this.orderRepository.save(order).toDTO();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import br.com.fiap.grupo30.fastfood.application.dto.OrderDTO;
import br.com.fiap.grupo30.fastfood.application.dto.OrderItemDTO;
import br.com.fiap.grupo30.fastfood.application.services.exceptions.CantChangeOrderProductsAfterSubmitException;
import br.com.fiap.grupo30.fastfood.application.services.exceptions.CompositeDomainValidationException;
import jakarta.persistence.*;
import java.time.Instant;
import java.util.Collection;
Expand Down Expand Up @@ -42,6 +44,10 @@ public class OrderEntity {
private Instant deletedAt;

public void addProduct(ProductEntity product, Long quantity) {
if (!this.isDraft()) {
throw new CantChangeOrderProductsAfterSubmitException();
}

this.items.stream()
.filter(orderItem -> orderItem.getProduct().equals(product))
.findFirst()
Expand All @@ -54,6 +60,10 @@ public void addProduct(ProductEntity product, Long quantity) {
}

public void removeProduct(ProductEntity product) {
if (!this.isDraft()) {
throw new CantChangeOrderProductsAfterSubmitException();
}

this.items.removeIf(orderItem -> orderItem.getProduct().equals(product));

this.recalculateTotalPrice();
Expand All @@ -67,18 +77,30 @@ private void recalculateTotalPrice() {
this.totalPrice = this.items.stream().mapToDouble(item -> item.getTotalPrice()).sum();
}

public Boolean isDraft() {
private Boolean isDraft() {
return OrderStatus.DRAFT.equals(this.status);
}

public Boolean hasProducts() {
private Boolean hasProducts() {
return !this.items.isEmpty();
}

public void setStatus(OrderStatus status) {
this.status = status;
}

public void validate() {
var errors = new LinkedList<String>();

if (this.status == OrderStatus.SUBMITTED && !this.hasProducts()) {
errors.add("Can not submit order without products");
}

if (!errors.isEmpty()) {
throw new CompositeDomainValidationException(errors);
}
}

@PostLoad
protected void postLoad() {
recalculateTotalPrice();
Expand All @@ -87,11 +109,13 @@ protected void postLoad() {
@PrePersist
protected void prePersist() {
createdAt = Instant.now();
this.validate();
}

@PreUpdate
protected void preUpdate() {
updatedAt = Instant.now();
this.validate();
}

@PreRemove
Expand Down

0 comments on commit 1a979bb

Please sign in to comment.