-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
223 additions
and
115 deletions.
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
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.