/
AuctionControllerAction.java
50 lines (39 loc) · 1.65 KB
/
AuctionControllerAction.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
49
50
package com.example;
import com.example.domain.AuctionCommand;
import com.example.domain.AuctionEntity;
import kalix.javasdk.action.Action;
import kalix.javasdk.annotations.Acl;
import kalix.javasdk.client.ComponentClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.UUID;
@Acl(allow = @Acl.Matcher(principal = Acl.Principal.INTERNET))
@RequestMapping("/auctions")
public class AuctionControllerAction extends Action {
private ComponentClient componentClient;
public AuctionControllerAction(ComponentClient componentClient) {
this.componentClient = componentClient;
}
@PostMapping("/create/{auctionId}/{target}")
public Action.Effect<Response> create(
@PathVariable String auctionId,
@PathVariable int target) {
if (target <= 0)
return effects().error("Target needs to be higher than 0.");
var createCmd = new AuctionCommand.Create(target);
var defCall = componentClient
.forEventSourcedEntity(auctionId)
.call(AuctionEntity::create)
.params(createCmd);
return effects().forward(defCall);
}
@PostMapping("/bid/{auctionId}/{value}")
public Action.Effect<Response> bid(@PathVariable String auctionId, @PathVariable int value) {
if (value <= 0)
return effects().error("Bid value needs to be higher than 0.");
var bid = new AuctionCommand.Bid(UUID.randomUUID().toString(), value);
var defCall = componentClient.forEventSourcedEntity(auctionId).call(AuctionEntity::bid).params(bid);
return effects().forward(defCall);
}
}