Skip to content

Commit 76d2fac

Browse files
committed
chore: refactor PaymentCounterService to extract counter update logic into execute method
1 parent f78f439 commit 76d2fac

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/main/java/com/acme/statistics/PaymentCounterService.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import jakarta.inject.Inject;
1414

1515
import java.util.List;
16+
import java.util.function.Consumer;
1617
import java.util.logging.Logger;
1718

1819
@ApplicationScoped
@@ -35,20 +36,20 @@ public class PaymentCounterService {
3536

3637
void success(@Observes PaymentConfirmedEvent event) {
3738
LOGGER.info("Payment successful, incrementing counter: " + event.payment().getId());
38-
Payment payment = event.payment();
39-
Product product = payment.getProduct();
40-
PaymentCounter counter = repository.findById(payment.getProduct().code()).orElseGet(() -> new PaymentCounter(product));
41-
counter.paymentSucceeded();
42-
repository.save(counter);
39+
execute(PaymentCounter::paymentSucceeded, event.payment().getProduct());
4340
}
4441

4542

4643
void success(@Observes PaymentFailedEvent event) {
4744
LOGGER.info("Payment failed, incrementing counter: " + event.payment().getId());
48-
Payment payment = event.payment();
49-
Product product = payment.getProduct();
50-
PaymentCounter counter = repository.findById(payment.getProduct().code()).orElseGet(() -> new PaymentCounter(product));
51-
counter.paymentFailed();
45+
execute(PaymentCounter::paymentFailed, event.payment().getProduct());
46+
}
47+
48+
void execute(Consumer<PaymentCounter> action, Product product) {
49+
PaymentCounter counter = repository.findById(product.code()).orElseGet(() -> new PaymentCounter(product));
50+
LOGGER.info("Counter found: " + counter);
51+
action.accept(counter);
52+
LOGGER.info("Counter incremented: " + counter);
5253
repository.save(counter);
5354
}
5455

0 commit comments

Comments
 (0)