diff options
Diffstat (limited to 'central/src/main/java/moe/yuuta/dn42peering/node/NodeServiceImpl.java')
-rw-r--r-- | central/src/main/java/moe/yuuta/dn42peering/node/NodeServiceImpl.java | 92 |
1 files changed, 88 insertions, 4 deletions
diff --git a/central/src/main/java/moe/yuuta/dn42peering/node/NodeServiceImpl.java b/central/src/main/java/moe/yuuta/dn42peering/node/NodeServiceImpl.java index a2535d4..bc7e5bf 100644 --- a/central/src/main/java/moe/yuuta/dn42peering/node/NodeServiceImpl.java +++ b/central/src/main/java/moe/yuuta/dn42peering/node/NodeServiceImpl.java @@ -4,15 +4,15 @@ import io.vertx.core.AsyncResult; import io.vertx.core.Future; import io.vertx.core.Handler; import io.vertx.core.Vertx; +import io.vertx.mysqlclient.MySQLException; import io.vertx.sqlclient.Pool; import io.vertx.sqlclient.RowSet; +import io.vertx.sqlclient.SqlResult; 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; +import java.util.*; class NodeServiceImpl implements INodeService { private final Pool pool; @@ -54,7 +54,7 @@ class NodeServiceImpl implements INodeService { .mapTo(NodeRowMapper.INSTANCE) .execute(Collections.singletonMap("id", id)) .compose(nodeRowMappers -> { - if(nodeRowMappers.iterator().hasNext()) { + if (nodeRowMappers.iterator().hasNext()) { return Future.succeededFuture(nodeRowMappers.iterator().next()); } else { return Future.succeededFuture(null); @@ -63,4 +63,88 @@ class NodeServiceImpl implements INodeService { .onComplete(handler); return this; } + + @Nonnull + @Override + public INodeService addNew(@Nonnull Node node, + @Nonnull Handler<AsyncResult<Long>> handler) { + node.setId(0); + Future.<Long>future(f -> { + Future.<RowSet<Node>>future(f1 -> SqlTemplate + .forUpdate(pool, "INSERT INTO node (id, asn, name, notice, " + + "public_ip, " + + "dn42_ip4, dn42_ip6, dn42_ip6_nonll," + + "internal_ip, internal_port," + + "vpn_type_wg) " + + "VALUES (#{id}, #{asn}, #{name}, #{notice}, " + + "#{public_ip}, " + + "#{dn42_ip4}, #{dn42_ip6}, #{dn42_ip6_nonll}, " + + "#{internal_ip}, #{internal_port}, " + + "#{vpn_type_wg}" + + ")") + .mapFrom(NodeParametersMapper.INSTANCE) + .mapTo(NodeRowMapper.INSTANCE) + .execute(node, f1)) + .compose(rows -> Future.succeededFuture(rows.property(DatabaseUtils.LAST_INSERTED_ID))) + .onFailure(err -> { + if (err instanceof MySQLException) { + if (((MySQLException) err).getErrorCode() == 1062 /* Duplicate */) { + f.fail(new DuplicateNodeException()); + return; + } + f.fail(err); + } + }) + .onSuccess(f::complete); + }).onComplete(handler); + return this; + } + + @Nonnull + @Override + public INodeService updateTo(@Nonnull Node node, @Nonnull Handler<AsyncResult<Long>> handler) { + Future.<Long>future(f -> { + Future.<RowSet<Node>>future(f1 -> SqlTemplate + .forUpdate(pool, "UPDATE node SET " + + "asn = #{asn}," + + "name = #{name}," + + "notice = #{notice}," + + "public_ip = #{public_ip}," + + "dn42_ip4 = #{dn42_ip4}," + + "dn42_ip6 = #{dn42_ip6}," + + "dn42_ip6_nonll = #{dn42_ip6_nonll}," + + "internal_ip = #{internal_ip}," + + "internal_port = #{internal_port}," + + "vpn_type_wg = #{vpn_type_wg} " + + "WHERE id = #{id}") + .mapFrom(NodeParametersMapper.INSTANCE) + .mapTo(NodeRowMapper.INSTANCE) + .execute(node, f1)) + .compose(rows -> Future.succeededFuture(rows.property(DatabaseUtils.LAST_INSERTED_ID))) + .onFailure(err -> { + if (err instanceof MySQLException) { + if (((MySQLException) err).getErrorCode() == 1062 /* Duplicate */) { + f.fail(new DuplicateNodeException()); + return; + } + f.fail(err); + } + }) + .onSuccess(f::complete); + }).onComplete(handler); + return this; + } + + @Nonnull + @Override + public INodeService delete(int id, @Nonnull Handler<AsyncResult<Void>> handler) { + final Map<String, Object> params = new HashMap<>(2); + params.put("id", id); + Future.<SqlResult<Void>>future(f -> SqlTemplate + .forUpdate(pool, "DELETE FROM node WHERE id = #{id}") + .execute(params, f)) + .<Void>compose(voidSqlResult -> Future.succeededFuture(null)) + .onComplete(handler); + return this; + } } |