Skip to content
This repository has been archived by the owner on May 9, 2019. It is now read-only.

Commit

Permalink
Finish submitDeliveryDetails callback
Browse files Browse the repository at this point in the history
  • Loading branch information
yg-apaza committed Jun 1, 2017
1 parent fb0a017 commit 69181bd
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

// import com.example.auction.item.api.DeliveryOption;

import com.fasterxml.jackson.annotation.JsonCreator;
import lombok.Value;

@Value
public final class DeliveryInfo {

private final String addressLine1;
Expand All @@ -12,6 +16,7 @@ public final class DeliveryInfo {
private final String country;
// private final DeliveryOption selectedDeliveryOption;

@JsonCreator
public DeliveryInfo(String addressLine1, String addressLine2, String city, String state, int postalCode, String country /* DeliveryOption selectedDeliveryOption*/) {
this.addressLine1 = addressLine1;
this.addressLine2 = addressLine2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ default Descriptor descriptor() {
return named("transaction").withCalls(
pathCall("/api/item/:id/transaction", this::submitDeliveryDetails)
).withPathParamSerializer(UUID.class, PathParamSerializers.required("UUID", UUID::fromString, UUID::toString))
.withHeaderFilter(SecurityHeaderFilter.INSTANCE);
.withHeaderFilter(SecurityHeaderFilter.INSTANCE)
.withAutoAcl(true);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ public Transaction(UUID itemId, UUID creator, UUID winner, int itemPrice) {
this.deliveryPrice = 0;
this.deliveryData = Optional.empty();
}

public Transaction withDeliveryData(DeliveryData deliveryData){
return new Transaction(itemId, creator, winner, itemPrice, deliveryPrice, Optional.of(deliveryData));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.example.auction.transaction.impl;

import akka.Done;
import com.example.auction.item.api.ItemEvent;
import com.lightbend.lagom.javadsl.api.transport.Forbidden;
import com.lightbend.lagom.javadsl.persistence.PersistentEntity;
import com.example.auction.transaction.impl.TransactionCommand.*;
import com.example.auction.transaction.impl.TransactionEvent.*;

import java.time.Instant;
import java.util.Optional;
import java.util.UUID;

public class TransactionEntity extends PersistentEntity<TransactionCommand, TransactionEvent, TransactionState> {

@Override
public Behavior initialBehavior(Optional<TransactionState> snapshotState) {
if (!snapshotState.isPresent()) {
Expand All @@ -30,14 +35,14 @@ public Behavior initialBehavior(Optional<TransactionState> snapshotState) {
private Behavior notStarted(TransactionState state) {
BehaviorBuilder builder = newBehaviorBuilder(state);

builder.setCommandHandler(StartTransaction.class, (start, ctx) ->
ctx.thenPersist(new TransactionStarted(entityUUID(), start.getTransaction()), (e) ->
builder.setCommandHandler(StartTransaction.class, (cmd, ctx) ->
ctx.thenPersist(new TransactionStarted(entityUUID(), cmd.getTransaction()), (e) ->
ctx.reply(Done.getInstance())
)
);

builder.setEventHandlerChangingBehavior(TransactionStarted.class, started ->
negotiatingDelivery(TransactionState.start(started.getTransaction()))
builder.setEventHandlerChangingBehavior(TransactionStarted.class, event ->
negotiatingDelivery(TransactionState.start(event.getTransaction()))
);

return builder.build();
Expand All @@ -46,19 +51,24 @@ private Behavior notStarted(TransactionState state) {
private Behavior negotiatingDelivery(TransactionState state) {
BehaviorBuilder builder = newBehaviorBuilder(state);

builder.setReadOnlyCommandHandler(StartTransaction.class, (start, ctx) ->
builder.setReadOnlyCommandHandler(StartTransaction.class, (cmd, ctx) ->
ctx.reply(Done.getInstance())
);

builder.setCommandHandler(SubmitDeliveryDetails.class, (cmd, ctx) ->
ctx.thenPersist(new DeliveryDetailsSubmitted(entityUUID(), cmd.getDeliveryData()), (e) ->
builder.setCommandHandler(SubmitDeliveryDetails.class, (cmd, ctx) -> {
if(cmd.getUserId().equals(state().getTransaction().get().getWinner())) {
return ctx.thenPersist(new DeliveryDetailsSubmitted(entityUUID(), cmd.getDeliveryData()), (e) ->
ctx.reply(Done.getInstance())
)
);
);
}
else {
ctx.commandFailed(new Forbidden("Only the buyer can submit delivery details"));
return ctx.done();
}
});

builder.setEventHandlerChangingBehavior(DeliveryDetailsSubmitted.class, event ->
// WIP
null
builder.setEventHandlerChangingBehavior(DeliveryDetailsSubmitted.class, evt ->
paymentSubmitted(state().updateDeliveryData(evt.getDeliveryData()))
);

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.auction.transaction.impl;

import com.example.auction.transaction.api.DeliveryInfo;

public class TransactionMappers {

public static DeliveryInfo toApi(DeliveryData data) {
return new DeliveryInfo(
data.getAddressLine1(),
data.getAddressLine2(),
data.getCity(),
data.getState(),
data.getPostalCode(),
data.getCountry()
);
}

public static DeliveryData fromApi(DeliveryInfo data) {
return new DeliveryData(
data.getAddressLine1(),
data.getAddressLine2(),
data.getCity(),
data.getState(),
data.getPostalCode(),
data.getCountry()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public Topic<TransactionEvent> transactionEvents() {
@Override
public ServiceCall<DeliveryInfo, Done> submitDeliveryDetails(UUID itemId) {
return authenticated(userId -> deliveryInfo -> {
return null;
SubmitDeliveryDetails submit = new SubmitDeliveryDetails(itemId, TransactionMappers.fromApi(deliveryInfo));
return entityRef(itemId).ask(submit);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
import lombok.Value;

import java.util.Optional;
import java.util.function.Function;

/**
* The transaction state.
*/
@Value
public class TransactionState implements Jsonable {

/**
* The transaction details.
*/
private final Optional<Transaction> transaction;
private final TransactionStatus status;

Expand All @@ -31,4 +29,13 @@ public static TransactionState notStarted() {
public static TransactionState start(Transaction transaction) {
return new TransactionState(Optional.of(transaction), TransactionStatus.NEGOTIATING_DELIVERY);
}

public TransactionState updateDeliveryData(DeliveryData deliveryData) {
return update(i -> i.withDeliveryData(deliveryData), status);
}

private TransactionState update(Function<Transaction, Transaction> updateFunction, TransactionStatus status) {
assert transaction.isPresent();
return new TransactionState(transaction.map(updateFunction), status);
}
}

0 comments on commit 69181bd

Please sign in to comment.