/// <summary> /// If the check returns <c>true</c>, <see cref="ValidationException"/> /// is emitted as an error with the given status code. /// Otherwise the given value is emitted. /// </summary> /// <typeparam name="TSource">The type of the value.</typeparam> /// <param name="observable">The parent observable.</param> /// <param name="invalidCheck">The invalidCheck function.</param> /// <param name="statusCode">The status code of the error.</param> /// <param name="error">The error to be used on a failed check.</param> /// <returns>An instance of <see cref="IProviderObservable{TSource}"/>.</returns> public static IProviderObservable <TSource> InvalidWhen <TSource>( this IProviderObservable <TSource> observable, Func <TSource, bool> invalidCheck, int statusCode, object error) => observable.InvalidWhen(invalidCheck, statusCode, s => error);
/// <summary> /// If the check returns <c>true</c>, <see cref="ValidationException"/> /// is emitted as an error with the status code 403 (Forbidden). /// Otherwise the given value is emitted. /// </summary> /// <typeparam name="TSource">The type of the value.</typeparam> /// <param name="observable">The parent observable.</param> /// <param name="invalidCheck">The invalidCheck function.</param> /// <param name="error">The error to be used on a failed check.</param> /// <returns>An instance of <see cref="IProviderObservable{TSource}"/>.</returns> public static IProviderObservable <TSource> ForbiddenWhen <TSource>( this IProviderObservable <TSource> observable, Func <bool> invalidCheck, object error) => observable.InvalidWhen( s => invalidCheck(), StatusCodes.Status403Forbidden, s => error);
/// <summary> /// Emits <see cref="NoContentResult"/> on receiving a value. Does not contain the value. /// <para> /// Catches <see cref="ValidationException"/> and converts it to /// an appropriate <see cref="IActionResult"/>. /// </para> /// </summary> /// <typeparam name="TSource">The type of the value.</typeparam> /// <param name="observable">The parent observable.</param> /// <returns>An instance of <see cref="IProviderObservable{IActionResult}"/>.</returns> public static IProviderObservable <IActionResult> ToNoContentResult <TSource>( this IProviderObservable <TSource> observable) => observable.ToActionResult(s => new NoContentResult());
public MapObservable(Func <TSource, TTarget> mapping, IProviderObservable <TSource> observable) : base(observable) { Check.IsNull(mapping, nameof(mapping)); this.mapping = mapping; }
/// <summary> /// If the check returns <c>true</c>, <see cref="ValidationException"/> /// is emitted as an error with the status code 403 (Forbidden). /// Otherwise the given value is emitted. /// </summary> /// <typeparam name="TSource">The type of the value.</typeparam> /// <param name="observable">The parent observable.</param> /// <param name="invalidCheck">The invalidCheck function.</param> /// <param name="errorFactory">The error factory method.</param> /// <returns>An instance of <see cref="IProviderObservable{TSource}"/>.</returns> public static IProviderObservable <TSource> ForbiddenWhen <TSource>( this IProviderObservable <TSource> observable, Func <bool> invalidCheck, Func <TSource, object> errorFactory = null) => observable.InvalidWhen( s => invalidCheck(), StatusCodes.Status403Forbidden, errorFactory);
/// <summary> /// Performs a subsequent ordering of the elements in a sequence /// in descending order by using a specified comparer. /// </summary> /// <typeparam name="TSource">The type of the value.</typeparam> /// <typeparam name="TKey">The type of the key.</typeparam> /// <param name="observable">The parent observable.</param> /// <param name="keySelector">A function to extract a key from an element.</param> /// <param name="comparer"> /// An <see cref="T:System.Collections.Generic.IComparer`1"></see> to compare keys. /// </param> /// <returns>An instance of <see cref="IProviderObservable{IOrderedQueryable}"/>.</returns> public static IProviderObservable <IOrderedQueryable <TSource> > ThenByDescending <TSource, TKey>( this IProviderObservable <IOrderedQueryable <TSource> > observable, Expression <Func <TSource, TKey> > keySelector, IComparer <TKey> comparer) => observable.Map(s => s.ThenByDescending(keySelector, comparer));
/// <summary> /// Map the received value to the desired output. /// </summary> /// <typeparam name="TSource">The type of the value.</typeparam> /// <typeparam name="TTarget">The type of the output.</typeparam> /// <param name="observable">The parent observable.</param> /// <param name="mapping">The mapping function.</param> /// <returns>An instance of <see cref="IProviderObservable{TSource}"/>.</returns> public static IProviderObservable <TTarget> Map <TSource, TTarget>( this IProviderObservable <TSource> observable, Func <TSource, TTarget> mapping) => new MapObservable <TSource, TTarget>(mapping, observable);
/// <summary> /// Apply filter logic to the received <see cref="IQueryable{T}"/>. /// <para> /// Matches the query parameters with the keys of the given filter dictionary. /// Implement <see cref="IFilterByClientRequestInterpreter"/> for custom behavior. /// </para> /// <para>Requires <see cref="HttpContextProviderAttribute"/> to be set.</para> /// </summary> /// <typeparam name="TSource">The type of the value.</typeparam> /// <param name="observable">The parent observable.</param> /// <param name="filterDictionary">A dictionary of available filters.</param> /// <returns>An instance of <see cref="IProviderObservable{TSource}"/>.</returns> public static IProviderObservable <IQueryable <TSource> > ApplyFilterByClientRequest <TSource>( this IProviderObservable <IQueryable <TSource> > observable, IDictionary <string, IFilterExpressionProvider <TSource> > filterDictionary) => new ApplyFilterByClientRequestObservable <TSource>(filterDictionary, observable);
/// <summary> /// Catch an exception emitted from the previous observables or operators /// and return a new observable. This will only catch the exception if it is /// an instance of the declared exception type. /// </summary> /// <typeparam name="TSource">The type of the value.</typeparam> /// <typeparam name="TException">The exception type.</typeparam> /// <param name="observable">The parent observable.</param> /// <param name="handler">The function to handle the exception.</param> /// <returns>An instance of <see cref="IProviderObservable{TSource}"/>.</returns> public static IProviderObservable <TSource> Catch <TSource, TException>( this IProviderObservable <TSource> observable, Func <TException, IProviderObservable <TSource> > handler) where TException : Exception => new CatchObservable <TSource, TException>(handler, observable);
/// <summary> /// Emits the only element of a sequence, and throws an exception /// if there is not exactly one element in the sequence. /// </summary> /// <typeparam name="TSource">The type of the value.</typeparam> /// <param name="observable">The parent observable.</param> /// <returns>An instance of <see cref="IProviderObservable{TSource}"/>.</returns> public static IProviderObservable <TSource> SingleAsync <TSource>( this IProviderObservable <IQueryable <TSource> > observable) => observable.MapAsync(async s => await s.SingleAsync());
/// <summary> /// Emits the only element of a sequence that satisfies a specified /// condition, and throws an exception if more than one such element exists. /// </summary> /// <typeparam name="TSource">The type of the value.</typeparam> /// <param name="observable">The parent observable.</param> /// <param name="predicate">A function to test each element for a condition.</param> /// <returns>An instance of <see cref="IProviderObservable{TSource}"/>.</returns> public static IProviderObservable <TSource> SingleAsync <TSource>( this IProviderObservable <IQueryable <TSource> > observable, Expression <Func <TSource, bool> > predicate) => observable.MapAsync(async s => await s.SingleAsync(predicate));
/// <summary> /// Catch an exception emitted from the previous observables or operators /// and perform an action with it. /// </summary> /// <typeparam name="TSource">The type of the value.</typeparam> /// <param name="observable">The parent observable.</param> /// <param name="action">The action to be performed.</param> /// <returns>An instance of <see cref="IProviderObservable{TSource}"/>.</returns> public static IProviderObservable <TSource> Catch <TSource>( this IProviderObservable <TSource> observable, Action <Exception> action) => observable.Catch <TSource, Exception>(action);
/// <summary> /// Catch an exception emitted from the previous observables or operators /// and return a new observable. /// </summary> /// <typeparam name="TSource">The type of the value.</typeparam> /// <param name="observable">The parent observable.</param> /// <param name="handler">The function to handle the exception.</param> /// <returns>An instance of <see cref="IProviderObservable{TSource}"/>.</returns> public static IProviderObservable <TSource> Catch <TSource>( this IProviderObservable <TSource> observable, Func <Exception, IProviderObservable <TSource> > handler) => observable.Catch <TSource, Exception>(handler);
/// <summary> /// Perform an action with the <see cref="DbContext"/>. /// </summary> /// <typeparam name="TSource">The type of the value.</typeparam> /// <param name="observable">The parent observable.</param> /// <param name="action">The action to be performed.</param> /// <returns>An instance of <see cref="IProviderObservable{TSource}"/>.</returns> public static IProviderObservable <TSource> WithDbContext <TSource>( this IProviderObservable <TSource> observable, Action <TSource, DbContext> action) where TSource : class => new WithDbContextObservable <TSource>(action, observable);
/// <summary> /// Perform an async action with the <see cref="DbContext"/>. /// </summary> /// <typeparam name="TSource">The type of the value.</typeparam> /// <param name="observable">The parent observable.</param> /// <param name="action">The action to be performed.</param> /// <returns>An instance of <see cref="IProviderObservable{TSource}"/>.</returns> public static IProviderObservable <TSource> WithDbContextAsync <TSource>( this IProviderObservable <TSource> observable, Func <TSource, DbContext, Task> action) where TSource : class => new WithDbContextAsyncObservable <TSource>(action, observable);
/// <summary> /// Cache the received value in <see cref="IDistributedCache"/> with the given key /// and the defined distributed cache options factory function. /// If an entry with the given key is found in the cache, it will be emitted /// and the previous chain is skipped. /// </summary> /// <typeparam name="TSource">The type of the value.</typeparam> /// <param name="observable">The parent observable.</param> /// <param name="key">The cache key.</param> /// <param name="optionsFactory">The factory function for the cache options.</param> /// <returns>An instance of <see cref="IProviderObservable{TSource}"/>.</returns> public static IProviderObservable <TSource> CacheInDistributedCache <TSource>( this IProviderObservable <TSource> observable, string key, Func <TSource, DistributedCacheEntryOptions> optionsFactory) => new CacheInDistributedCacheObservable <TSource>(key, optionsFactory, observable);