/// <summary> /// Method that extends IQueryable<T> allowing to order 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 ordination</returns> public static IQueryable <TSource> OrderBy <TSource>( this IQueryable <TSource> source, IWrapRequest <TSource> request ) where TSource : class { var ordination = request.Ordination(); string order = ordination.Order; string orderBy = ordination.OrderBy; if (source == null) { throw new ArgumentNullException(nameof(source)); } if (request == null) { throw new ArgumentNullException(nameof(request)); } Type type = typeof(TSource); var lambda = LambdasHelper.GenereteLambdaExpression <TSource>(ref type, orderBy); MethodInfo orderMethod; if (order.ToLower().Equals(Constants.CONST_ORDENATION_ORDER_ASCENDING.ToLower())) { orderMethod = ReflectionsHelper.GetMethodFromType(typeof(Queryable), "OrderBy", 2, 2); } else { orderMethod = ReflectionsHelper.GetMethodFromType(typeof(Queryable), "OrderByDescending", 2, 2); } return((IQueryable <TSource>)orderMethod.MakeGenericMethod(typeof(TSource), type).Invoke(null, new object[] { source, lambda })); }
/// <summary> /// Method that extends IQueryable<T> allowing to select results 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 select</returns> public static IQueryable <object> Select <TSource>( this IQueryable <TSource> source, IWrapRequest <TSource> request ) where TSource : class { return(source.Select(LambdasHelper.GenerateSelectExpression <TSource>(request.GetSelectedModel()))); }
internal static bool IsPropertySuppressedResponse <TModel>( this IWrapRequest <TModel> source, string property ) where TModel : class { return(source.SuppressedResponseProperties().Any(suppressed => suppressed.ToLower().Equals(property.ToLower()))); }
/// <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)); }
private static List <SelectedProperty> GetValidProperties <TModel>( IWrapRequest <TModel> source, Type type, List <string> requestProperties, string rootName = null, bool isFromCollection = false) where TModel : class { var properties = new List <SelectedProperty>(); type.GetProperties() .Where(property => !source.IsPropertySuppressedResponse(string.Join(".", TermsHelper.GetTerms(rootName, ".").Union(new List <string> { property.Name }).Where(term => !string.IsNullOrWhiteSpace(term)))) && ( (requestProperties.Count == 0 && IsToLoadComplexPropertyWhenNotRequested(TypesHelper.TypeIsComplex(property.PropertyType), ConfigurationService.GetConfiguration().ByDefaultLoadComplexProperties, string.IsNullOrWhiteSpace(rootName))) || (requestProperties.Any(requested => TermsHelper.GetTerms(requested, ".").FirstOrDefault().ToLower().Equals(property.Name.ToLower()))) ) ) .ToList() .ForEach(property => { if (!TypesHelper.TypeIsComplex(property.PropertyType)) { properties.Add(new SelectedProperty { RequestedPropertyName = string.Join(".", TermsHelper.GetTerms(rootName, ".").Union(new List <string> { property.Name }).Where(term => !string.IsNullOrWhiteSpace(term))), PropertyName = property.Name, RootPropertyName = rootName, PropertyType = property.PropertyType, PropertyInfo = property }); } else if (ConfigurationService.GetConfiguration().ByDefaultLoadComplexProperties) { properties.AddRange( GetValidProperties( source, TypesHelper.TypeIsCollection(property.PropertyType) ? property.PropertyType.GetGenericArguments()[0] : property.PropertyType, requestProperties .Where(requested => TermsHelper.GetTerms(requested, ".").FirstOrDefault().ToLower().Equals(property.Name.ToLower())) .Select(requested => string.Join(".", TermsHelper.GetTerms(requested, ".").Skip(1))) .Where(requested => !string.IsNullOrWhiteSpace(requested)) .ToList(), string.Join(".", TermsHelper.GetTerms(rootName, ".").Union(new List <string> { property.Name }).Where(term => !string.IsNullOrWhiteSpace(term))), TypesHelper.TypeIsCollection(property.PropertyType) ) ); } }); return(properties); }
/// <summary> /// Method that extends IWrapRequest<T> allowing to get MinimumReturnedCollectionSize property /// </summary> /// <typeparam name="TModel">Generic type of the entity</typeparam> /// <param name="source">Self IWrapRequest<T> instance</param> /// <returns>Returns the minimum return collection size</returns> internal static int MinimumReturnedCollectionSize <TModel>( this IWrapRequest <TModel> source ) where TModel : class { object configMin = null; source.ConfigValues.TryGetValue(Constants.CONST_MIN_COLLECTION_SIZE, out configMin); return(configMin == null?ConfigurationService.GetConfiguration().MinimumReturnedCollectionSize : Convert.ToInt32(configMin)); }
/// <summary> /// Method that extends IWrapRequest<T> allowing to get DefaultReturnedCollectionSize property /// </summary> /// <typeparam name="TModel">Generic type of the entity</typeparam> /// <param name="source">Self IWrapRequest<T> instance</param> /// <returns>Returns the default return collection size</returns> internal static int DefaultReturnedCollectionSize <TModel>( this IWrapRequest <TModel> source ) where TModel : class { object size = null; source.ConfigValues.TryGetValue(Constants.CONST_DEFAULT_COLLECTION_SIZE, out size); return(size == null?ConfigurationService.GetConfiguration().DefaultReturnedCollectionSize : Convert.ToInt32(size)); }
/// <summary> /// Method that extends IQueryable<T> allowing to paginate 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 pagination</returns> public static IQueryable <TSource> Page <TSource>( this IQueryable <TSource> source, IWrapRequest <TSource> request ) where TSource : class { var pagination = request.Pagination(); int pageNumber = pagination.Number - 1; int pageSize = pagination.Size; return(source.Skip(pageNumber * pageSize).Take(pageSize)); }
/// <summary> /// Method that extends IQueryable allowing all search functionalities from ModelWrapper /// </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> /// <param name="count">Count of entities</param> /// <returns>Returns IQueryable instance with with the configuration for full search</returns> public static IQueryable <object> FullSearch <TSource>( this IQueryable <TSource> source, IWrapRequest <TSource> request, out long count ) where TSource : class { return(source .Filter(request) .Search(request) .LongCount(out count) .OrderBy(request) .Page(request) .Select(request)); }
/// <summary> /// Method that extends IWrapRequest<T> allowing to get pagination 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 Pagination Pagination <TModel>( this IWrapRequest <TModel> source ) where TModel : class { var pagination = new Pagination(); #region GET PAGE SIZE VALUE var pageSizeProperty = source.AllProperties.Where(x => x.Name.ToLower().Equals(Constants.CONST_PAGINATION_SIZE.ToLower()) && x.Source == WrapPropertySource.FromQuery ).FirstOrDefault(); if (pageSizeProperty != null) { bool changed = false; int typedValue = TypesHelper.TryChangeType <int>(pageSizeProperty.Value.ToString(), out changed); if (changed) { typedValue = typedValue > source.MaximumReturnedCollectionSize() ? source.MaximumReturnedCollectionSize() : typedValue; typedValue = typedValue < source.MinimumReturnedCollectionSize() ? source.MinimumReturnedCollectionSize() : typedValue; pagination.Size = typedValue; } } else { pagination.Size = source.DefaultReturnedCollectionSize(); } #endregion #region GET PAGE NUMBER VALUE var pageNumberProperty = source.AllProperties.Where(x => x.Name.ToLower().Equals(Constants.CONST_PAGINATION_NUMBER.ToLower()) && x.Source == WrapPropertySource.FromQuery).FirstOrDefault(); if (pageNumberProperty != null) { bool changed = false; int typedValue = TypesHelper.TryChangeType <int>(pageNumberProperty.Value.ToString(), out changed); if (changed) { pagination.Number = typedValue != default(int) ? typedValue : 1; } } else { pagination.Number = 1; } #endregion source.RequestObject.SetValue(Constants.CONST_PAGINATION, pagination); return(pagination); }
internal static SelectedModel GetSelectedModel <TModel>( this IWrapRequest <TModel> source ) where TModel : class { var responseProperties = source.ResponseProperties().Select(property => property.TypedClone()).ToList(); var selectedModel = new SelectedModel { Name = source.Model.GetType().Name, OriginalType = source.Model.GetType(), Properties = GetSelectedModelProperties(source.Model.GetType(), responseProperties) }; return(selectedModel); }
/// <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 ordination 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 Ordination Ordination <TModel>( this IWrapRequest <TModel> source ) where TModel : class { var ordination = new Ordination(); #region GET ORDER PROPERTY var orderProperty = source.AllProperties.Where(x => x.Name.ToLower().Equals(Constants.CONST_ORDENATION_ORDER.ToLower()) && x.Source == WrapPropertySource.FromQuery ).FirstOrDefault(); if (orderProperty != null && (orderProperty.Value.ToString().ToLower().Equals(Constants.CONST_ORDENATION_ORDER_ASCENDING.ToLower()) || orderProperty.Value.ToString().ToLower().Equals(Constants.CONST_ORDENATION_ORDER_DESCENDING.ToLower()))) { ordination.Order = orderProperty.Value.ToString().ToLower().Equals(Constants.CONST_ORDENATION_ORDER_ASCENDING.ToLower()) ? Constants.CONST_ORDENATION_ORDER_ASCENDING : Constants.CONST_ORDENATION_ORDER_DESCENDING; } else { ordination.Order = Constants.CONST_ORDENATION_ORDER_ASCENDING; } #endregion #region GET ORDERBY PROPERTY var orderByProperty = source.AllProperties.Where(x => x.Name.ToLower().Equals(Constants.CONST_ORDENATION_ORDERBY.ToLower()) && x.Source == WrapPropertySource.FromQuery ).FirstOrDefault(); if (orderByProperty != null && typeof(TModel).GetProperties().Any(x => x.Name.ToLower().Equals(orderByProperty.Value.ToString().ToLower()))) { var property = typeof(TModel).GetProperties().Where(x => x.Name.ToLower().Equals(orderByProperty.Value.ToString().ToLower())).SingleOrDefault(); ordination.OrderBy = property.Name.ToCamelCase(); } else { if (source.KeyProperties().Any()) { ordination.OrderBy = source.KeyProperties().FirstOrDefault(); } } #endregion source.RequestObject.SetValue(Constants.CONST_ORDENATION, ordination); return(ordination); }
/// <summary> /// Method that extends IWrapRequest<T> allowing to get select 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 <SelectedProperty> ResponseProperties <TModel>( this IWrapRequest <TModel> source ) where TModel : class { List <string> requestProperties = new List <string>(); source.AllProperties.Where(x => x.Name.ToLower().Equals(Constants.CONST_RESPONSE_PROPERTIES.ToLower()) ).ToList().ForEach(x => requestProperties.Add(x.Value.ToString()) ); List <SelectedProperty> properties = GetValidProperties(source, typeof(TModel), requestProperties); source.RequestObject.SetValue(Constants.CONST_RESPONSE_PROPERTIES, properties.Select(x => string.Join(".", x.RequestedPropertyName.Split(".").Select(part => part.ToCamelCase())))); return(properties); }
/// <summary> /// Method that set model properties into the request object /// </summary> /// <typeparam name="TModel">Type of the model/entity class</typeparam> /// <param name="source">IWrapRequest object instance</param> /// <param name="model">Model object</param> /// <param name="properties">List of properties that will be inserted on the request object</param> internal static void SetModelOnRequest <TModel>( this IWrapRequest <TModel> source, TModel model, IList <PropertyInfo> properties ) where TModel : class { if (properties.Count > 0) { var dictionary = new Dictionary <string, object>(); properties.ToList().ForEach(property => { dictionary.Add(property.Name, property.GetValue(model)); }); source.RequestObject.SetValue(Constants.CONST_MODEL, dictionary); } }
/// <summary> /// Method that extends IQueryable<T> allowing to filter 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 filter</returns> public static IQueryable <TSource> Filter <TSource>( this IQueryable <TSource> source, IWrapRequest <TSource> request ) where TSource : class { var filterProperties = request.FilterProperties(); if (filterProperties == null || filterProperties.Count == 0) { return(source); } request.RequestObject.SetValue(Constants.CONST_FILTER_PROPERTIES.ToCamelCase(), filterProperties); var filterDictionary = new Dictionary <string, object>(); filterProperties.ForEach(filter => filterDictionary.Add(filter.Name, filter.Value)); var criteriaExp = LambdasHelper.GenerateFilterCriteriaExpression <TSource>(filterDictionary); return(source.Where(criteriaExp)); }
/// <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); }
/// <summary> /// Method that returns a list of configured keys properties. /// </summary> /// <typeparam name="TModel">Type of the model/entity class</typeparam> /// <param name="source">IWrapRequest object instance</param> /// <returns>List of keys properties</returns> internal static List <string> KeyProperties <TModel>( this IWrapRequest <TModel> source ) where TModel : class { return(source.ConfigProperties.GetValue(Constants.CONST_KEYS)); }
/// <summary> /// Method that returns a list of configured suppressed response properties. /// </summary> /// <typeparam name="TModel">Type of the model/entity class</typeparam> /// <param name="source">IWrapRequest object instance</param> /// <returns>List of supperssed response properties</returns> internal static List <string> SuppressedResponseProperties <TModel>( this IWrapRequest <TModel> source ) where TModel : class { return(source.ConfigProperties.GetValue(Constants.CONST_SUPPRESSED_RESPONSE)); }
/// <summary> /// Method that extends IWrapRequest<T> allowing to get query 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 Search Search <TModel>( this IWrapRequest <TModel> source ) where TModel : class { var search = new Search(); #region GET QUERY PROPERTY var queryProperty = source.AllProperties.Where(x => x.Name.ToLower().Equals(Constants.CONST_QUERY.ToLower()) && x.Source == WrapPropertySource.FromQuery ).FirstOrDefault(); if (queryProperty != null) { bool changed = false; string typedValue = TypesHelper.TryChangeType <string>(queryProperty.Value.ToString(), out changed); if (changed) { search.Query = typedValue; } else { search.Query = string.Empty; } } else { search.Query = string.Empty; } #endregion #region GET QUERY STRICT PROPERTY var queryStrictProperty = source.AllProperties.Where(x => x.Name.ToLower().Equals(Constants.CONST_QUERY_STRICT.ToLower()) && x.Source == WrapPropertySource.FromQuery ).FirstOrDefault(); if (queryStrictProperty != null) { bool changed = false; bool typedValue = TypesHelper.TryChangeType <bool>(queryStrictProperty.Value.ToString(), out changed); if (changed) { search.Strict = typedValue; } else { search.Strict = false; } } else { search.Strict = false; } #endregion #region GET QUERY PHRASE PROPERTY var queryPhraseProperty = source.AllProperties.Where(x => x.Name.ToLower().Equals(Constants.CONST_QUERY_PHRASE.ToLower()) && x.Source == WrapPropertySource.FromQuery ).FirstOrDefault(); if (queryPhraseProperty != null) { bool changed = false; bool typedValue = TypesHelper.TryChangeType <bool>(queryPhraseProperty.Value.ToString(), out changed); if (changed) { search.Phrase = typedValue; } else { search.Phrase = false; } } else { search.Phrase = false; } #endregion source.RequestObject.SetValue(Constants.CONST_SEARCH_PROPERTIES, search); return(search); }
/// <summary> /// Method that returns a list of configured supplied properties. /// </summary> /// <typeparam name="TModel">Type of the model/entity class</typeparam> /// <param name="source">IWrapRequest object instance</param> /// <returns>List of spupplied properties</returns> internal static List <string> SuppliedProperties <TModel>( this IWrapRequest <TModel> source ) where TModel : class { return(source.ConfigProperties.GetValue(Constants.CONST_SUPPLIED)); }