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

fr.ouestfrance.querydsl.postgrest.PostgrestRpcClient Maven / Gradle / Ivy

The newest version!
package fr.ouestfrance.querydsl.postgrest;

import fr.ouestfrance.querydsl.postgrest.model.Filter;
import fr.ouestfrance.querydsl.postgrest.model.impl.SelectFilter;
import fr.ouestfrance.querydsl.postgrest.services.ext.PostgrestQueryProcessorService;
import fr.ouestfrance.querydsl.postgrest.utils.FilterUtils;
import fr.ouestfrance.querydsl.service.ext.QueryDslProcessorService;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
public class PostgrestRpcClient {

    private static final String RPC = "rpc/";
    private final PostgrestClient client;
    private final QueryDslProcessorService processorService = new PostgrestQueryProcessorService();

    /**
     * Execute a rpc call without body
     *
     * @param rpcName rpc name
     * @param clazz   class of return
     * @param    type of the return object
     * @return response
     */
    public  Optional executeRpc(String rpcName, Class clazz) {
        return executeRpc(rpcName, null, clazz);
    }

    /**
     * Execute a rpc call
     *
     * @param rpcName rpc name
     * @param body    body request to send
     * @param clazz   class of return
     * @param    type of return object
     * @return response
     */
    public  Optional executeRpc(String rpcName, Object body, Class clazz) {
        return executeRpc(rpcName, null, body, clazz);
    }

    /**
     * Execute a rpc call
     *
     * @param rpcName rpc name
     * @param body    body request to send
     * @param clazz   class of return
     * @param    type of return object
     * @return response
     */
    public  Optional executeRpc(String rpcName, Object criteria, Object body, Class clazz) {
        // List filters
        List queryParams = processorService.process(criteria);
        // Extract selection
        getSelects(criteria).ifPresent(queryParams::add);
        return Optional.ofNullable(client.rpc(RPC + rpcName, FilterUtils.toMap(queryParams), body, clazz));
    }

    /**
     * Extract selection on criteria and class
     *
     * @param criteria search criteria
     * @return attributes
     */
    private Optional getSelects(Object criteria) {
        return Optional.of(FilterUtils.getSelectAttributes(criteria))
                .filter(x -> !x.isEmpty())
                .map(SelectFilter::only);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy