This repository has been archived by the owner on May 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
TransactionServiceImpl.java
48 lines (40 loc) · 1.93 KB
/
TransactionServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.example.auction.transaction.impl;
import akka.Done;
import akka.stream.javadsl.Flow;
import com.example.auction.item.api.Item;
import com.example.auction.item.api.ItemEvent;
import com.example.auction.item.api.ItemService;
import com.example.auction.transaction.impl.TransactionCommand.*;
import com.example.auction.transaction.api.TransactionService;
import com.lightbend.lagom.javadsl.persistence.PersistentEntityRef;
import com.lightbend.lagom.javadsl.persistence.PersistentEntityRegistry;
import javax.inject.Inject;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class TransactionServiceImpl implements TransactionService {
private final PersistentEntityRegistry registry;
@Inject
public TransactionServiceImpl(PersistentEntityRegistry registry, ItemService itemService) {
this.registry = registry;
registry.register(TransactionEntity.class);
// Subscribe to the events from the item service.
itemService.itemEvents().subscribe().atLeastOnce(Flow.<ItemEvent>create().mapAsync(1, itemEvent -> {
if (itemEvent instanceof ItemEvent.AuctionFinished) {
ItemEvent.AuctionFinished auctionFinished = (ItemEvent.AuctionFinished) itemEvent;
Item item = auctionFinished.getItem();
Transaction transaction = new Transaction(item.getId(), item.getCreator(),
item.getAuctionWinner().get(), item.getPrice(), 0);
return entityRef(auctionFinished.getItemId()).ask(new StartTransaction(transaction));
} else {
return CompletableFuture.completedFuture(Done.getInstance());
}
}));
}
/*@Override
public Topic<TransactionEvent> transactionEvents() {
return null;
}*/
private PersistentEntityRef<TransactionCommand> entityRef(UUID itemId) {
return registry.refFor(TransactionEntity.class, itemId.toString());
}
}