Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subscription data to book details #31

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

NenadJeckovic
Copy link

Subscription data included in response when requesting book details.

@NenadJeckovic NenadJeckovic linked an issue Mar 28, 2024 that may be closed by this pull request

public JsonNode generateBookDetailsDto(Object book, List<Object> rentalRecords, int availableBooksCount) {
public JsonNode generateBookDetailsDto(Object book, List<Object> rentalRecords, int availableBooksCount, boolean bookSubscription) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's good to introduce some kind of DTO when more then 3 parameters

Comment on lines 33 to 42
public Mono<Boolean> getBookSubscription(String bookId, String jwtToken) {
var subscriptionUrl = inventoryServiceUrl + "/api/inventory/subscriptions/" + bookId;

return webClient
.get()
.uri(subscriptionUrl)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + jwtToken)
.retrieve()
.bodyToMono(Boolean.class)
.onErrorReturn(RuntimeException.class, false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we create endpoint like this /api/inventory/subscriptions/{bookId}, by only reading it I would guess that it should return all book subscriptions, but I assume that you return if logged in user is subscribed or not

- id: inventory-route
uri: ${INVENTORY_SERVICE_URL}
predicates:
- Path=/api/inventory/subscriptions/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can route traffic just from /api/inventory/** part

Comment on lines 15 to 16
private Object bookDataDto;
private List<Object> rentalRecordsDto;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to have a *Dto suffix for bookData and rentalRecords?
BookDetailsDto is a fine name for the class, but I guess there is no need for DTO in the attribute names.
Can BookDetailsDto be a record instead of a class?

Comment on lines 21 to 22
var jwt = JWTParser.parse(jwtToken);
var userId = jwt.getJWTClaimsSet().getClaim(CLAIM_EMAIL).toString();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is repeated in the method below, meaning it should be extracted in a separate method.

Comment on lines 27 to 35
var bookSubscriptionMono = inventoryClient.getBookSubscription(bookId, jwtToken, userId);

var bookDetailsDtoMono = Mono.zip(bookDtoMono, rentalRecordsDtoMono, availableBooksCountMono).flatMap(tuple -> {
var book = bookDetailsResponseCombiner.generateBookDetailsDto(tuple.getT1(), tuple.getT2(), tuple.getT3());
var bookDetailsDtoMono = Mono.zip(bookDtoMono, rentalRecordsDtoMono, availableBooksCountMono, bookSubscriptionMono).flatMap(tuple -> {
var bookDetailsDto = new BookDetailsDto();
bookDetailsDto.setBookDataDto(tuple.getT1());
bookDetailsDto.setRentalRecordsDto(tuple.getT2());
bookDetailsDto.setAvailableBookCount(tuple.getT3());
bookDetailsDto.setBookSubscription(tuple.getT4());
var book = bookDetailsResponseCombiner.generateBookDetailsDto(bookDetailsDto);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is repeated as well, getBookDetailsById and getBookDetailsByTitleAndAuthor can be optimized to avoid code duplication.

public Mono<Integer> getAvailableBookCopiesCount(String bookId, String jwtToken){
var inventoryBookUrl = inventoryServiceUrl + bookId;
public Mono<Integer> getAvailableBookCopiesCount(String bookId, String jwtToken) {
var inventoryBookUrl = inventoryServiceUrl + "/api/inventory/book/" + bookId;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an old URL, but it would be great to fix it and use the plural books instead of book.
Currently, we have /api/inventory/book/bookId and /api/inventory/books/bookId

@SneakyThrows
public JsonNode getBookDetailsById(String bookId, String jwtToken) {
var userId = getClaimEmail(jwtToken);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename getClaimEmail toextractUserId or similar, because userId is what you get

@@ -28,13 +29,16 @@ class BookServiceShould {
private static final String BOOK_ID = "1";
private static final String BOOK_TITLE = "::title::";
private static final String BOOK_AUTHOR = "::author::";
private static final String JWT_TOKEN = "";
private static final String JWT_TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImVtYWlsIn0.9_82fPfiHdHoOkyx8WQY8FsgPivtguil3QL5a3bDE7g";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be avoided to store any secrets in Git, even if it's used for testing.
Is it possible to create an Util class that will generate for you a token which can be used for the test?
Please investigate and let me know

Comment on lines 14 to 18
public class BookSubscriptionDto {
private String bookId;
private String userId;
private Date createdDate;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this DTO?

Copy link

sonarcloud bot commented Apr 16, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Collect subscription data when opening a book details
3 participants