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
55
56
57
58
59
60
61
62
63
64
65
66
|
package moe.yuuta.dn42peering.node;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.RowSet;
import io.vertx.sqlclient.templates.SqlTemplate;
import moe.yuuta.dn42peering.database.DatabaseUtils;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class NodeServiceImpl implements INodeService {
private final Pool pool;
private final Vertx vertx;
NodeServiceImpl(@Nonnull Vertx vertx, @Nonnull Pool mySQLPool) {
this.vertx = vertx;
this.pool = mySQLPool;
}
@Nonnull
@Override
public INodeService listNodes(@Nonnull Handler<AsyncResult<List<Node>>> handler) {
SqlTemplate
.forQuery(pool, "SELECT id, public_ip, dn42_ip4, dn42_ip6, dn42_ip6_nonll, asn, " +
"internal_ip, internal_port, name, notice, vpn_type_wg " +
"FROM node")
.mapTo(NodeRowMapper.INSTANCE)
.execute(null)
.compose(nodeRowMappers -> {
final List<Node> nodes = new ArrayList<>(10);
for (Node node : nodeRowMappers)
nodes.add(node);
return Future.succeededFuture(nodes);
})
.onComplete(handler);
return this;
}
@Nonnull
@Override
public INodeService getNode(int id, @Nonnull Handler<AsyncResult<Node>> handler) {
SqlTemplate
.forQuery(pool, "SELECT id, public_ip, asn, " +
"dn42_ip4, dn42_ip6, dn42_ip6_nonll, " +
"internal_ip, internal_port, name, notice, vpn_type_wg " +
"FROM node " +
"WHERE id = #{id}")
.mapTo(NodeRowMapper.INSTANCE)
.execute(Collections.singletonMap("id", id))
.compose(nodeRowMappers -> {
if(nodeRowMappers.iterator().hasNext()) {
return Future.succeededFuture(nodeRowMappers.iterator().next());
} else {
return Future.succeededFuture(null);
}
})
.onComplete(handler);
return this;
}
}
|