Skip to content

DomainEvent #8

@CamilYed

Description

@CamilYed

In fact, some classes that implement the OrderDomainEvent interface aren't exactly domain events.

  public static record ItemQuantityChangeFailedBecauseAlreadyPaid(
    OrderId orderId,
    VinylId vinylId
  ) implements OrderDomainEvent {
  }

  public static record ItemQuantityChangeFailedBecauseNotExists(
    OrderId orderId,
    VinylId notExistingItem
  ) implements OrderDomainEvent {
  }

  public static record OrderPayFailedBecauseAlreadyPaid(
    OrderId orderId,
    Instant when
  ) implements OrderDomainEvent {
  }

  public static record OrderPayFailedBecauseDifferentAmounts(
    OrderId orderId,
    Instant when,
    Money difference
  ) implements OrderDomainEvent {
  }


  public OrderDomainEvent pay(Money amount, Delivery delivery) {
    if (unpaid) {
      final var toPay = orderLines.totalCost().add(delivery.cost());
      if (amount.equalTo(toPay)) {
        this.delivery = delivery;
        unpaid = false;
        return orderPaidSuccessfully();
      } else {
        return amountToBePaidIsDifferent(amount.subtract(toPay));
      }
    } else return orderPayFailedBecauseAlreadyPaid();
  }

  public OrderDomainEvent changeItemQuantity(VinylId product, QuantityChange change) {
    if (unpaid) {
      final var orderLine = orderLines.findLineToUpdate(product);
      if (orderLine.isPresent()) {
        final var lineToUpdate = orderLine.get();
        final var updatedLine = lineToUpdate.changeQuantity(change);
        orderLines.add(updatedLine);
        return itemQuantityChanged(product, updatedLine.quantity());
      }
      return itemDoesNotExists(product);
    }
    return itemQuantityChangeFailedBecauseAlreadyPaid(product);
  }

Maybe aggregate should return some object called Result, or throw some exceptions...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions