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

Kotlin solution branch #1

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

Conversation

dirkluijk
Copy link

No description provided.

<properties>
<kotlin.version>1.7.20</kotlin.version>
</properties>

Copy link
Author

Choose a reason for hiding this comment

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

moved to root pom

fun handle(command: RejectPaymentCommand) {
if (!closed) {
AggregateLifecycle.apply(PaymentRejectedEvent(command.paymentId, paymentReference!!))
}
Copy link
Author

Choose a reason for hiding this comment

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

any tips to get rid of the non-null assertions?

Copy link

Choose a reason for hiding this comment

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

Yeah you can make the private vars private lateinit var paymentReference: String (non-nullable) and make sure to set them when handling the PaymentPreparedEvent (which you do)

paymentStatusRepository
.findById(event.paymentId)
.map { it.rejected() }
.ifPresent { paymentStatusRepository.save(it) }
Copy link
Author

Choose a reason for hiding this comment

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

Not sure if this .save() invocation is needed? It was not present in the original solution branch, but I don't know why?


@Saga
class PaymentSaga{
@Autowired @Transient lateinit var commandGateway: CommandGateway
Copy link
Author

Choose a reason for hiding this comment

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

I guess we cannot use constructor injection here?

bikeStatusRepository
.findById(event.bikeId)
.map { it.returnedAt(it.location) }
.ifPresent { bikeStatusRepository.save(it) }
Copy link
Author

@dirkluijk dirkluijk Apr 4, 2024

Choose a reason for hiding this comment

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

Not sure if this .save() invocation is needed? It was not present in the original solution branch, but I don't know why? Don't we need to update the entity? Don't we need to update the entity?

Copy link

Choose a reason for hiding this comment

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

In general to make this more Kotlin-esque you can get rid of the java optional by using this kotlin extension function that's in the spring-data repo;

CrudRepository<T, ID>.findByIdOrNull(...): T?

Copy link

Choose a reason for hiding this comment

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

for example:

val status = bikeStatusRepository.findByIdOrNull(event.bikeId) ?: throw IllegalStateException("Cannot find bike status")

Copy link
Author

Choose a reason for hiding this comment

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

Nice! Already did this for some cases :) good suggestion

fun handle(command: RejectPaymentCommand) {
if (!closed) {
AggregateLifecycle.apply(PaymentRejectedEvent(command.paymentId, paymentReference!!))
}
Copy link

Choose a reason for hiding this comment

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

Yeah you can make the private vars private lateinit var paymentReference: String (non-nullable) and make sure to set them when handling the PaymentPreparedEvent (which you do)

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