BAEL-2275: Change orderitem to be built based on product

This commit is contained in:
Lukasz Rys 2019-11-17 21:27:07 +01:00
parent 3cde05a6ed
commit 1f94507c52
4 changed files with 19 additions and 17 deletions

View File

@ -45,8 +45,7 @@ public class Order {
return orderItems return orderItems
.stream() .stream()
.filter(orderItem -> orderItem .filter(orderItem -> orderItem
.getProduct() .getProductId()
.getId()
.equals(id)) .equals(id))
.findFirst() .findFirst()
.orElseThrow(() -> new DomainException("Product with " + id + " doesn't exist.")); .orElseThrow(() -> new DomainException("Product with " + id + " doesn't exist."));

View File

@ -2,20 +2,23 @@ package com.baeldung.ddd.layers.domain;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Objects; import java.util.Objects;
import java.util.UUID;
public class OrderItem { public class OrderItem {
private final Product product; private final UUID productId;
private final BigDecimal price;
public OrderItem(final Product product) { public OrderItem(final Product product) {
this.product = product; this.productId = product.getId();
this.price = product.getPrice();
}
public UUID getProductId() {
return productId;
} }
public BigDecimal getPrice() { public BigDecimal getPrice() {
return product.getPrice(); return price;
}
public Product getProduct() {
return product;
} }
@Override @Override
@ -23,11 +26,11 @@ public class OrderItem {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
OrderItem orderItem = (OrderItem) o; OrderItem orderItem = (OrderItem) o;
return Objects.equals(product, orderItem.product); return Objects.equals(productId, orderItem.productId) && Objects.equals(price, orderItem.price);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(product); return Objects.hash(productId, price);
} }
} }

View File

@ -50,12 +50,12 @@ class OrderUnitTest {
@Test @Test
void shouldRemoveProduct_thenUpdatePrice() { void shouldRemoveProduct_thenUpdatePrice() {
final Order order = OrderProvider.getCreatedOrder(); final Order order = OrderProvider.getCreatedOrder();
final UUID productId = order
order.removeOrder(order
.getOrderItems() .getOrderItems()
.get(0) .get(0)
.getProduct() .getProductId();
.getId());
order.removeOrder(productId);
assertEquals(0, order assertEquals(0, order
.getOrderItems() .getOrderItems()

View File

@ -80,8 +80,8 @@ class DomainOrderServiceUnitTest {
final UUID productId = order final UUID productId = order
.getOrderItems() .getOrderItems()
.get(0) .get(0)
.getProduct() .getProductId();
.getId();
when(orderRepository.findById(order.getId())).thenReturn(Optional.of(order)); when(orderRepository.findById(order.getId())).thenReturn(Optional.of(order));
tested.deleteProduct(order.getId(), productId); tested.deleteProduct(order.getId(), productId);