1313import jakarta .inject .Inject ;
1414
1515import java .util .List ;
16+ import java .util .function .Consumer ;
1617import 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