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

chore: simplifying workflow samples #1716

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.example.Main;
import com.example.transfer.TransferState.Transfer;
import com.example.wallet.WalletEntity.Balance;
import kalix.spring.testkit.KalixIntegrationTestKitSupport;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -132,12 +131,12 @@ private void createWallet(String walletId, int amount) {
}

private int getWalletBalance(String walletId) {
Balance response = webClient.get().uri("/wallet/" + walletId)
Integer response = webClient.get().uri("/wallet/" + walletId)
.retrieve()
.bodyToMono(Balance.class)
.bodyToMono(Integer.class)
.block(timeout);

return response.value();
return response;
}

private TransferState getTransferState(String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ public Wallet deposit(int amount) {
}
}

public record Balance(int value) {
}

public sealed interface WithdrawResult {
record WithdrawFailed(String errorMsg) implements WithdrawResult {
}
Expand Down Expand Up @@ -58,11 +55,10 @@ public Effect<String> create(@PathVariable String id, @PathVariable int initBala

@PatchMapping("/withdraw/{amount}")
public Effect<WithdrawResult> withdraw(@PathVariable int amount) {
var newBalance = currentState().balance() - amount;
if (newBalance < 0) {
Wallet updatedWallet = currentState().withdraw(amount);
if (updatedWallet.balance < 0) {
return effects().reply(new WithdrawFailed("Insufficient balance"));
} else {
Wallet updatedWallet = currentState().withdraw(amount);
// end::wallet[]
logger.info("Withdraw walletId: [{}] amount -{} balance after {}", currentState().id(), amount, updatedWallet.balance());
// tag::wallet[]
Expand All @@ -84,8 +80,8 @@ public Effect<DepositResult> deposit(@PathVariable int amount) {
}

@GetMapping // <4>
public Effect<Balance> get() {
return effects().reply(new Balance(currentState().balance()));
public Effect<Integer> get() {
return effects().reply(currentState().balance());
}
}
// end::wallet[]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.example.Main;
import com.example.transfer.TransferState.Transfer;
import com.example.wallet.WalletEntity.Balance;
import kalix.spring.testkit.KalixIntegrationTestKitSupport;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -73,12 +72,12 @@ private void createWallet(String walletId, int amount) {
}

private int getWalletBalance(String walletId) {
Balance response = webClient.get().uri("/wallet/" + walletId)
Integer response = webClient.get().uri("/wallet/" + walletId)
.retrieve()
.bodyToMono(Balance.class)
.bodyToMono(Integer.class)
.block(timeout);

return response.value();
return response;
}

private TransferState getTransferState(String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public Wallet deposit(int amount) {
}
}

public record Balance(int value) {
}

// end::wallet[]

private static final Logger logger = LoggerFactory.getLogger(WalletEntity.class);
Expand All @@ -36,11 +33,10 @@ public Effect<String> create(@PathVariable String id, @PathVariable int initBala

@PatchMapping("/withdraw/{amount}") // <2>
public Effect<String> withdraw(@PathVariable int amount) {
var newBalance = currentState().balance() - amount;
if (newBalance < 0) {
Wallet updatedWallet = currentState().withdraw(amount);
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, why not, just please add similar changes to java-spring-transfer-workflow-compensation

if (updatedWallet.balance < 0) {
return effects().error("Insufficient balance");
} else {
Wallet updatedWallet = currentState().withdraw(amount);
// end::wallet[]
logger.info("Withdraw walletId: [{}] amount -{} balance after {}", currentState().id(), amount, updatedWallet.balance());
// tag::wallet[]
Expand All @@ -58,8 +54,8 @@ public Effect<String> deposit(@PathVariable int amount) {
}

@GetMapping // <4>
public Effect<Balance> get() {
return effects().reply(new Balance(currentState().balance()));
public Effect<Integer> get() {
Copy link
Member

Choose a reason for hiding this comment

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

It's better to return a complex type.

I think that returning Interge will have a similar effect as returning a String (see #1686).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Although, I don't think an Integer would compose as a String but I don't know how to test it. How did you?
On the other hand, if it does compose like a String I think we could add a comment somewhere in the docs.

Copy link
Member

Choose a reason for hiding this comment

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

you are right, it will just be a raw number being returned with content type json

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool. We agree then 👍

return effects().reply(currentState().balance());
}
}
// end::wallet[]