refactor to remove static supports method

This commit is contained in:
Jake Potrebic 2021-03-25 12:31:11 -07:00
parent f8107e0138
commit af775ee613
No known key found for this signature in database
GPG Key ID: 7C58557EC9C421F8
4 changed files with 41 additions and 18 deletions

View File

@ -1,8 +1,17 @@
package io.papermc.hangar.controller.extras.pagination;
import io.papermc.hangar.controller.extras.pagination.Filter.FilterInstance;
import org.jdbi.v3.core.statement.SqlStatement;
import org.springframework.web.context.request.NativeWebRequest;
public interface Filter {
public interface Filter<F extends FilterInstance> {
boolean supports(NativeWebRequest webRequest);
F create(NativeWebRequest webRequest);
interface FilterInstance {
void createSql(StringBuilder sb, SqlStatement<?> q);
}
void createSql(StringBuilder sb, SqlStatement<?> q);
}

View File

@ -1,26 +1,36 @@
package io.papermc.hangar.controller.extras.pagination;
import io.papermc.hangar.controller.extras.pagination.ProjectCategoryFilter.ProjectCategoryFilterInstance;
import io.papermc.hangar.model.common.projects.Category;
import org.jdbi.v3.core.statement.SqlStatement;
import org.springframework.web.context.request.NativeWebRequest;
public class ProjectCategoryFilter implements Filter {
public class ProjectCategoryFilter implements Filter<ProjectCategoryFilterInstance> {
private static final String QUERY_PARAM = "projectCategory";
private final Category category;
public ProjectCategoryFilter(NativeWebRequest webRequest) {
this.category = Category.fromValue(webRequest.getParameterMap().get(QUERY_PARAM)[0]);
@Override
public boolean supports(NativeWebRequest webRequest) {
return webRequest.getParameterMap().containsKey(QUERY_PARAM);
}
@Override
public void createSql(StringBuilder sb, SqlStatement<?> q) {
sb.append(" AND ").append("p.category").append(" = :").append("category");
q.bind("category", category);
public ProjectCategoryFilterInstance create(NativeWebRequest webRequest) {
return new ProjectCategoryFilterInstance(webRequest);
}
public static boolean supports(NativeWebRequest webRequest) {
return webRequest.getParameterMap().containsKey(QUERY_PARAM);
static class ProjectCategoryFilterInstance implements FilterInstance {
private final Category category;
public ProjectCategoryFilterInstance(NativeWebRequest webRequest) {
this.category = Category.fromValue(webRequest.getParameterMap().get(QUERY_PARAM)[0]);
}
@Override
public void createSql(StringBuilder sb, SqlStatement<?> q) {
sb.append(" AND ").append("p.category").append(" = :").append("category");
q.bind("category", category);
}
}
}

View File

@ -1,6 +1,7 @@
package io.papermc.hangar.controller.extras.resolver;
import io.papermc.hangar.controller.extras.pagination.Filter;
import io.papermc.hangar.controller.extras.pagination.Filter.FilterInstance;
import io.papermc.hangar.controller.extras.pagination.ProjectCategoryFilter;
import io.papermc.hangar.model.api.requests.RequestPagination;
import org.springframework.core.MethodParameter;
@ -23,8 +24,11 @@ public class PaginationResolver implements HandlerMethodArgumentResolver {
private final HandlerMethodArgumentResolver delegate;
Map<Predicate<NativeWebRequest>, FilterConstructor> map = Map.of(
ProjectCategoryFilter::supports, ProjectCategoryFilter::new
// Map<Predicate<NativeWebRequest>, FilterConstructor> map = Map.of(
// ProjectCategoryFilter::supports, ProjectCategoryFilter::new
// );
List<Filter<?>> filters = List.of(
new ProjectCategoryFilter()
);
@FunctionalInterface
@ -45,9 +49,9 @@ public class PaginationResolver implements HandlerMethodArgumentResolver {
public Object resolveArgument(@NonNull MethodParameter parameter, ModelAndViewContainer mavContainer, @NonNull NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
Object result = delegate.resolveArgument(parameter, mavContainer, webRequest, binderFactory);
map.forEach((nativeWebRequestPredicate, filterConstructor) -> {
if (nativeWebRequestPredicate.test(webRequest)) {
Filter filter = filterConstructor.create(webRequest);
filters.forEach(f -> {
if (f.supports(webRequest)) {
FilterInstance filterInstance = f.create(webRequest);
// add filter to RequestPagination object
}
});

View File

@ -35,7 +35,7 @@ public class UnlockedVoter implements AccessDecisionVoter<MethodInvocation> {
return ACCESS_DENIED;
}
if (((HangarAuthenticationToken) authentication).getPrincipal().isLocked()) {
throw new HangarApiException(HttpStatus.UNAUTHORIZED, "error.user.locked");
throw new HangarApiException(HttpStatus.UNAUTHORIZED, "error.userLocked");
}
return ACCESS_GRANTED;
}