/// <summary> /// Method that extends IQueryable<T> allowing to search query with request properties /// </summary> /// <typeparam name="TSource">Generic type of the entity</typeparam> /// <param name="source">Self IQueryable<T> instance</param> /// <param name="request">Self IWrapRequest<T> instance</param> /// <returns>Returns IQueryable instance with with the configuration for search</returns> public static IQueryable <TSource> Search <TSource>( this IQueryable <TSource> source, IWrapRequest <TSource> request ) where TSource : class { var search = request.Search(); var query = search.Query; var queryStrict = search.Strict; var queryPhrase = search.Phrase; if (string.IsNullOrWhiteSpace(query)) { return(source); } var queryTokens = TermsHelper.GetSearchTerms(query, queryPhrase); query = string.Join("+", queryTokens.ToArray()); if (queryTokens.Count == 0) { return(source); } List <string> searchableProperties = new List <string>(); searchableProperties = typeof(TSource).GetProperties().Where(x => !request.IsPropertySuppressed(x.Name) && !TypesHelper.TypeIsComplex(x.PropertyType) ).Select(x => x.Name).ToList(); var criteriaExp = LambdasHelper.GenerateSearchCriteriaExpression <TSource>(searchableProperties, queryTokens, queryStrict); return(source.Where(criteriaExp)); }
/// <summary> /// Mathod that extends IWrapRequest<T> allowing use patch /// </summary> /// <typeparam name="TModel">Generic type of the entity</typeparam> /// <param name="request">Self IWrapRequest<T> instance</param> /// <param name="model">Entity were data from request will be patched</param> /// <returns>Entity updated</returns> public static TModel Patch <TModel>( this IWrapRequest <TModel> request, TModel model ) where TModel : class { var properties = typeof(TModel).GetProperties().Where(x => request.SuppliedProperties().Any(y => y.ToLower().Equals(x.Name.ToLower()))).ToList(); properties = properties.Where(p => !request.IsPropertySuppressed(p.Name)).ToList(); properties = properties.Where(p => !request.KeyProperties().Any(x => x.ToLower().Equals(p.Name.ToLower()))).ToList(); properties.ForEach(property => property.SetValue(model, property.GetValue(request.Model))); request.SetModelOnRequest(model, properties); return(model); }
/// <summary> /// Mathod that extends IWrapRequest<T> allowing use post /// </summary> /// <typeparam name="TModel">Generic type of the entity</typeparam> /// <param name="request">Self IWrapRequest<T> instance</param> /// <returns>New entity</returns> public static TModel Post <TModel>( this IWrapRequest <TModel> request ) where TModel : class { var model = Activator.CreateInstance <TModel>(); var properties = typeof(TModel).GetProperties().ToList(); properties = properties.Where(p => !request.IsPropertySuppressed(p.Name)).ToList(); properties = properties.Where(p => !request.KeyProperties().Any(x => x.ToLower().Equals(p.Name.ToLower()))).ToList(); properties.ForEach(property => property.SetValue(model, property.GetValue(request.Model))); request.SetModelOnRequest(model, properties); return(model); }
/// <summary> /// Method that extends IWrapRequest<T> allowing to get filter properties from request /// </summary> /// <typeparam name="TModel">Generic type of the entity</typeparam> /// <param name="source">Self IWrapRequest<T> instance</param> /// <returns>Returns a dictionary with properties and values found</returns> internal static List <FilterProperty> FilterProperties <TModel>( this IWrapRequest <TModel> source ) where TModel : class { var filterProperties = new List <FilterProperty>(); foreach (var property in typeof(TModel).GetProperties().Where(x => !source.IsPropertySuppressed(x.Name) ).ToList()) { foreach (var criteria in CriteriasHelper.GetPropertyTypeCriteria(property.PropertyType)) { var criteriaName = $"{property.Name.ToCamelCase()}{criteria}"; var listObjects = new List <object>(); foreach (var value in source.AllProperties.Where(x => x.Name.ToLower().Equals(criteriaName.ToLower()) ).Select(x => x.Value).ToList()) { bool changed = false; object typedValue = TypesHelper.TryChangeType(value.ToString(), property.PropertyType, out changed); if (changed) { listObjects.Add(typedValue); } } if (listObjects.Count > 0) { filterProperties.Add(new FilterProperty { Name = criteriaName, Value = listObjects.Count > 1 ? listObjects : listObjects.FirstOrDefault() }); } } } return(filterProperties); }