All files / src/Query Query.ts

100% Statements 18/18
50% Branches 1/2
100% Functions 9/9
100% Lines 17/17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90          1x                         1x 39x             85x               84x       39x 39x                     4x 4x         4x 5x                   1x   9x 9x 9x 9x                           4x      
/**
 * @module Query
 */ /** */
 
import { Observable } from 'rxjs/Observable';
import { QueryExpression, QueryResult, QuerySegment } from '.';
import { IContent } from '../Content';
import { IODataParams } from '../ODataApi';
import { BaseRepository } from '../Repository/BaseRepository';
 
/**
 * Represents an instance of a Query expression.
 * Usage example:
 * ```ts
 * const query = new Query(q => q.TypeIs(ContentTypes.Task).And.Equals('DisplayName', 'Test'))
 * console.log(query.toString());   // the content query expression
 * ```
 */
export class Query<T extends IContent = IContent> {
    private readonly _segments: QuerySegment<T>[] = [];
 
    /**
     * Appends a new QuerySegment to the existing Query
     * @param {QuerySegment<T>} newSegment The Segment to be added
     */
    public AddSegment(newSegment: QuerySegment<T> ) {
        this._segments.push(newSegment);
    }
 
    /**
     * @returns {String} The Query expression as a sensenet Content Query
     */
    // tslint:disable-next-line:naming-convention
    public toString(): string {
        return this._segments.map((s) => s.toString()).join('');
    }
 
    constructor(build: (first: QueryExpression<any>) => QuerySegment<T>) {
        const firstExpression = new QueryExpression<T>(this);
        build(firstExpression);
    }
 
    /**
     * Method that executes the Query and creates an OData request
     * @param {BaseRepository} repository The Repository instance
     * @param {string} path The Path for the query
     * @param {ODataParams} odataParams Additional OData parameters (like $select, $expand, etc...)
     * @returns {Observable<QueryResult<TReturns>>} An Observable that will publish the Query result
     */
    public Exec(repository: BaseRepository, path: string, odataParams: IODataParams<T> = {}): Observable<QueryResult<T>> {
        odataParams.query = this.toString();
        return repository.GetODataApi().Fetch<T>({
                path,
                params: odataParams
            })
            .map((q) => {
                return {
                    Result: q.d.results.map((c) => repository.HandleLoadedContent<T>(c)),
                    Count: q.d.__count
                };
            });
    }
}
 
/**
 * Represents a finialized Query instance that has a Repository, path and OData Parameters set up
 */
export class FinializedQuery<T extends IContent = IContent> extends Query<T> {
    constructor(build: (first: QueryExpression<any>) => QuerySegment<T>,
                private readonly _repository: BaseRepository,
                private readonly _path: string,
                private readonly _odataParams: IODataParams<T> = {}) {
        super(build);
    }
 
    /**
     * Executes the Query expression
     * Usage:
     * ```ts
     * const query = new Query(q => q.TypeIs(ContentTypes.Task).And.Equals('DisplayName', 'Test'))
     * query.Exec().subscribe(result=>{
     *  console.log(result);
     * })
     * ```
     */
    public Exec(): Observable<QueryResult<T>> {
        return super.Exec(this._repository, this._path, this._odataParams);
    }
}