Add GET /search route

This commit is contained in:
colinmcdonald22 2016-10-21 21:21:01 -04:00
parent 2ff31dab1d
commit b30df61c75
2 changed files with 52 additions and 4 deletions

View File

@ -42,10 +42,7 @@ import net.frozenorb.apiv3.handler.AuthorizationHandler;
import net.frozenorb.apiv3.handler.MetricsHandler; import net.frozenorb.apiv3.handler.MetricsHandler;
import net.frozenorb.apiv3.handler.WebsiteUserSessionHandler; import net.frozenorb.apiv3.handler.WebsiteUserSessionHandler;
import net.frozenorb.apiv3.model.*; import net.frozenorb.apiv3.model.*;
import net.frozenorb.apiv3.route.GETDumpsType; import net.frozenorb.apiv3.route.*;
import net.frozenorb.apiv3.route.GETMetrics;
import net.frozenorb.apiv3.route.GETWhoAmI;
import net.frozenorb.apiv3.route.POSTLogout;
import net.frozenorb.apiv3.route.accessTokens.DELETEAccessTokensId; import net.frozenorb.apiv3.route.accessTokens.DELETEAccessTokensId;
import net.frozenorb.apiv3.route.accessTokens.GETAccessTokens; import net.frozenorb.apiv3.route.accessTokens.GETAccessTokens;
import net.frozenorb.apiv3.route.accessTokens.GETAccessTokensId; import net.frozenorb.apiv3.route.accessTokens.GETAccessTokensId;
@ -412,6 +409,7 @@ public final class APIv3 extends AbstractVerticle {
http.get("/dumps/:dumpType").handler(new GETDumpsType()); http.get("/dumps/:dumpType").handler(new GETDumpsType());
http.get("/metrics").handler(new GETMetrics()); http.get("/metrics").handler(new GETMetrics());
http.get("/search").blockingHandler(new GETSearch());
http.get("/whoami").handler(new GETWhoAmI()); http.get("/whoami").handler(new GETWhoAmI());
http.post("/logout").handler(new POSTLogout()); http.post("/logout").handler(new POSTLogout());

View File

@ -0,0 +1,50 @@
package net.frozenorb.apiv3.route;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.Rank;
import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
public final class GETSearch implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
String type = ctx.request().getParam("type");
String query = ctx.request().getParam("q");
if (type == null) {
ErrorUtils.respondRequiredInput(ctx, "type");
return;
} else if (query == null) {
ErrorUtils.respondRequiredInput(ctx, "q");
return;
}
switch (type.toLowerCase()) {
case "user":
User user = SyncUtils.runBlocking(v -> User.findById(query, v));
if (user == null) {
user = SyncUtils.runBlocking(v -> User.findByLastUsernameLower(query, v));
}
if (user == null) {
user = SyncUtils.runBlocking(v -> User.findByConfirmedEmail(query, v));
}
if (user == null) {
ErrorUtils.respondNotFound(ctx, "User", ctx.request().getParam("userId"));
} else {
APIv3.respondJson(ctx, 200, user);
}
break;
default:
ErrorUtils.respondInvalidInput(ctx, type + " is not a valid search type. Valid types: [ user ]");
break;
}
}
}