All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.afrozaar.wordpress.wpapi.v2.request.SearchRequest Maven / Gradle / Ivy

There is a newer version: 4.8.3
Show newest version
package com.afrozaar.wordpress.wpapi.v2.request;

import static java.lang.String.format;

import com.google.common.collect.ImmutableMap;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class SearchRequest extends Request {

    private final Class clazz;

    public SearchRequest(String uri, Map> params, Class clazz) {
        super(uri, params);
        this.clazz = clazz;
    }

    public Class getClazz() {
        return clazz;
    }

    public static class Builder {
        ImmutableMap.Builder> paramBuilder = new ImmutableMap.Builder<>();
        String uri = Request.POSTS;
        final Class clazz;

        private Builder(Class clazz) {
            this.clazz = clazz;
        }

        public static  Builder aSearchRequest(Class clazz) {
            return new Builder<>(clazz);
        }

        public Builder withContext(String context) {
            return withParam(Request.QP_CONTEXT, context);
        }

        public Builder withOrderBy(String orderBy) {
            return withParam(Request.QP_ORDER_BY, orderBy);
        }

        public Builder withOrderAsc() {
            return withOrder("asc");
        }

        public Builder withOrderDesc() {
            return withOrder("desc");
        }

        private Builder withOrder(String order) {
            return withParam(Request.QP_ORDER, order);
        }

        public Builder withFilter(String key, String... values) {
            return withParam(format("filter[%s]", key), values);
        }

        public Builder withParam(String key, String... values) {
            paramBuilder.put(key, Arrays.asList(values));
            return this;
        }

        public Builder withParams(Map> params) {
            this.paramBuilder.putAll(params);
            return this;
        }

        public Builder withPagination(int pageSize, int pageNumber) {
            withParam("page", String.valueOf(pageNumber));
            withParam("per_page", String.valueOf(pageSize));
            return this;
        }

        public Builder withUri(String uri) {
            this.uri = uri;
            return this;
        }

        public Builder but() {
            return Builder.aSearchRequest(clazz).withParams(paramBuilder.build()).withUri(uri);
        }

        public SearchRequest build() {
            return new SearchRequest<>(uri, paramBuilder.build(), clazz);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy