-
Notifications
You must be signed in to change notification settings - Fork 3
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: aligning view store sample #118
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -318,26 +318,3 @@ include::example$key-value-customer-registry/src/test/java/customer/application/ | |
Views are not replicated directly in the same way as for example xref:event-sourced-entities.adoc#_replication[Event Sourced Entity replication]. A View is built from entities in the same service, or another service, in the same region. The entities will replicate all events across regions and identical Views are built in each region. | ||
|
||
A View can also be built from a message broker topic, and that could be regional or global depending on how the message broker is configured. | ||
|
||
== Exposing views directly | ||
|
||
include::partial$component-endpoint.adoc[] | ||
|
||
=== API | ||
|
||
The view is exposed at a fixed path: | ||
|
||
[source] | ||
---- | ||
/akka/v1.0/view/<component id>/<method> | ||
---- | ||
|
||
Taking the sample from the <<value-entity, first section>> as an example, that would be: | ||
|
||
[source,shell] | ||
---- | ||
curl localhost:9000/akka/v1.0/view/view_customers_by_email/getCustomer \ | ||
--header "Content-Type: application/json" \ | ||
-XPOST \ | ||
--data '{"email":"[email protected]"}' | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
samples/view-store/src/main/java/store/customer/api/CustomerEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package store.customer.api; | ||
|
||
import akka.http.javadsl.model.HttpResponse; | ||
import akka.javasdk.annotations.Acl; | ||
import akka.javasdk.annotations.http.Get; | ||
import akka.javasdk.annotations.http.HttpEndpoint; | ||
import akka.javasdk.annotations.http.Post; | ||
import akka.javasdk.client.ComponentClient; | ||
import store.customer.application.CustomerEntity; | ||
import store.customer.domain.Customer; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
import static akka.javasdk.http.HttpResponses.created; | ||
|
||
@HttpEndpoint("/customers") | ||
@Acl(allow = @Acl.Matcher(principal = Acl.Principal.INTERNET)) | ||
public class CustomerEndpoint { | ||
|
||
private final ComponentClient componentClient; | ||
|
||
public CustomerEndpoint(ComponentClient componentClient) { | ||
this.componentClient = componentClient; | ||
} | ||
|
||
@Post("/{customerId}") | ||
public CompletionStage<HttpResponse> create(String customerId, Customer customer) { | ||
return componentClient.forEventSourcedEntity(customerId) | ||
.method(CustomerEntity::create) | ||
.invokeAsync(customer) | ||
.thenApply(__ -> created()); | ||
} | ||
|
||
@Get("/{customerId}") | ||
public CompletionStage<Customer> get(String customerId) { | ||
return componentClient.forEventSourcedEntity(customerId) | ||
.method(CustomerEntity::get) | ||
.invokeAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
samples/view-store/src/main/java/store/order/api/OrderEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package store.order.api; | ||
|
||
import akka.http.javadsl.model.HttpResponse; | ||
import akka.javasdk.annotations.Acl; | ||
import akka.javasdk.annotations.http.Get; | ||
import akka.javasdk.annotations.http.HttpEndpoint; | ||
import akka.javasdk.annotations.http.Post; | ||
import akka.javasdk.client.ComponentClient; | ||
import store.order.application.CreateOrder; | ||
import store.order.application.OrderEntity; | ||
import store.order.domain.Order; | ||
import store.order.view.joined.JoinedCustomerOrdersView; | ||
import store.order.view.nested.CustomerOrders; | ||
import store.order.view.nested.NestedCustomerOrdersView; | ||
import store.order.view.structured.StructuredCustomerOrdersView; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
import static akka.javasdk.http.HttpResponses.created; | ||
|
||
@HttpEndpoint("/orders") | ||
@Acl(allow = @Acl.Matcher(principal = Acl.Principal.INTERNET)) | ||
public class OrderEndpoint { | ||
|
||
private final ComponentClient componentClient; | ||
|
||
public OrderEndpoint(ComponentClient componentClient) { | ||
this.componentClient = componentClient; | ||
} | ||
|
||
@Post("/{orderId}") | ||
public CompletionStage<HttpResponse> create(String orderId, CreateOrder createOrder) { | ||
return componentClient.forKeyValueEntity(orderId) | ||
.method(OrderEntity::create) | ||
.invokeAsync(createOrder) | ||
.thenApply(__ -> created()); | ||
} | ||
|
||
@Get("/{orderId}") | ||
public CompletionStage<Order> get(String orderId) { | ||
return componentClient.forKeyValueEntity(orderId) | ||
.method(OrderEntity::get) | ||
.invokeAsync(); | ||
} | ||
|
||
@Get("/joined-by-customer/{customerId}") | ||
public CompletionStage<JoinedCustomerOrdersView.CustomerOrders> joinedByCustomer(String customerId) { | ||
return componentClient.forView() | ||
.method(JoinedCustomerOrdersView::get) | ||
.invokeAsync(customerId); | ||
} | ||
|
||
@Get("/nested-by-customer/{customerId}") | ||
public CompletionStage<CustomerOrders> nestedByCustomer(String customerId) { | ||
return componentClient.forView() | ||
.method(NestedCustomerOrdersView::get) | ||
.invokeAsync(customerId); | ||
} | ||
|
||
@Get("/structured-by-customer/{customerId}") | ||
public CompletionStage<store.order.view.structured.CustomerOrders> structuredByCustomer(String customerId) { | ||
return componentClient.forView() | ||
.method(StructuredCustomerOrdersView::get) | ||
.invokeAsync(customerId); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ain/java/store/order/api/CreateOrder.java → .../store/order/application/CreateOrder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package store.order.api; | ||
package store.order.application; | ||
|
||
public record CreateOrder(String productId, String customerId, int quantity) { | ||
} |
9 changes: 6 additions & 3 deletions
9
...ain/java/store/order/api/OrderEntity.java → .../store/order/application/OrderEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
package store.order.api; | ||
package store.order.application; | ||
|
||
import akka.Done; | ||
import akka.javasdk.annotations.ComponentId; | ||
import akka.javasdk.keyvalueentity.KeyValueEntity; | ||
import store.order.domain.Order; | ||
|
||
import java.time.Instant; | ||
|
||
import static akka.Done.done; | ||
|
||
@ComponentId("order") | ||
public class OrderEntity extends KeyValueEntity<Order> { | ||
|
||
public Effect<Order> get() { | ||
return effects().reply(currentState()); | ||
} | ||
|
||
public Effect<String> create(CreateOrder createOrder) { | ||
public Effect<Done> create(CreateOrder createOrder) { | ||
Order order = | ||
new Order( | ||
commandContext().entityId(), | ||
createOrder.productId(), | ||
createOrder.customerId(), | ||
createOrder.quantity(), | ||
Instant.now().toEpochMilli()); | ||
return effects().updateState(order).thenReply("OK"); | ||
return effects().updateState(order).thenReply(done()); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...java/store/view/joined/CustomerOrder.java → ...tore/order/view/joined/CustomerOrder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
...view/joined/JoinedCustomerOrdersView.java → ...view/joined/JoinedCustomerOrdersView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
package store.view.joined; | ||
package store.order.view.joined; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've decided to keep a separate "view" package, because that's what we want to highlight in this sample. |
||
import akka.javasdk.annotations.Query; | ||
import akka.javasdk.annotations.Consume; | ||
import akka.javasdk.annotations.Table; | ||
import akka.javasdk.annotations.ComponentId; | ||
import akka.javasdk.view.View; | ||
import akka.javasdk.view.TableUpdater; | ||
import store.customer.api.CustomerEntity; | ||
import store.customer.application.CustomerEntity; | ||
import store.customer.domain.CustomerEvent; | ||
import store.order.api.OrderEntity; | ||
import store.order.application.OrderEntity; | ||
import store.order.domain.Order; | ||
import store.product.api.ProductEntity; | ||
import store.product.application.ProductEntity; | ||
import store.product.domain.ProductEvent; | ||
import store.view.model.Customer; | ||
import store.view.model.Product; | ||
import store.order.view.model.Customer; | ||
import store.order.view.model.Product; | ||
|
||
import java.util.List; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
.../main/java/store/view/model/Customer.java → ...java/store/order/view/model/Customer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package store.view.model; | ||
package store.order.view.model; | ||
|
||
import store.customer.domain.Address; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...c/main/java/store/view/model/Product.java → .../java/store/order/view/model/Product.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package store.view.model; | ||
package store.order.view.model; | ||
|
||
import store.product.domain.Money; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...java/store/view/nested/CustomerOrder.java → ...tore/order/view/nested/CustomerOrder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package store.view.nested; | ||
package store.order.view.nested; | ||
|
||
import store.product.domain.Money; | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only part that doesn't read so well to me, with importing the different
CustomerOrders
in different ways — directly, nested class, full package. Could be be better to prefix each of those classes instead:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, fixed