Skip to content

Commit c2d05ec

Browse files
committed
chore: refactor Payment and PaymentCounter to use UUID, add PaymentCounterService to track payment outcomes
1 parent 201f694 commit c2d05ec

4 files changed

Lines changed: 70 additions & 15 deletions

File tree

src/main/java/com/acme/payment/Payment.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class Payment {
1717

1818
@Id
19-
private String id;
19+
private UUID id;
2020

2121
@Column
2222
private BigDecimal amount;
@@ -31,13 +31,13 @@ public class Payment {
3131
}
3232

3333
Payment(Product product, BigDecimal amount, PaymentStatus status) {
34-
this.id = UUID.randomUUID().toString();
34+
this.id = UUID.randomUUID();
3535
this.amount = amount;
3636
this.status = status;
3737
this.product = product;
3838
}
3939

40-
public String getId() {
40+
public UUID getId() {
4141
return id;
4242
}
4343

@@ -54,6 +54,10 @@ public int hashCode() {
5454
return Objects.hashCode(id);
5555
}
5656

57+
public Product getProduct() {
58+
return product;
59+
}
60+
5761
@Override
5862
public String toString() {
5963
return "Payment{" +

src/main/java/com/acme/stock/PaymentCounter.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,37 @@
77
import jakarta.nosql.Entity;
88
import jakarta.nosql.Id;
99

10+
import java.util.UUID;
11+
1012
@Entity
1113
@JsonbVisibility(JsonFieldStrategy.class)
1214
public class PaymentCounter {
1315

1416
@Id
15-
private String productCode;
17+
private UUID productCode;
1618

1719
@Column
1820
private Product product;
1921

2022
@Column
21-
private int successfulPayments;
23+
int successfulPayments;
2224

2325
@Column
24-
private int failedPayments;
26+
int failedPayments;
2527

26-
public String getProductCode() {
27-
return productCode;
28+
PaymentCounter(Product product) {
29+
this.productCode = product.code();
30+
this.product = product;
2831
}
2932

30-
public Product getProduct() {
31-
return product;
33+
PaymentCounter() {
3234
}
3335

34-
public int getSuccessfulPayments() {
35-
return successfulPayments;
36+
public void failedPayments() {
37+
this.failedPayments++;
3638
}
3739

38-
public int getFailedPayments() {
39-
return failedPayments;
40+
public void successfulPayments() {
41+
this.successfulPayments++;
4042
}
4143
}

src/main/java/com/acme/stock/PaymentCounterRepository.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import jakarta.data.repository.BasicRepository;
44
import jakarta.data.repository.Repository;
55

6+
import java.util.UUID;
7+
68
@Repository
7-
public interface PaymentCounterRepository extends BasicRepository<PaymentCounter, String> {
9+
public interface PaymentCounterRepository extends BasicRepository<PaymentCounter, UUID> {
810
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.acme.stock;
2+
3+
import com.acme.Product;
4+
import com.acme.payment.Payment;
5+
import com.acme.payment.PaymentErrorEvent;
6+
import com.acme.payment.PaymentSuccessEvent;
7+
import jakarta.enterprise.context.ApplicationScoped;
8+
import jakarta.enterprise.event.Observes;
9+
import jakarta.inject.Inject;
10+
11+
import java.util.logging.Logger;
12+
13+
@ApplicationScoped
14+
public class PaymentCounterService {
15+
16+
private static final Logger LOGGER = Logger.getLogger(PaymentCounterService.class.getName());
17+
18+
private final PaymentCounterRepository repository;
19+
20+
@Inject
21+
PaymentCounterService(PaymentCounterRepository repository) {
22+
this.repository = repository;
23+
}
24+
25+
PaymentCounterService() {
26+
this.repository = null;
27+
}
28+
29+
void success(@Observes PaymentSuccessEvent event) {
30+
LOGGER.info("Payment successful, incrementing counter");
31+
Payment payment = event.payment();
32+
Product product = payment.getProduct();
33+
PaymentCounter counter = repository.findById(payment.getProduct().code()).orElseGet(() -> new PaymentCounter(product));
34+
counter.successfulPayments();
35+
repository.save(counter);
36+
}
37+
38+
39+
void success(@Observes PaymentErrorEvent event) {
40+
LOGGER.info("Payment failed, incrementing counter");
41+
Payment payment = event.payment();
42+
Product product = payment.getProduct();
43+
PaymentCounter counter = repository.findById(payment.getProduct().code()).orElseGet(() -> new PaymentCounter(product));
44+
counter.failedPayments();
45+
repository.save(counter);
46+
}
47+
}

0 commit comments

Comments
 (0)