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

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

There is a newer version: 1.4.3
Show newest version
package fr.ouestfrance.querydsl.postgrest;

import fr.ouestfrance.querydsl.postgrest.model.BulkOptions;
import fr.ouestfrance.querydsl.postgrest.model.BulkResponse;
import fr.ouestfrance.querydsl.postgrest.model.Page;
import fr.ouestfrance.querydsl.postgrest.model.Pageable;
import fr.ouestfrance.querydsl.postgrest.model.exceptions.PostgrestRequestException;

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

/**
 * Repository interface
 *
 * @param  type of return object
 */
public interface Repository {

    /**
     * Search from criteria object
     *
     * @param criteria search criteria
     * @return page result
     */
    default Page search(Object criteria) {
        return search(criteria, Pageable.unPaged());
    }


    /**
     * Count all items
     *
     * @return count result
     */
    default long count() {
        return count(null);
    }

    /**
     * Count from criteria object
     *
     * @param criteria search criteria
     * @return count result
     */
    long count(Object criteria);

    /**
     * Search from criteria object with pagination
     *
     * @param criteria search criteria
     * @param pageable pagination data
     * @return page result
     */
    Page search(Object criteria, Pageable pageable);

    /**
     * Find one object using criteria, method can return one or empty
     *
     * @param criteria search criteria
     * @return Optional result
     * @throws PostgrestRequestException when search criteria result gave more than one item
     */
    Optional findOne(Object criteria);

    /**
     * Get one object using criteria, method should return the response
     *
     * @param criteria search criteria
     * @return Result object
     * @throws PostgrestRequestException no element found, or more than one item
     */
    T getOne(Object criteria);

    /**
     * Post a value
     *
     * @param value to post
     * @return value inserted
     */
    default T post(Object value) {
        BulkResponse post = post(List.of(value));
        if (post == null) {
            return null;
        }
        return post.stream().findFirst().orElse(null);
    }

    /**
     * Post multiple values
     *
     * @param values values to post
     * @return values inserted
     */
    default BulkResponse post(List values) {
        return post(values, new BulkOptions());
    }

    /**
     * Post multiple values with bulkMode
     *
     * @param value   values to post
     * @param options bulk options
     * @return bulk response
     */
    BulkResponse post(List value, BulkOptions options);

    /**
     * Upsert a value
     *
     * @param value to upsert
     * @return inserted or updated value
     */
    default T upsert(Object value) {
        BulkResponse upsert = upsert(List.of(value));
        if (upsert == null) {
            return null;
        }
        return upsert.stream().findFirst().orElse(null);
    }

    /**
     * Upsert multiple values
     *
     * @param values values to upsert
     * @return values inserted or updated
     */
    default BulkResponse upsert(List values) {
        return upsert(values, new BulkOptions());
    }

    /**
     * Upsert multiple values with bulkMode
     *
     * @param values  values to upsert
     * @param options bulk options
     * @return bulk response
     */
    BulkResponse upsert(List values, BulkOptions options);

    /**
     * Update multiple body
     *
     * @param criteria criteria data
     * @param body     to update
     * @return list of patched object
     */
    default BulkResponse patch(Object criteria, Object body) {
        return patch(criteria, body, new BulkOptions());
    }

    /**
     * Update multiple body
     *
     * @param criteria criteria data
     * @param body     to update
     * @param options  bulk options
     * @return list of patched object
     */
    BulkResponse patch(Object criteria, Object body, BulkOptions options);


    /**
     * Delete items using criteria
     *
     * @param criteria criteria to create deletion query
     * @return list of deleted items
     */
    default BulkResponse delete(Object criteria) {
        return delete(criteria, new BulkOptions());
    }

    /**
     * Delete items using criteria
     *
     * @param criteria criteria to create deletion query
     * @param options  bulk options
     * @return list of deleted items
     */
    BulkResponse delete(Object criteria, BulkOptions options);

}