blob: fc90bc082912a4829e28ebc8da54cace42243bd2 (
plain)
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
51
52
53
54
|
package market.guess.ui.client;
import java.util.ArrayList;
import java.util.List;
import market.guess.api.AccountContext;
import market.guess.api.Result;
import market.guess.model.ledger.LedgerDTO;
import okhttp3.FormBody;
/**
* Accounts over HTTP. Only our own ledger is ours to read; the server shows other users as name
* and balance, so they appear here as accounts without entries, after our own.
*/
public final class RemoteAccountContext implements AccountContext {
private final ServerClient client;
public RemoteAccountContext(ServerClient client) {
this.client = client;
}
@Override
public Result<List<LedgerDTO>> getAllAccounts() {
var snapshot = client.snapshot();
var own = snapshot.account();
var all = new ArrayList<LedgerDTO>();
if (own != null) {
all.add(own);
}
for (var u : snapshot.users()) {
if (own == null || !u.username().equalsIgnoreCase(own.owner())) {
all.add(new LedgerDTO(u.username(), u.balance(), false, List.of()));
}
}
return Result.ok(all);
}
@Override
public Result<LedgerDTO> getAccount(String owner) {
return getAllAccounts().getData().stream()
.filter(a -> a.owner().equalsIgnoreCase(owner))
.findFirst()
.map(Result::ok)
.orElseGet(() -> Result.error("Account not found"));
}
@Override
public Result<LedgerDTO> deposit(String owner, String amount) {
Result<LedgerDTO> res =
client.post(
LedgerDTO.class, new FormBody.Builder().add("amount", amount).build(), "user", "deposit");
client.pull();
return res;
}
}
|