Skip to content

Commit 0a76593

Browse files
authored
Merge pull request #9 from BOURGUI07/docs
Document the code of the rating-service
2 parents 3182490 + a805616 commit 0a76593

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

rating-service/src/main/java/com/example/ratingservice/config/OrderEventConsumerConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
import org.springframework.messaging.Message;
1313
import reactor.core.publisher.Flux;
1414

15+
/**
16+
* The moment a order is completed, the order-microservice will emit
17+
* order-events. Among the microservices that will consume those events is
18+
* the rating-microservice. So that whenever a user wants to write a rating for a product
19+
* the rating service will be able to validate if the productId and customerId were part of some order.
20+
*/
21+
22+
1523
@Configuration
1624
@Slf4j
1725
@RequiredArgsConstructor

rating-service/src/main/java/com/example/ratingservice/service/OrderService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
import org.springframework.transaction.annotation.Transactional;
1313
import reactor.core.publisher.Mono;
1414

15+
/**
16+
* The order-service is responsible for persisting the completed orders.
17+
*/
18+
1519
@Service
1620
@RequiredArgsConstructor
1721
@Slf4j
@@ -20,6 +24,12 @@ public class OrderService {
2024

2125
private final OrderHistoryRepo repo;
2226

27+
/**
28+
* Make sure the order hasn't been persisted before, if so raise DuplicateException
29+
* Convert the event into OrderHistory entity
30+
* save the entity
31+
* convert it into dto
32+
*/
2333
public Mono<Void> saveOrder(OrderEvent.Completed event) {
2434
return repo.existsByOrderId(event.orderId())
2535
.filter(Predicate.not(b -> b))

rating-service/src/main/java/com/example/ratingservice/service/RatingService.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
import org.springframework.transaction.annotation.Transactional;
2626
import reactor.core.publisher.Mono;
2727

28+
/**
29+
* The rating-service is responsible for standard CRUD endpoints.
30+
*/
31+
32+
2833
@Service
2934
@RequiredArgsConstructor
3035
@Slf4j
@@ -33,6 +38,15 @@ public class RatingService {
3338
private final RatingRepo ratingRepo;
3439
private final RatingServiceProperties properties;
3540

41+
/**
42+
* Receive the request
43+
* validate the fields nullability
44+
* make sure the orderId exists
45+
* make sure the request productId and customerId equals to the existing OrderHistory entity
46+
* convert the request into entity
47+
* save the entity
48+
* convert the saved entity into response dto
49+
*/
3650
@Transactional
3751
public Mono<RatingResponse> createRating(Mono<RatingCreationRequest> request) {
3852
return RatingRequestValidator.validate()
@@ -63,6 +77,13 @@ private BiPredicate<RatingCreationRequest, OrderHistory> hasValidProductAndCusto
6377
&& Objects.equals(request.productId(), orderHistory.getProductId());
6478
}
6579

80+
81+
/**
82+
* make sure the ratingId exists. if not, raise notFound exception
83+
* update the fields. The user has to update at least one field.
84+
* save the rating entity
85+
* convert the saved entity into response dto
86+
*/
6687
@Transactional
6788
public Mono<RatingResponse> updateRating(Long ratingId, Mono<RatingUpdateRequest> request) {
6889
return ratingRepo
@@ -87,6 +108,13 @@ private BiFunction<Rating, RatingUpdateRequest, Mono<RatingResponse>> executeUpd
87108
};
88109
}
89110

111+
112+
/**
113+
* find the rating entities by customerId
114+
* convert it into dto
115+
* convert the flux into list
116+
* convert the list into paginated response
117+
*/
90118
public Mono<PaginatedRatingResponse> findByCustomerId(Long customerId, int page) {
91119
var pageNumber = page >= 1 ? page - 1 : 0;
92120
PageRequest pageable = PageRequest.of(

rating-service/src/main/java/com/example/ratingservice/validator/RatingRequestValidator.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
import java.util.function.UnaryOperator;
88
import reactor.core.publisher.Mono;
99

10+
/**
11+
* The RatingCreationRequest should basically have:
12+
* customerId non-null
13+
* productId non-null
14+
* orderId non-null
15+
* the rating value non-null as well as to be between 1 and 5.
16+
*/
17+
1018
public class RatingRequestValidator {
1119

1220
public static UnaryOperator<Mono<RatingCreationRequest>> validate() {

0 commit comments

Comments
 (0)