-
-
Save pawelpluta/9a3c345fb2a932961ab9759b83d59781 to your computer and use it in GitHub Desktop.
Article: Tell don't ask - sales with disconnected domain model
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
public class Sales { | |
private final CrateRepository crateRepository; | |
private final DeliveryService deliveryService; | |
public Sales(CrateRepository crateRepository, DeliveryService deliveryService) { | |
this.crateRepository = crateRepository; | |
this.deliveryService = deliveryService; | |
} | |
public SaleResult sell(Integer soldCrates, ProductType type, Customer customer) { | |
Crates crates = Crates.of(crateRepository.findByTypeLimit(type, soldCrates)); | |
if (!crates.hasAtLeastCountOf(soldCrates)) { | |
return SaleResult.productOutOfStock(); | |
} | |
Package packageToDeliver = crates.packFor(customer); | |
if (packageToDeliver.send(deliveryService) == ACCEPTED) { | |
crateRepository.delete(packageToDeliver.deliveredCrates()); | |
return SaleResult.sold(); | |
} | |
return SaleResult.receiverAddressUnknown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment