Skip to content

Commit 59adf7b

Browse files
committed
Document the code of analytics and customer microservices
1 parent 62eeb4e commit 59adf7b

File tree

13 files changed

+138
-1
lines changed

13 files changed

+138
-1
lines changed

analytics-service/src/main/java/com/example/analytics_service/config/ProductEventConsumerConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
import java.util.function.Consumer;
1818
import java.util.function.Function;
1919

20+
/**
21+
* The Catalog-Microservice will emit product-events
22+
* among these events, ViewedProductEvents
23+
* The Analytics-Microservice gonna receive those events wrapped in Message formats
24+
* Then it will convert them into custom Records
25+
* Consumer the Record message (or the Message payload)
26+
* Then acknowledge the messages.
27+
*/
28+
2029
@Configuration
2130
@RequiredArgsConstructor
2231
@Slf4j

analytics-service/src/main/java/com/example/analytics_service/consumer/ProductViewConsumerImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import reactor.core.publisher.Flux;
99
import reactor.core.publisher.Mono;
1010

11+
/**
12+
* The Analytics-Microservice gonna consume ViewedProductEvents
13+
*/
14+
1115
@Service
1216
@RequiredArgsConstructor
1317
@Slf4j

analytics-service/src/main/java/com/example/analytics_service/events/ProductEvent.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
import lombok.Builder;
44

5+
56
public sealed interface ProductEvent {
67

78
String code();
89

10+
/**
11+
* This ViewedProductEvent gonna be emitted by the catalog-microservice
12+
*/
13+
914
@Builder
1015
record View(String code) implements ProductEvent {}
1116
}

analytics-service/src/main/java/com/example/analytics_service/service/ProductViewService.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@ public class ProductViewService {
2626

2727
private final ProductViewRepo repo;
2828

29+
/**
30+
* Return the top 5 most viewed products in descending order
31+
*/
2932
public Flux<ProductViewDTO> products() {
3033
return repo.findTop5ByOrderByViewCountDesc()
3134
.map(Mapper.toDTO());
3235
}
3336

34-
37+
/**
38+
* Find the product by code
39+
* if it does exist, increase its viewing count by one
40+
* if it doesn't, create a new one with 1 as view count
41+
* then save into the DB
42+
*/
3543
public Function<ProductEvent.View,Mono<Void>> consume(){
3644
return event -> repo.findByProductCode(event.code())
3745
.doOnNext(p -> p.setViewCount(p.getViewCount()+1))

customer-service/src/main/java/com/example/customer_service/config/CustomerEventPublisherConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
import java.util.function.Function;
1515
import java.util.function.Supplier;
1616

17+
/**
18+
* The Customer-Microservice gonna emit CustomerEvents.
19+
* Before emitting them, it will wrap them in Message format
20+
* As the Notification-Microservice will eventually need the customer infos
21+
* such as username and email, it will consumer these customer-events
22+
* to be able to send emails.
23+
*/
24+
1725
@Configuration
1826
@RequiredArgsConstructor
1927
@Slf4j

customer-service/src/main/java/com/example/customer_service/config/OrderEventProcessorConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414

1515
import java.util.function.Function;
1616

17+
/**
18+
* The Order-Microservice gonna emit order-events.
19+
* Among the service that gonna process those events, is the Customer-Microservice
20+
* It will convert the Message of events into Record of events.
21+
* Process the Records messages (or the Message payloads)
22+
* then as a reaction, will publish customer-events wrapped into messages
23+
*/
24+
25+
1726
@Slf4j
1827
@Configuration
1928
@RequiredArgsConstructor

customer-service/src/main/java/com/example/customer_service/events/EventProcessor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
import reactor.core.publisher.Mono;
44

5+
/**
6+
* The Event process gonna process order-events which are order-saga
7+
* then publish customer-events which are order-saga as well
8+
*/
9+
10+
511
public interface EventProcessor<T extends OrderSaga, R extends OrderSaga> {
612
Mono<R> process(T event);
713
}

customer-service/src/main/java/com/example/customer_service/events/OrderEventProcessor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import reactor.core.publisher.Mono;
44

5+
/**
6+
* The customer-Microservice gonna process each order-event
7+
* be it created, completed, or cancelled
8+
*/
9+
510
public interface OrderEventProcessor extends EventProcessor<OrderEvent,PaymentEvent> {
611

712
@Override

customer-service/src/main/java/com/example/customer_service/processor/OrderEventProcessorImpl.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
public class OrderEventProcessorImpl implements OrderEventProcessor {
2121
private final PaymentService service;
2222

23+
/**
24+
* if the OrderEvent received is Created, then it will convert it into
25+
* PaymentRequest DTO so that the payment-service will be able to process it
26+
* After processing, the payment-service will return a PaymentResponse.DTO
27+
* the latter will be converted into DeductedPaymentEvent.
28+
* If a duplicate event exception is raised, then do nothing
29+
* A DeclinedPaymentEvent will be returned if either CustomerNotFoundException or NotEnoughBalanceException is raised
30+
*/
31+
2332
@Override
2433
public Mono<PaymentEvent> handle(OrderEvent.Created event) {
2534
return service.processPayment().apply(((PaymentDTO.Request) EventMapper.toRequest().apply(event)))
@@ -31,11 +40,22 @@ public Mono<PaymentEvent> handle(OrderEvent.Created event) {
3140
.onErrorResume(NotEnoughBalanceException.class, ex -> EventMapper.toDeclined().apply(ex,event));
3241
}
3342

43+
/**
44+
* If the order is completed, we basically have to do nothing
45+
*/
46+
3447
@Override
3548
public Mono<PaymentEvent> handle(OrderEvent.Completed event) {
3649
return Mono.empty();
3750
}
3851

52+
53+
/**
54+
* If the order is cancelled, we basically have to refund the payment.
55+
* we will only need event orderId to process the refund request.
56+
* The payment-service will return a PaymentResponseDTO
57+
* will converted to RefundedPaymentEvent
58+
*/
3959
@Override
4060
public Mono<PaymentEvent> handle(OrderEvent.Cancelled event) {
4161
return service.refund().apply(event.orderId())

customer-service/src/main/java/com/example/customer_service/publisher/CustomerEventPublisherImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
import org.springframework.stereotype.Service;
99
import reactor.core.publisher.Flux;
1010

11+
/**
12+
* These customer-events will be published so that
13+
* the notification-Microservice can consume them
14+
* here we've used the EventCarriedStateTransfer pattern
15+
* to avoid network calls
16+
*/
17+
18+
1119
@Service
1220
@RequiredArgsConstructor
1321
@Slf4j

0 commit comments

Comments
 (0)