Skip to content

chore: using the same conventions for Views implementation is samples #323

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -173,7 +173,7 @@ public CompletionStage<UserInfo> getUserInfo(String userId) {


@Get("/by-country/{country}")
public CompletionStage<UsersByCountryView.UserList> getUsersByCountry(String country) {
public CompletionStage<UsersByCountryView.UserEntries> getUsersByCountry(String country) {
return
client.forView()
.method(UsersByCountryView::getUserByCountry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
/**
* A View to query users by country.
*/
@ComponentId("view-users-by-newCountry")
@ComponentId("users-by-country")
public class UsersByCountryView extends View {

private static Logger logger = LoggerFactory.getLogger(UsersByCountryView.class);

@Consume.FromEventSourcedEntity(value = UserEntity.class)
public static class UsersByCountry extends TableUpdater<UserView> {
public Effect<UserView> onEvent(UserEvent evt) {
public static class UsersByCountryUpdater extends TableUpdater<UserEntry> {
public Effect<UserEntry> onEvent(UserEvent evt) {
return switch (evt) {
case UserWasCreated created -> {
logger.info("User was created: {}", created);
var currentId = updateContext().eventSubject().orElseThrow();
yield effects().updateRow(new UserView(currentId, created.name(), created.country(), created.email()));
yield effects().updateRow(new UserEntry(currentId, created.name(), created.country(), created.email()));
}
case EmailAssigned emailAssigned -> {
logger.info("User address changed: {}", emailAssigned);
Expand All @@ -41,16 +41,16 @@ public Effect<UserView> onEvent(UserEvent evt) {
}
}

public record UserView(String id, String name, String country, String email) {
public UserView withEmail(String email) {
return new UserView(id, name, country, email);
public record UserEntry(String id, String name, String country, String email) {
public UserEntry withEmail(String email) {
return new UserEntry(id, name, country, email);
}
}

public record UserList(List<UserView> users) { }
public record UserEntries(List<UserEntry> users) { }

@Query("SELECT * AS users FROM users_by_country WHERE country = :country")
public QueryEffect<UserList> getUserByCountry(String country) {
public QueryEffect<UserEntries> getUserByCountry(String country) {
return queryResult();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,35 @@
@ComponentId("counter-by-value-enrichment")
public class CounterByValueViewEnrichment extends View {

public record CounterByValue(String name, int value) {
public record CounterByValueEntry(String name, int value) {
}


public record CounterByValueList(List<CounterByValue> counters) {
public record CounterByValueEntries(List<CounterByValueEntry> counters) {
}

// tag::events-enrichment[]
@Consume.FromEventSourcedEntity(CounterEntity.class)
public static class CounterByValueUpdater extends TableUpdater<CounterByValue> {
public Effect<CounterByValue> onEvent(CounterEvent counterEvent) {
public static class CounterByValueUpdater extends TableUpdater<CounterByValueEntry> {
public Effect<CounterByValueEntry> onEvent(CounterEvent counterEvent) {
var name = updateContext().eventSubject().get();
return switch (counterEvent) {
case ValueIncreased increased -> effects().updateRow(
new CounterByValue(name, increased.updatedValue())); // <1>
new CounterByValueEntry(name, increased.updatedValue())); // <1>
case ValueMultiplied multiplied -> effects().updateRow(
new CounterByValue(name, multiplied.updatedValue())); // <1>
new CounterByValueEntry(name, multiplied.updatedValue())); // <1>
};
}
}
// end::events-enrichment[]

@Query("SELECT * AS counters FROM counter_by_value WHERE value > :value")
public QueryEffect<CounterByValueList> findByCountersByValueGreaterThan(int value) {
public QueryEffect<CounterByValueEntries> findByCountersByValueGreaterThan(int value) {
return queryResult();
}

@Query("SELECT * AS counters FROM counter_by_value")
public QueryEffect<CounterByValueList> findAll() {
public QueryEffect<CounterByValueEntries> findAll() {
return queryResult();
}
// tag::events-enrichment[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import akka.javasdk.http.HttpException;
import akka.javasdk.http.HttpResponses;
import customer.application.CustomersByNameView;
import customer.domain.CustomersList;
import customer.domain.CustomerEntries;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -62,7 +62,7 @@ public CompletionStage<HttpResponse> create(String id, CreateCustomerRequest cre
// end::cross-service-call[]

@Get("/by_name/{name}")
public CompletionStage<CustomersList> findByName(String name) {
public CompletionStage<CustomerEntries> findByName(String name) {
return componentClient.forView().method(CustomersByNameView::findByName).invokeAsync(name);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package customer.application;

import akka.javasdk.annotations.Acl;
import akka.javasdk.annotations.ComponentId;
import akka.javasdk.annotations.Consume;
import akka.javasdk.annotations.Query;
import akka.javasdk.view.TableUpdater;
import akka.javasdk.view.View;
import customer.domain.Customer;
import customer.domain.CustomersList;
import customer.domain.CustomerEntry;
import customer.domain.CustomerEntries;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// tag::view[]

@ComponentId("customers_by_email_view")
@ComponentId("customers-by-email")
public class CustomersByEmailView extends View {
// end::view[]
private static final Logger logger = LoggerFactory.getLogger(CustomersByEmailView.class);
Expand All @@ -24,17 +23,17 @@ public class CustomersByEmailView extends View {
id = "customer_events", // <3>
consumerGroup = "customer-by-email-view" // <4>
)
public static class CustomersByEmail extends TableUpdater<Customer> {
public Effect<Customer> onEvent(CustomerPublicEvent.Created created) {
public static class CustomersByEmailUpdater extends TableUpdater<CustomerEntry> {
public Effect<CustomerEntry> onEvent(CustomerPublicEvent.Created created) {
// end::view[]
logger.info("Received: {}", created);
// tag::view[]
var id = updateContext().eventSubject().get();
return effects().updateRow(
new Customer(id, created.email(), created.name()));
new CustomerEntry(id, created.email(), created.name()));
}

public Effect<Customer> onEvent(CustomerPublicEvent.NameChanged nameChanged) {
public Effect<CustomerEntry> onEvent(CustomerPublicEvent.NameChanged nameChanged) {
// end::view[]
logger.info("Received: {}", nameChanged);
// tag::view[]
Expand All @@ -44,7 +43,7 @@ public Effect<Customer> onEvent(CustomerPublicEvent.NameChanged nameChanged) {
}

@Query("SELECT * AS customers FROM customers_by_email WHERE email = :email")
public QueryEffect<CustomersList> findByEmail(String email) {
public QueryEffect<CustomerEntries> findByEmail(String email) {
return queryResult();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package customer.application;

import akka.javasdk.annotations.Acl;
import akka.javasdk.annotations.Query;
import akka.javasdk.annotations.Consume;
import akka.javasdk.annotations.ComponentId;
import akka.javasdk.view.View;
import akka.javasdk.view.TableUpdater;
import customer.domain.Customer;
import customer.domain.CustomersList;
import customer.domain.CustomerEntry;
import customer.domain.CustomerEntries;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// tag::view[]
@ComponentId("customers_by_name")
@ComponentId("customers-by-name")
public class CustomersByNameView extends View {
// end::view[]
private static final Logger logger = LoggerFactory.getLogger(CustomersByNameView.class);
Expand All @@ -23,19 +22,19 @@ public class CustomersByNameView extends View {
id = "customer_events", // <3>
consumerGroup = "customer-by-name-view" // <4>
)
public static class CustomersByName extends TableUpdater<Customer> {
public static class CustomersByNameUpdater extends TableUpdater<CustomerEntry> {

public Effect<Customer> onEvent( // <5>
CustomerPublicEvent.Created created) {
public Effect<CustomerEntry> onEvent( // <5>
CustomerPublicEvent.Created created) {
// end::view[]
logger.info("Received: {}", created);
// tag::view[]
var id = updateContext().eventSubject().get();
return effects().updateRow(
new Customer(id, created.email(), created.name()));
new CustomerEntry(id, created.email(), created.name()));
}

public Effect<Customer> onEvent(
public Effect<CustomerEntry> onEvent(
CustomerPublicEvent.NameChanged nameChanged) {
// end::view[]
logger.info("Received: {}", nameChanged);
Expand All @@ -46,7 +45,7 @@ public Effect<Customer> onEvent(
}

@Query("SELECT * as customers FROM customers_by_name WHERE name = :name")
public QueryEffect<CustomersList> findByName(String name) {
public QueryEffect<CustomerEntries> findByName(String name) {
return queryResult();
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package customer.domain;

import java.util.Collection;

public record CustomerEntries(Collection<CustomerEntry> customers) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package customer.domain;

public record CustomerEntry(String id, String email, String name) {
public CustomerEntry withName(String newName) {
return new CustomerEntry(id, email, newName);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@


import akka.http.javadsl.model.StatusCodes;
import akka.javasdk.http.HttpClient;
import akka.javasdk.http.StrictResponse;
import akka.util.ByteString;
import customer.api.CustomerRegistryEndpoint.CreateCustomerRequest;
import customer.application.CustomersByNameView;
import customer.domain.Customer;
import customer.domain.CustomerEntry;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -62,7 +61,7 @@ public void create() {
componentClient.forView()
.method(CustomersByNameView::findByName)
.invokeAsync(createRequest.name())
).customers().stream().map(Customer::name);
).customers().stream().map(CustomerEntry::name);

assertThat(foundCustomers).containsExactly(createRequest.name());
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package customer.api;

import customer.domain.Customer;
import customer.domain.CustomerEntry;
import customer.application.CustomerPublicEvent.Created;
import customer.application.CustomersByEmailView;
import customer.application.CustomersByNameView;
Expand Down Expand Up @@ -38,23 +38,23 @@ public void shouldReturnCustomersFromViews() {
.pollInterval(1, TimeUnit.SECONDS)
.untilAsserted(() -> {

Customer customer =
CustomerEntry customer =
await(
componentClient.forView()
.method(CustomersByNameView::findByName)
.invokeAsync(created1.name())
).customers().stream().findFirst().get();

assertThat(customer).isEqualTo(new Customer("b", created1.email(), created1.name()));
assertThat(customer).isEqualTo(new CustomerEntry("b", created1.email(), created1.name()));

Customer customer2 =
CustomerEntry customer2 =
await(
componentClient.forView()
.method(CustomersByEmailView::findByEmail)
.invokeAsync(created2.email())
).customers().stream().findFirst().get();

assertThat(customer2).isEqualTo(new Customer("a", created2.email(), created2.name()));
assertThat(customer2).isEqualTo(new CustomerEntry("a", created2.email(), created2.name()));

}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import akka.javasdk.client.ComponentClient;
import akka.javasdk.http.HttpException;
import akka.javasdk.http.HttpResponses;
import customer.application.CustomerByEmailView;
import customer.application.CustomerByNameView;
import customer.application.CustomersByEmailView;
import customer.application.CustomersByNameView;
import customer.application.CustomerEntity;
import customer.domain.Address;
import customer.domain.Customer;
import customer.domain.CustomersList;
import customer.domain.CustomerEntries;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -76,16 +76,16 @@ public CompletionStage<HttpResponse> changeAddress(String customerId, Address ne
}

@Get("/by-name/{name}")
public CompletionStage<CustomersList> customerByName(String name) {
public CompletionStage<CustomerEntries> customerByName(String name) {
return componentClient.forView()
.method(CustomerByNameView::getCustomers)
.method(CustomersByNameView::getCustomers)
.invokeAsync(name);
}

@Get("/by-email/{email}")
public CompletionStage<CustomersList> customerByEmail(String email) {
public CompletionStage<CustomerEntries> customerByEmail(String email) {
return componentClient.forView()
.method(CustomerByEmailView::getCustomers)
.method(CustomersByEmailView::getCustomers)
.invokeAsync(email);
}
}
Loading
Loading