From c361554916208c9557b0d5685e35888023097bd9 Mon Sep 17 00:00:00 2001 From: Sammers21 Date: Tue, 19 Dec 2023 12:46:26 +0300 Subject: [PATCH] releaseb return char on error --- src/io/github/sammers/pla/http/Http.java | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/io/github/sammers/pla/http/Http.java b/src/io/github/sammers/pla/http/Http.java index 512c1404..c29cad94 100644 --- a/src/io/github/sammers/pla/http/Http.java +++ b/src/io/github/sammers/pla/http/Http.java @@ -126,14 +126,7 @@ public void start() { VTHREAD_EXECUTOR.execute(() -> { String realm = ctx.pathParam("realm"); String name = ctx.pathParam("name"); - Optional charWithName = ladder.wowChar(ladder.realms.get().nameToSlug(realm), name); - Optional charWithSlug = ladder.wowChar(realm, name); - Optional res = Stream.of(charWithName, charWithSlug).filter(Optional::isPresent).findFirst().map(Optional::get); - if (res.isEmpty() || res.get().hidden()) { - ctx.response().setStatusCode(404).end(new JsonObject().put("error", "Character not found").encode()); - } else { - ctx.response().end(wowCharToJson(res.get()).encode()); - } + nameRealmLookupResponse(ctx, realm, name); }); }); router.get("/api/:region/:realm/:name/update").handler(ctx -> { @@ -149,7 +142,7 @@ public void start() { } else { ctx.response().end(wowCharToJson(wowAPICharacter.get()).encode()); } - }); + }, err -> nameRealmLookupResponse(ctx, realm, name)); }); }); router.get("/:region/ladder/:bracket").handler(ctx -> ctx.response().sendFile("index.html")); @@ -160,6 +153,17 @@ public void start() { vertx.createHttpServer().requestHandler(router).listen(9000); } + private void nameRealmLookupResponse(RoutingContext ctx, String realm, String name) { + Optional charWithName = ladder.wowChar(ladder.realms.get().nameToSlug(realm), name); + Optional charWithSlug = ladder.wowChar(realm, name); + Optional res = Stream.of(charWithName, charWithSlug).filter(Optional::isPresent).findFirst().map(Optional::get); + if (res.isEmpty() || res.get().hidden()) { + ctx.response().setStatusCode(404).end(new JsonObject().put("error", "Character not found").encode()); + } else { + ctx.response().end(wowCharToJson(res.get()).encode()); + } + } + private JsonObject wowCharToJson(WowAPICharacter character) { Set alts = characterCache.altsFor(character); JsonObject res = character.toJson();