Skip to content

Commit

Permalink
chore: remove static methods from the quickstart domain model (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
aludwiko authored Nov 23, 2024
1 parent 736510e commit 9d3d41b
Showing 1 changed file with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ public LineItem withQuantity(int quantity) {
// tag::itemAdded[]
public ShoppingCart onItemAdded(ShoppingCartEvent.ItemAdded itemAdded) {
var item = itemAdded.item();
var lineItem = updateItem(item, this); // <1>
List<LineItem> lineItems = removeItemByProductId(this, item.productId()); // <2>
var lineItem = updateItem(item); // <1>
List<LineItem> lineItems = removeItemByProductId(item.productId()); // <2>
lineItems.add(lineItem); // <3>
lineItems.sort(Comparator.comparing(LineItem::productId));
return new ShoppingCart(cartId, lineItems, checkedOut); // <4>
}

private static LineItem updateItem(LineItem item, ShoppingCart cart) {
return cart.findItemByProductId(item.productId())
private LineItem updateItem(LineItem item) {
return findItemByProductId(item.productId())
.map(li -> li.withQuantity(li.quantity() + item.quantity()))
.orElse(item);
}

private static List<LineItem> removeItemByProductId(ShoppingCart cart, String productId) {
return cart.items().stream()
private List<LineItem> removeItemByProductId(String productId) {
return items().stream()
.filter(lineItem -> !lineItem.productId().equals(productId))
.collect(Collectors.toList());
}
Expand All @@ -56,7 +56,7 @@ public Optional<LineItem> findItemByProductId(String productId) {

public ShoppingCart onItemRemoved(ShoppingCartEvent.ItemRemoved itemRemoved) {
List<LineItem> updatedItems =
removeItemByProductId(this, itemRemoved.productId());
removeItemByProductId(itemRemoved.productId());
updatedItems.sort(Comparator.comparing(LineItem::productId));
return new ShoppingCart(cartId, updatedItems, checkedOut);
}
Expand Down

0 comments on commit 9d3d41b

Please sign in to comment.