Skip to content

Instantly share code, notes, and snippets.

@gbzarelli
Last active November 21, 2023 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbzarelli/8c92a2c9151fe1626ea6da8076e4b7d8 to your computer and use it in GitHub Desktop.
Save gbzarelli/8c92a2c9151fe1626ea6da8076e4b7d8 to your computer and use it in GitHub Desktop.
Domain model - Anemic Sample
@AllArgsConstructor
@Getter
@Setter
public class Order {
private String id;
private List<Product> items;
private Payment payment;
private BigDecimal orderAmount;
}
@AllArgsConstructor
public class OrderService {
private final OrderRepository orderRepository;
public Long payOrder(String orderId, BigDecimal amount, LocalDateTime paymentDate){
if(paymentDate.isAfter(LocalDateTime.now())){
throw new IllegalArgumentException("The payment date can't be after now!");
}
final var order = orderRepository.getOrder(orderId);
if(order.getPayment() != null){
throw new IllegalArgumentException("Order already paid");
}
if(order.getOrderAmount().compareTo(amount)!=0) {
throw new IllegalArgumentException("The order value is different from the received");
}
final var payment = Payment
.builder()
.paymentDateTime(paymentDate)
.amount(amount)
.build();
order.setPayment(payment);
return orderRepository.saveOrderPayment(order);
}
}
@Getter
@Setter
@Builder
public class Payment {
private Long paymentId;
private BigDecimal amount;
private LocalDateTime paymentDateTime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment