/v1/filters entry point

Fetches the list of Filters that can be applied in a query
This commit is contained in:
Risto Lahtela 2020-03-27 10:42:12 +02:00
parent a14d7d4769
commit 8f5db846af
3 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,78 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.resolver.json;
import com.djrapitops.plan.delivery.web.resolver.MimeType;
import com.djrapitops.plan.delivery.web.resolver.Resolver;
import com.djrapitops.plan.delivery.web.resolver.Response;
import com.djrapitops.plan.delivery.web.resolver.request.Request;
import com.djrapitops.plan.delivery.web.resolver.request.WebUser;
import com.djrapitops.plan.storage.database.queries.filter.Filter;
import com.djrapitops.plan.storage.database.queries.filter.QueryFilters;
import com.djrapitops.plan.utilities.java.Maps;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Singleton
public class FiltersJSONResolver implements Resolver {
private QueryFilters filters;
@Inject
public FiltersJSONResolver(
QueryFilters filters
) {
this.filters = filters;
}
@Override
public boolean canAccess(Request request) {
WebUser user = request.getUser().orElse(new WebUser(""));
return user.hasPermission("page.players");
}
@Override
public Optional<Response> resolve(Request request) {
return Optional.of(getResponse());
}
private Response getResponse() {
return Response.builder()
.setMimeType(MimeType.JSON)
.setJSONContent(Maps.builder(String.class, Object.class)
.put("filters", serializeFilters())
.build())
.build();
}
private List<Map<String, Object>> serializeFilters() {
List<Map<String, Object>> filterList = new ArrayList<>();
for (Map.Entry<String, Filter> entry : filters.getFilters().entrySet()) {
filterList.add(Maps.builder(String.class, Object.class)
.put("kind", entry.getKey())
.put("options", entry.getValue().getOptions())
.put("expectedParameters", entry.getValue().getExpectedParameters())
.build());
}
return filterList;
}
}

View File

@ -53,6 +53,7 @@ public class RootJSONResolver {
PlayerJSONResolver playerJSONResolver,
NetworkJSONResolver networkJSONResolver,
FiltersJSONResolver filtersJSONResolver,
QueryJSONResolver queryJSONResolver
) {
this.identifiers = identifiers;
@ -71,6 +72,7 @@ public class RootJSONResolver {
.add("performanceOverview", forJSON(DataID.PERFORMANCE_OVERVIEW, performanceJSONCreator))
.add("player", playerJSONResolver)
.add("network", networkJSONResolver.getResolver())
.add("filters", filtersJSONResolver)
.add("query", queryJSONResolver)
.build();
}

View File

@ -84,4 +84,8 @@ public class QueryFilters {
return current == null ? null : current.notApplied(filter);
}
}
public Map<String, Filter> getFilters() {
return filters;
}
}