Skip to content

Conversation

@rdsmart97
Copy link

@rdsmart97 rdsmart97 commented Dec 9, 2022

Details

Revision 2:

Refactoring PaymentController and PaymentControllerTest code to revmove redundant variables.

Why Are We Making This Change?

  • We want to be able to retrieve payment information for a user. It's possible for a user to have multiple payment methods so all payment methods should be returned for the specified user.

What Is Changing In This Review?

  • Added new controller for Payment APIs
  • Added new service for Payment APIs
  • Added API to retrieve payment by user Id
  • Added test payment data to data.sql for local testing
  • Changed substring length for ResourceMapper convert payment method because the old value was resulting in a string of 5 characters for "setCardNumberLast4".

How Was This Tested?


@GetMapping(value = "/retrieve/{userId}")
@ResponseBody
public List<PaymentDto> retrieve(@NonNull final @PathVariable String userId) {
Copy link
Member

Choose a reason for hiding this comment

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

What benefit does @NonNull provide here?

Copy link
Author

Choose a reason for hiding this comment

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

I added the @nonnull annotation here to make sure that a NullPointerException would be thrown if this method is ever called without userId since userId is required for this method.

Copy link
Author

Choose a reason for hiding this comment

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

I just tested this some more by making curl calls to the API on my terminal. It seems like even if I don't provide a userId the API just treats this as a userId with the value of EMPTYSTRING so I think you are right and this check is not needed. I think the only way userId will be null is if someone specifically passes in an encoded version of null and when I tested that, it was causing the API to throw a BadRequest exception so I think this is already handled.

final List<Integer> ids = IntStream.range(1, 10).boxed().collect(Collectors.toList());

this.payments = ids.stream().map(id -> createPaymentDto(Integer.toString(id)))
.collect(Collectors.toList());
Copy link
Member

Choose a reason for hiding this comment

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

ids variable is unnecessary since it's only used once. You can combine these statements together.

this.payments = IntStream.range(1, 10).boxed()
    .map(id -> createPaymentDto(Integer.toString(id)))
    .collect(Collectors.toList())

Copy link
Author

Choose a reason for hiding this comment

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

Good call out. I have refactored the code and fixed this to removed the redundant variable.

final String cardNumber = payment.getCardNumber();
final PaymentDto dto = mapperFacade.map(payment, PaymentDto.class);
dto.setCardNumberLast4(cardNumber.substring(cardNumber.length() - 5));
dto.setCardNumberLast4(cardNumber.substring(cardNumber.length() - 4));
Copy link
Member

Choose a reason for hiding this comment

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

Good job catching this bug! 👍🏻

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.

2 participants