BAEL-2275: Change orderitem to be built based on product
This commit is contained in:
parent
3cde05a6ed
commit
1f94507c52
|
@ -45,8 +45,7 @@ public class Order {
|
|||
return orderItems
|
||||
.stream()
|
||||
.filter(orderItem -> orderItem
|
||||
.getProduct()
|
||||
.getId()
|
||||
.getProductId()
|
||||
.equals(id))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new DomainException("Product with " + id + " doesn't exist."));
|
||||
|
|
|
@ -2,20 +2,23 @@ package com.baeldung.ddd.layers.domain;
|
|||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
public class OrderItem {
|
||||
private final Product product;
|
||||
private final UUID productId;
|
||||
private final BigDecimal price;
|
||||
|
||||
public OrderItem(final Product product) {
|
||||
this.product = product;
|
||||
this.productId = product.getId();
|
||||
this.price = product.getPrice();
|
||||
}
|
||||
|
||||
public UUID getProductId() {
|
||||
return productId;
|
||||
}
|
||||
|
||||
public BigDecimal getPrice() {
|
||||
return product.getPrice();
|
||||
}
|
||||
|
||||
public Product getProduct() {
|
||||
return product;
|
||||
return price;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -23,11 +26,11 @@ public class OrderItem {
|
|||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
OrderItem orderItem = (OrderItem) o;
|
||||
return Objects.equals(product, orderItem.product);
|
||||
return Objects.equals(productId, orderItem.productId) && Objects.equals(price, orderItem.price);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(product);
|
||||
return Objects.hash(productId, price);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,12 +50,12 @@ class OrderUnitTest {
|
|||
@Test
|
||||
void shouldRemoveProduct_thenUpdatePrice() {
|
||||
final Order order = OrderProvider.getCreatedOrder();
|
||||
|
||||
order.removeOrder(order
|
||||
final UUID productId = order
|
||||
.getOrderItems()
|
||||
.get(0)
|
||||
.getProduct()
|
||||
.getId());
|
||||
.getProductId();
|
||||
|
||||
order.removeOrder(productId);
|
||||
|
||||
assertEquals(0, order
|
||||
.getOrderItems()
|
||||
|
|
|
@ -80,8 +80,8 @@ class DomainOrderServiceUnitTest {
|
|||
final UUID productId = order
|
||||
.getOrderItems()
|
||||
.get(0)
|
||||
.getProduct()
|
||||
.getId();
|
||||
.getProductId();
|
||||
|
||||
when(orderRepository.findById(order.getId())).thenReturn(Optional.of(order));
|
||||
|
||||
tested.deleteProduct(order.getId(), productId);
|
||||
|
|
Loading…
Reference in New Issue