/**
* @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 QueryBuilder<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<T>) => void) {
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
};
});
}
}
|