예제 #1
0
        /// <summary>
        ///     Filters this route's dataset by a parameter value
        /// </summary>
        /// <param name="property">An expression getting the property to filter for</param>
        /// <param name="retriever">A function that will get the value of the parameter for a given request</param>
        /// <typeparam name="T">The type of the parameter to filter with</typeparam>
        /// <returns>This <see cref="RestModelOptionsBuilder{TModel, TUser}" /> object, for chaining</returns>
        public RestModelOptionsBuilder <TModel, TUser> FilterByParameterEqual <T>(Expression <Func <TModel, T> > property, ParameterRetriever retriever)
        {
            PropertyInfo Info = RestModelOptionsBuilder <TModel, TUser> .ExtractProperty(property);

            ParameterExpression ModelParameter     = Expression.Parameter(typeof(TModel));
            MemberExpression    PropertyExpression = Expression.Property(ModelParameter, Info);
            Type PropertyType = typeof(T);

            this.Filter(
                (c, d) => {
                string?ParamValue = retriever.GetValue(c.Request);
                if (ParamValue == null)
                {
                    throw new ConditionFailedException("Failed to parse request parameter");
                }
                T Parsed = (T)ParameterResolver.ParseParameter(ParamValue, PropertyType);

                // ModelParameter.Property == ParamValue
                Expression ComparisonExpression = Expression.Equal(PropertyExpression, Expression.Constant(Parsed));

                // and create the lambda
                Expression <Func <TModel, bool> > FilterExpression =
                    Expression.Lambda <Func <TModel, bool> >(ComparisonExpression, ModelParameter);
                return(d.Where(FilterExpression));
            });
            return(this);
        }
 /// <summary>
 ///     Sets a value obtained from a query parameter on parsed models before the operation occurs. The name of the route value is inferred from camelCasing the property name.
 /// </summary>
 /// <param name="property">The property to set the value on</param>
 /// <returns>This <see cref="RestModelOptionsBuilder{TModel, TUser}" /> object, for chaining</returns>
 public RestModelOptionsBuilder <TModel, TUser> SetValueQuery(
     Expression <Func <TModel, object?> > property) =>
 this.SetValueQuery(RestModelOptionsBuilder <TModel, TUser> .ExtractProperty(property));
 /// <summary>
 ///     Sets a value on parsed models before the operation occurs.
 /// </summary>
 /// <param name="property">The property to set the value on</param>
 /// <param name="retriever">A parameter retriever to use to get the value for a request</param>
 /// <returns>This <see cref="RestModelOptionsBuilder{TModel, TUser}" /> object, for chaining</returns>
 public RestModelOptionsBuilder <TModel, TUser> SetValue(
     Expression <Func <TModel, object?> > property,
     ParameterRetriever retriever) =>
 this.SetValue(RestModelOptionsBuilder <TModel, TUser> .ExtractProperty(property), retriever);
 /// <summary>
 ///     Sets a value obtained from a route value on parsed models before the operation occurs.
 /// </summary>
 /// <param name="property">The property to set the value on</param>
 /// <param name="parameterName">The name of the route value whose value <paramref name="property" /> will be set to</param>
 /// <returns>This <see cref="RestModelOptionsBuilder{TModel, TUser}" /> object, for chaining</returns>
 public RestModelOptionsBuilder <TModel, TUser> SetValueRoute(
     Expression <Func <TModel, object?> > property,
     string parameterName) =>
 this.SetValueRoute(RestModelOptionsBuilder <TModel, TUser> .ExtractProperty(property), parameterName);
 /// <summary>
 ///     Sets a value on parsed models before the operation occurs.
 /// </summary>
 /// <param name="property">The property to set the value on</param>
 /// <param name="valueGetter">A handler to use to get the value when requested</param>
 /// <returns>This <see cref="RestModelOptionsBuilder{TModel, TUser}" /> object, for chaining</returns>
 public RestModelOptionsBuilder <TModel, TUser> SetValueAsync(
     Expression <Func <TModel, object> > property,
     Func <IApiContext <TModel, TUser>, Task <object?> > valueGetter) =>
 this.SetValueAsync(RestModelOptionsBuilder <TModel, TUser> .ExtractProperty(property), valueGetter);