-
Notifications
You must be signed in to change notification settings - Fork 109
Transaction Service - Submit delivery details #94
Changes from 20 commits
eff0e91
fb0a017
69181bd
789aa48
b7476de
d7f3dfd
78707b1
b8effcb
6836d6c
46cf4bb
81c6c0b
7f32f9a
99deb00
2a3e8e9
0ee2fba
c0783b6
4b81f9e
ab2430a
ec88a6d
8ed1425
c3e4dea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,35 @@ | ||
package com.example.auction.transaction.api; | ||
|
||
//import com.example.auction.item.api.Item; | ||
import org.pcollections.PSequence; | ||
import com.example.auction.item.api.ItemData; | ||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import lombok.Value; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
@Value | ||
public final class TransactionInfo { | ||
|
||
//private final Item item; | ||
private final PSequence<TransactionMessage> messages; | ||
private final TransactionStatus status; | ||
private final DeliveryInfo deliveryInfo; | ||
//private final PSequence<TransactionMessage> messages; | ||
private final UUID itemId; | ||
private final UUID creator; | ||
private final UUID winner; | ||
private final ItemData itemData; | ||
private final int itemPrice; | ||
private final int deliveryPrice; | ||
private final PaymentInfo paymentInfo; | ||
private final Optional<DeliveryInfo> deliveryInfo; | ||
private final TransactionInfoStatus status; | ||
//private final PaymentInfo paymentInfo; | ||
|
||
public TransactionInfo(/*Item item, */PSequence<TransactionMessage> messages, TransactionStatus status, DeliveryInfo deliveryInfo, int deliveryPrice, PaymentInfo paymentInfo) { | ||
// this.item = item; | ||
this.messages = messages; | ||
this.status = status; | ||
this.deliveryInfo = deliveryInfo; | ||
@JsonCreator | ||
public TransactionInfo(UUID itemId, UUID creator, UUID winner, ItemData itemData, int itemPrice, int deliveryPrice, Optional<DeliveryInfo> deliveryInfo, TransactionInfoStatus status) { | ||
this.itemId = itemId; | ||
this.creator = creator; | ||
this.winner = winner; | ||
this.itemData = itemData; | ||
this.itemPrice = itemPrice; | ||
this.deliveryPrice = deliveryPrice; | ||
this.paymentInfo = paymentInfo; | ||
this.deliveryInfo = deliveryInfo; | ||
this.status = status; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.example.auction.transaction.impl; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import lombok.Value; | ||
|
||
@Value | ||
public class DeliveryData { | ||
private final String addressLine1; | ||
private final String addressLine2; | ||
private final String city; | ||
private final String state; | ||
private final int postalCode; | ||
private final String country; | ||
|
||
@JsonCreator | ||
public DeliveryData(String addressLine1, String addressLine2, String city, String state, int postalCode, String country) { | ||
this.addressLine1 = addressLine1; | ||
this.addressLine2 = addressLine2; | ||
this.city = city; | ||
this.state = state; | ||
this.postalCode = postalCode; | ||
this.country = country; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
package com.example.auction.transaction.impl; | ||
|
||
import akka.Done; | ||
import com.lightbend.lagom.javadsl.api.transport.Forbidden; | ||
import com.lightbend.lagom.javadsl.api.transport.NotFound; | ||
import com.lightbend.lagom.javadsl.persistence.PersistentEntity; | ||
import com.example.auction.transaction.impl.TransactionCommand.*; | ||
import com.example.auction.transaction.impl.TransactionEvent.*; | ||
|
||
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()) { | ||
|
@@ -19,6 +23,8 @@ public Behavior initialBehavior(Optional<TransactionState> snapshotState) { | |
return notStarted(state); | ||
case NEGOTIATING_DELIVERY: | ||
return negotiatingDelivery(state); | ||
case PAYMENT_SUBMITTED: | ||
return paymentSubmitted(state); | ||
default: | ||
throw new IllegalStateException(); | ||
} | ||
|
@@ -28,25 +34,69 @@ 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())) | ||
); | ||
|
||
addGetTransactionHandler(builder); | ||
return builder.build(); | ||
} | ||
|
||
private Behavior negotiatingDelivery(TransactionState state) { | ||
BehaviorBuilder builder = newBehaviorBuilder(state); | ||
|
||
builder.setReadOnlyCommandHandler(StartTransaction.class, (cmd, ctx) -> | ||
ctx.reply(Done.getInstance()) | ||
); | ||
|
||
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 | ||
throw new Forbidden("Only the auction winner can submit delivery details"); | ||
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 think you'd better use 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. Changing this will break your tests. 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'm not sure if that's the case - I think all commands are processed in a 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. So, can I throw an exception? Using 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. 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 think 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. Since some services use |
||
}); | ||
|
||
builder.setEventHandler(DeliveryDetailsSubmitted.class, evt -> | ||
state().updateDeliveryData(evt.getDeliveryData()) | ||
); | ||
|
||
addGetTransactionHandler(builder); | ||
|
||
return builder.build(); | ||
} | ||
|
||
private Behavior paymentSubmitted(TransactionState state) { | ||
BehaviorBuilder builder = newBehaviorBuilder(state); | ||
// WIP ... | ||
|
||
addGetTransactionHandler(builder); | ||
|
||
return builder.build(); | ||
} | ||
|
||
private void addGetTransactionHandler(BehaviorBuilder builder) { | ||
builder.setReadOnlyCommandHandler(GetTransaction.class, (cmd, ctx) -> { | ||
if(state().getTransaction().isPresent()) { | ||
if (cmd.getUserId().equals(state().getTransaction().get().getCreator()) || | ||
cmd.getUserId().equals(state().getTransaction().get().getWinner())) | ||
ctx.reply(state()); | ||
else | ||
throw new Forbidden("Only the item owner and the auction winner can see transaction details"); | ||
} | ||
else | ||
throw new NotFound("Transaction for item " + entityId() + " not found"); | ||
}); | ||
} | ||
|
||
private UUID entityUUID() { | ||
return UUID.fromString(entityId()); | ||
} | ||
|
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 private representation of the
transaction-api
'sDeliveryInfo
.We have a terrible mix of naming convetions in that sometimes the private copy is
PXyz
, sometimes it's the exact same name with only a package difference, ...In general I hate using
Data
andInfo
for anything other than signifying thatdata
is master andinfo
can be derived fromdata
.I don't think this issue of naming should be solved in this PR but it' be good if we settled and considered creating a convention.
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.
Raised #98