Last active
November 21, 2023 14:24
-
-
Save gbzarelli/8c92a2c9151fe1626ea6da8076e4b7d8 to your computer and use it in GitHub Desktop.
Domain model - Anemic Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@AllArgsConstructor | |
@Getter | |
@Setter | |
public class Order { | |
private String id; | |
private List<Product> items; | |
private Payment payment; | |
private BigDecimal orderAmount; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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