forked from vlingo/xoom-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cart entity and http resource; test failing
- Loading branch information
1 parent
0d392aa
commit 6772b2c
Showing
14 changed files
with
320 additions
and
174 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
73 changes: 73 additions & 0 deletions
73
vlingo-ecommerce/src/main/java/io/vlingo/examples/ecommerce/Bootstrap.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,73 @@ | ||
package io.vlingo.examples.ecommerce; | ||
|
||
import io.vlingo.actors.Definition; | ||
import io.vlingo.actors.World; | ||
import io.vlingo.examples.ecommerce.infra.CartEventAdapter; | ||
import io.vlingo.examples.ecommerce.infra.CartResource; | ||
import io.vlingo.examples.ecommerce.infra.MockJournalListener; | ||
import io.vlingo.examples.ecommerce.model.CartEntity; | ||
import io.vlingo.examples.ecommerce.model.CartEvents; | ||
import io.vlingo.http.resource.Configuration; | ||
import io.vlingo.http.resource.Resources; | ||
import io.vlingo.http.resource.Server; | ||
import io.vlingo.lattice.model.sourcing.SourcedTypeRegistry; | ||
import io.vlingo.symbio.store.journal.Journal; | ||
import io.vlingo.symbio.store.journal.inmemory.InMemoryJournalActor; | ||
|
||
public class Bootstrap { | ||
private static Bootstrap instance; | ||
private final Server server; | ||
|
||
private Bootstrap() { | ||
World world = World.startWithDefaults("cartservice"); | ||
|
||
MockJournalListener listener = new MockJournalListener(); | ||
Journal<String> journal = world.actorFor(Definition.has(InMemoryJournalActor.class, Definition.parameters(listener)), Journal.class); | ||
journal.registerAdapter(CartEvents.CreatedEvent.class, new CartEventAdapter()); | ||
|
||
SourcedTypeRegistry registry = new SourcedTypeRegistry(world); | ||
registry.register(new SourcedTypeRegistry.Info<>(journal, CartEntity.class, CartEntity.class.getSimpleName())); | ||
|
||
final CartResource cartResource = new CartResource(world); | ||
|
||
final Resources resources = Resources.are(cartResource.routes()); | ||
|
||
this.server = Server.startWith(world.stage(), resources, 8081, Configuration.Sizing.define(), Configuration.Timing.define()); | ||
|
||
Runtime.getRuntime().addShutdownHook(new Thread(() -> { | ||
if (instance != null) { | ||
instance.server.stop(); | ||
|
||
System.out.println("\n"); | ||
System.out.println("======================="); | ||
System.out.println("Stopping frontservice."); | ||
System.out.println("======================="); | ||
pause(); | ||
} | ||
})); | ||
} | ||
|
||
public static final Bootstrap instance() { | ||
if (instance == null) instance = new Bootstrap(); | ||
return instance; | ||
} | ||
|
||
public static void main(final String[] args) throws Exception { | ||
System.out.println("======================="); | ||
System.out.println("service: started."); | ||
System.out.println("======================="); | ||
Bootstrap.instance(); | ||
} | ||
|
||
public void stop() throws InterruptedException { | ||
instance.server.shutDown().wait(1000); | ||
} | ||
|
||
private void pause() { | ||
try { | ||
Thread.sleep(1000L); | ||
} catch (Exception e) { | ||
// ignore | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
vlingo-ecommerce/src/main/java/io/vlingo/examples/ecommerce/infra/CartEventAdapter.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,21 @@ | ||
package io.vlingo.examples.ecommerce.infra; | ||
|
||
import io.vlingo.common.serialization.JsonSerialization; | ||
import io.vlingo.examples.ecommerce.model.CartEvents; | ||
import io.vlingo.symbio.Entry; | ||
import io.vlingo.symbio.EntryAdapter; | ||
import io.vlingo.symbio.Metadata; | ||
|
||
public class CartEventAdapter implements EntryAdapter<CartEvents.CreatedEvent,Entry.TextEntry> { | ||
|
||
@Override | ||
public CartEvents.CreatedEvent fromEntry(final Entry.TextEntry entry) { | ||
return JsonSerialization.deserialized(entry.entryData, CartEvents.CreatedEvent.class); | ||
} | ||
|
||
@Override | ||
public Entry.TextEntry toEntry(final CartEvents.CreatedEvent source) { | ||
final String serialization = JsonSerialization.serialized(source); | ||
return new Entry.TextEntry(CartEvents.CreatedEvent.class, 1, serialization, Metadata.nullMetadata()); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
vlingo-ecommerce/src/main/java/io/vlingo/examples/ecommerce/infra/CartResource.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,99 @@ | ||
package io.vlingo.examples.ecommerce.infra; | ||
|
||
import io.vlingo.actors.*; | ||
import io.vlingo.common.Completes; | ||
import io.vlingo.examples.ecommerce.model.Cart; | ||
import io.vlingo.examples.ecommerce.model.UserId; | ||
import io.vlingo.examples.ecommerce.model.ProductId; | ||
import io.vlingo.examples.ecommerce.model.CartEntity; | ||
import io.vlingo.http.Response; | ||
import io.vlingo.http.resource.Resource; | ||
|
||
import static io.vlingo.common.serialization.JsonSerialization.serialized; | ||
import static io.vlingo.http.Response.Status.*; | ||
import static io.vlingo.http.ResponseHeader.*; | ||
import static io.vlingo.http.resource.ResourceBuilder.*; | ||
|
||
public class CartResource { | ||
public static final String ROOT_URL = "/cart"; | ||
private final AddressFactory addressFactory; | ||
private final Stage stage; | ||
|
||
public CartResource(final World world) { | ||
this.addressFactory = world.addressFactory(); | ||
this.stage = world.stage(); | ||
} | ||
|
||
public Completes<Response> create(final UserId userId) { | ||
// Check if already exists and then return 404? | ||
final Address cartAddress = addressFactory.uniquePrefixedWith("sc-"); | ||
|
||
stage.actorFor(Definition.has(CartEntity.class, | ||
Definition.parameters(cartAddress.idString(), userId)), | ||
Cart.class, | ||
cartAddress); | ||
|
||
return Completes.withSuccess( | ||
Response.of(Created, | ||
headers(of(Location, location(cartAddress.idString()))), | ||
"")); | ||
} | ||
|
||
public Completes<Response> queryCart(String cartId) { | ||
return stage.actorOf(addressFactory.from(cartId), CartEntity.class) | ||
.andThenTo(cart -> Completes.withSuccess(Response.of(Ok, ""))) | ||
.otherwise(noUser -> Response.of(NotFound, location(cartId))); | ||
} | ||
|
||
private String doChangeItem(CartEntity entity, String idOfProduct, CartItemChange change) { | ||
ProductId productId = ProductId.fromId(idOfProduct); | ||
if (change.isAdd()) | ||
entity.addItem(productId); | ||
else | ||
entity.removeItem(productId); | ||
|
||
return serialized(entity.queryCart()); | ||
} | ||
|
||
public Completes<Response> changeCart(String cartId, String productId, CartItemChange change) { | ||
return stage.actorOf(addressFactory.from(cartId), CartEntity.class) | ||
.andThenTo(cart -> Completes.withSuccess(Response.of(Ok, doChangeItem(cart, productId, change)))) | ||
.otherwise(noUser -> Response.of(NotFound, location(cartId))); | ||
} | ||
|
||
|
||
private String location(final String shoppingCartId) { | ||
return ROOT_URL + "/" + shoppingCartId; | ||
} | ||
|
||
|
||
public static class CartItemChange { | ||
|
||
public CartItemChange(String operation) { | ||
this.operation = operation; | ||
} | ||
|
||
public final String operation; | ||
|
||
public boolean isAdd() { | ||
return operation.equals("add"); | ||
} | ||
} | ||
|
||
public Resource routes() { | ||
|
||
return resource("Cart resource fluent api", | ||
post("/cart") | ||
.body(UserId.class) | ||
.handle(this::create), | ||
patch("/cart/{cartId}/{productId}") | ||
.param(String.class) | ||
.param(String.class) | ||
.body(CartItemChange.class) | ||
.handle(this::changeCart), | ||
get("/cart/{cartId}") | ||
.param(String.class) | ||
.handle(this::queryCart)); | ||
} | ||
} | ||
|
32 changes: 32 additions & 0 deletions
32
vlingo-ecommerce/src/main/java/io/vlingo/examples/ecommerce/infra/MockJournalListener.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,32 @@ | ||
package io.vlingo.examples.ecommerce.infra; | ||
|
||
import io.vlingo.symbio.Entry; | ||
import io.vlingo.symbio.State; | ||
import io.vlingo.symbio.store.journal.JournalListener; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public final class MockJournalListener implements JournalListener<String> { | ||
public List<Entry<String>> entries = new ArrayList<>(); | ||
|
||
@Override | ||
public void appended(Entry<String> entry) { | ||
this.entries.add(entry); | ||
} | ||
|
||
@Override | ||
public void appendedWith(Entry<String> entry, State<String> snapshot) { | ||
this.entries.add(entry); | ||
} | ||
|
||
@Override | ||
public void appendedAll(List<Entry<String>> entries) { | ||
this.entries.addAll(entries); | ||
} | ||
|
||
@Override | ||
public void appendedAllWith(List<Entry<String>> entries, State<String> snapshot) { | ||
this.entries.addAll(entries); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...essmanager/choreography/ShoppingCart.java → ...vlingo/examples/ecommerce/model/Cart.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
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
4 changes: 2 additions & 2 deletions
4
...ager/choreography/ShoppingCartEvents.java → .../examples/ecommerce/model/CartEvents.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
Oops, something went wrong.