From 7a1ad105cdeff68b86ba69b275790c859bb61633 Mon Sep 17 00:00:00 2001 From: Topu Date: Thu, 20 Oct 2022 13:54:19 +0300 Subject: [PATCH] PUT requests --- application.yml | 4 +-- src/main/java/net/frozenorb/apiv3/APIv3.java | 35 +++++++------------ .../net/frozenorb/apiv3/util/ErrorUtils.java | 4 +++ .../apiv3/web/route/prefix/PUTPrefixesId.java | 28 ++++++++++++++- .../apiv3/web/route/ranks/PUTRanksId.java | 28 ++++++++++++++- .../route/serverGroups/PUTServerGroupsId.java | 29 +++++++++++++-- .../apiv3/web/route/servers/PUTServersId.java | 30 ++++++++++++++-- 7 files changed, 127 insertions(+), 31 deletions(-) diff --git a/application.yml b/application.yml index dafef5f..339e4eb 100644 --- a/application.yml +++ b/application.yml @@ -1,5 +1,5 @@ -mongoUri: mongodb://Admin:admin@15.160.229.161:27017/HQAPI?authMechanism=SCRAM-SHA-1&authSource=admin -redisUri: redis://15.160.229.161:6379 +mongoUri: mongodb://localhost:27017/HQAPI +redisUri: redis://localhost:6379 network: name: MineHQ Network diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index 25b4b07..c485f68 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -52,10 +52,7 @@ import net.frozenorb.apiv3.web.route.notificationTemplates.GETNotificationTempla import net.frozenorb.apiv3.web.route.notificationTemplates.GETNotificationTemplatesId; import net.frozenorb.apiv3.web.route.notificationTemplates.POSTNotificationTemplates; import net.frozenorb.apiv3.web.route.phoneIntel.GETPhoneInteld; -import net.frozenorb.apiv3.web.route.prefix.DELETEPrefixesId; -import net.frozenorb.apiv3.web.route.prefix.GETPrefixes; -import net.frozenorb.apiv3.web.route.prefix.GETPrefixesId; -import net.frozenorb.apiv3.web.route.prefix.POSTPrefixes; +import net.frozenorb.apiv3.web.route.prefix.*; import net.frozenorb.apiv3.web.route.prefixGrants.DELETEPrefixGrantsId; import net.frozenorb.apiv3.web.route.prefixGrants.GETPrefixGrants; import net.frozenorb.apiv3.web.route.prefixGrants.GETPrefixGrantsId; @@ -65,21 +62,9 @@ import net.frozenorb.apiv3.web.route.punishments.DELETEUsersIdActivePunishment; import net.frozenorb.apiv3.web.route.punishments.GETPunishments; import net.frozenorb.apiv3.web.route.punishments.GETPunishmentsId; import net.frozenorb.apiv3.web.route.punishments.POSTPunishments; -import net.frozenorb.apiv3.web.route.ranks.DELETERanksId; -import net.frozenorb.apiv3.web.route.ranks.GETRanks; -import net.frozenorb.apiv3.web.route.ranks.GETRanksId; -import net.frozenorb.apiv3.web.route.ranks.POSTRanks; -import net.frozenorb.apiv3.web.route.serverGroups.DELETEServerGroupsId; -import net.frozenorb.apiv3.web.route.serverGroups.GETServerGroups; -import net.frozenorb.apiv3.web.route.serverGroups.GETServerGroupsId; -import net.frozenorb.apiv3.web.route.serverGroups.GETServerGroupsPlayerCount; -import net.frozenorb.apiv3.web.route.serverGroups.POSTServerGroups; -import net.frozenorb.apiv3.web.route.servers.DELETEServersId; -import net.frozenorb.apiv3.web.route.servers.GETServers; -import net.frozenorb.apiv3.web.route.servers.GETServersId; -import net.frozenorb.apiv3.web.route.servers.GETServersPlayerCountId; -import net.frozenorb.apiv3.web.route.servers.POSTServers; -import net.frozenorb.apiv3.web.route.servers.POSTServersHeartbeat; +import net.frozenorb.apiv3.web.route.ranks.*; +import net.frozenorb.apiv3.web.route.serverGroups.*; +import net.frozenorb.apiv3.web.route.servers.*; import net.frozenorb.apiv3.web.route.users.*; import net.frozenorb.apiv3.util.SpringUtils; @@ -204,14 +189,14 @@ public final class APIv3 { httpGet(router, "/ranks/:rankId", GETRanksId.class); httpGet(router, "/ranks", GETRanks.class); httpPost(router, "/ranks", POSTRanks.class); - //httpPut(router, "/ranks/:rankId", PUTRanksId.class); + httpPut(router, "/ranks/:rankId", PUTRanksId.class); httpDelete(router, "/ranks/:rankId", DELETERanksId.class); httpGet(router, "/serverGroups/:serverGroupId", GETServerGroupsId.class); httpGet(router, "/serverGroups/:serverGroupId/playerCount", GETServerGroupsPlayerCount.class); httpGet(router, "/serverGroups", GETServerGroups.class); httpPost(router, "/serverGroups", POSTServerGroups.class); - //httpPut(router, "/serverGroups/:serverGroupId", PUTServerGroupsId.class); + httpPut(router, "/serverGroups/:serverGroupId", PUTServerGroupsId.class); httpDelete(router, "/serverGroups/:serverGroupId", DELETEServerGroupsId.class); httpGet(router, "/servers/:serverId", GETServersId.class); @@ -219,13 +204,13 @@ public final class APIv3 { httpGet(router, "/servers", GETServers.class); httpPost(router, "/servers/heartbeat", POSTServersHeartbeat.class); httpPost(router, "/servers", POSTServers.class); - //httpPut(router, "/servers/:serverId", PUTServersId.class); + httpPut(router, "/servers/:serverId", PUTServersId.class); httpDelete(router, "/servers/:serverId", DELETEServersId.class); httpGet(router, "/prefixes/:prefixId", GETPrefixesId.class); httpGet(router, "/prefixes", GETPrefixes.class); httpPost(router, "/prefixes", POSTPrefixes.class); - //httpPut(router, "/prefixes/:prefixId", PUTPrefixesId.class); + httpPut(router, "/prefixes/:prefixId", PUTPrefixesId.class); httpDelete(router, "/prefixes/:prefixId", DELETEPrefixesId.class); httpGet(router, "/prefixes/grants/:grantId", GETPrefixGrantsId.class); @@ -290,6 +275,10 @@ public final class APIv3 { router.get(route).blockingHandler((Handler) SpringUtils.getBean(handler), false); } + private void httpPut(Router router, String route, Class handler) { + router.put(route).blockingHandler((Handler) SpringUtils.getBean(handler), false); + } + private void httpPost(Router router, String route, Class handler) { router.post(route).blockingHandler((Handler) SpringUtils.getBean(handler), false); } diff --git a/src/main/java/net/frozenorb/apiv3/util/ErrorUtils.java b/src/main/java/net/frozenorb/apiv3/util/ErrorUtils.java index 89f746d..f542c5f 100644 --- a/src/main/java/net/frozenorb/apiv3/util/ErrorUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/ErrorUtils.java @@ -18,6 +18,10 @@ public class ErrorUtils { respond(ctx, 404, "Not found: " + itemType + " with id " + id + " cannot be found.", null, null); } + public static void alreadyFound(RoutingContext ctx, String itemType, String id) { + respond(ctx, 404, "Already exists: " + itemType + " with id " + id + " already exists.", null, null); + } + public static void respondInvalidInput(RoutingContext ctx, String message) { respond(ctx, 400, "Invalid input: " + message, null, null); } diff --git a/src/main/java/net/frozenorb/apiv3/web/route/prefix/PUTPrefixesId.java b/src/main/java/net/frozenorb/apiv3/web/route/prefix/PUTPrefixesId.java index 19372b8..9df1344 100644 --- a/src/main/java/net/frozenorb/apiv3/web/route/prefix/PUTPrefixesId.java +++ b/src/main/java/net/frozenorb/apiv3/web/route/prefix/PUTPrefixesId.java @@ -1,7 +1,33 @@ package net.frozenorb.apiv3.web.route.prefix; +import com.google.gson.Gson; +import io.vertx.core.Handler; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.web.RoutingContext; +import net.frozenorb.apiv3.domain.Prefix; +import net.frozenorb.apiv3.domain.Rank; +import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SpringUtils; +import net.frozenorb.apiv3.util.SyncUtils; import org.springframework.stereotype.Component; @Component -public class PUTPrefixesId { +public class PUTPrefixesId implements Handler { + + @Override + public void handle(RoutingContext ctx) { + JsonObject body = ctx.getBodyAsJson(); + Prefix prefix = Prefix.findById(body.getString("id")); + + if (prefix != null) { + ErrorUtils.alreadyFound(ctx, "Prefix", ctx.request().getParam("prefixId")); + return; + } + + prefix = SpringUtils.getBean(Gson.class) + .fromJson(ctx.getBodyAsJson().toString(), Prefix.class); + + SyncUtils.runBlocking(prefix::insert); + } + } diff --git a/src/main/java/net/frozenorb/apiv3/web/route/ranks/PUTRanksId.java b/src/main/java/net/frozenorb/apiv3/web/route/ranks/PUTRanksId.java index 1a7646a..f9f6f3c 100644 --- a/src/main/java/net/frozenorb/apiv3/web/route/ranks/PUTRanksId.java +++ b/src/main/java/net/frozenorb/apiv3/web/route/ranks/PUTRanksId.java @@ -1,7 +1,33 @@ package net.frozenorb.apiv3.web.route.ranks; +import com.google.gson.Gson; +import io.vertx.core.Handler; +import io.vertx.core.http.HttpServerRequest; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.web.RoutingContext; +import net.frozenorb.apiv3.domain.Rank; +import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SpringUtils; +import net.frozenorb.apiv3.util.SyncUtils; import org.springframework.stereotype.Component; @Component -public class PUTRanksId { +public class PUTRanksId implements Handler { + + @Override + public void handle(RoutingContext ctx) { + JsonObject body = ctx.getBodyAsJson(); + Rank rank = Rank.findById(body.getString("id")); + + if (rank != null) { + ErrorUtils.alreadyFound(ctx, "Rank", ctx.request().getParam("rankId")); + return; + } + + rank = SpringUtils.getBean(Gson.class) + .fromJson(ctx.getBodyAsJson().toString(), Rank.class); + + SyncUtils.runBlocking(rank::insert); + } + } diff --git a/src/main/java/net/frozenorb/apiv3/web/route/serverGroups/PUTServerGroupsId.java b/src/main/java/net/frozenorb/apiv3/web/route/serverGroups/PUTServerGroupsId.java index a31ee66..3db85eb 100644 --- a/src/main/java/net/frozenorb/apiv3/web/route/serverGroups/PUTServerGroupsId.java +++ b/src/main/java/net/frozenorb/apiv3/web/route/serverGroups/PUTServerGroupsId.java @@ -1,7 +1,32 @@ package net.frozenorb.apiv3.web.route.serverGroups; +import com.google.gson.Gson; +import io.vertx.core.Handler; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.web.RoutingContext; +import net.frozenorb.apiv3.domain.ServerGroup; +import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SpringUtils; +import net.frozenorb.apiv3.util.SyncUtils; import org.springframework.stereotype.Component; @Component -public class PUTServerGroupsId { -} +public class PUTServerGroupsId implements Handler { + + @Override + public void handle(RoutingContext ctx) { + JsonObject body = ctx.getBodyAsJson(); + ServerGroup serverGroup = ServerGroup.findById(body.getString("id")); + + if (serverGroup != null) { + ErrorUtils.alreadyFound(ctx, "ServerGroup", ctx.request().getParam("serverGroupId")); + return; + } + + serverGroup = SpringUtils.getBean(Gson.class) + .fromJson(ctx.getBodyAsJson().toString(), ServerGroup.class); + + SyncUtils.runBlocking(serverGroup::insert); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/web/route/servers/PUTServersId.java b/src/main/java/net/frozenorb/apiv3/web/route/servers/PUTServersId.java index 4c0f0f8..84fbc3b 100644 --- a/src/main/java/net/frozenorb/apiv3/web/route/servers/PUTServersId.java +++ b/src/main/java/net/frozenorb/apiv3/web/route/servers/PUTServersId.java @@ -1,7 +1,33 @@ package net.frozenorb.apiv3.web.route.servers; +import com.google.gson.Gson; +import io.vertx.core.Handler; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.web.RoutingContext; +import net.frozenorb.apiv3.domain.Prefix; +import net.frozenorb.apiv3.domain.Server; +import net.frozenorb.apiv3.util.ErrorUtils; +import net.frozenorb.apiv3.util.SpringUtils; +import net.frozenorb.apiv3.util.SyncUtils; import org.springframework.stereotype.Component; @Component -public class PUTServersId { -} +public class PUTServersId implements Handler { + + @Override + public void handle(RoutingContext ctx) { + JsonObject body = ctx.getBodyAsJson(); + Server server = Server.findById(body.getString("id")); + + if (server != null) { + ErrorUtils.alreadyFound(ctx, "Server", ctx.request().getParam("serverId")); + return; + } + + server = SpringUtils.getBean(Gson.class) + .fromJson(ctx.getBodyAsJson().toString(), Server.class); + + SyncUtils.runBlocking(server::insert); + } + +} \ No newline at end of file