Skip to content

Commit

Permalink
Merge pull request #10 from RicardoZambon/features/catalogs
Browse files Browse the repository at this point in the history
fix: simplify catalogs
  • Loading branch information
RicardoZambon authored Mar 8, 2024
2 parents dab7756 + f90475a commit 8ec9d6b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 49 deletions.
47 changes: 24 additions & 23 deletions ZWebAPI/ExtensionMethods/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,43 @@ public static class QueryableExtensions
/// <summary>
/// Gets the catalog result model fom a query.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="query">The query.</param>
/// <param name="parameters">The parameters.</param>
/// <param name="keySelector">The key selector.</param>
/// <param name="valueSelector">The value selector.</param>
/// <returns>The catalog result model.</returns>
public static CatalogResultModel<TKey> GetCatalog<TKey>(this IQueryable<KeyValuePair<TKey, string>> query, ICatalogParameters parameters)
public static CatalogResultModel<TKey> GetCatalog<TEntity, TKey>(this IQueryable<TEntity> query, ICatalogParameters parameters, Expression<Func<TEntity, TKey>> keySelector, Expression<Func<TEntity, string?>> valueSelector)
where TEntity : class
where TKey : struct
{
CatalogResultModel<TKey> result = new();

IQueryable<KeyValuePair<TKey, string?>> catalogQuery = query.ConvertToKeyValuePairs(keySelector, valueSelector);

if (!string.IsNullOrEmpty(parameters.Criteria))
{
query = query.Where(x => EF.Functions.Like(x.Value.ToLower(), $"%{parameters.Criteria.ToLower()}%"));
catalogQuery = catalogQuery.Where(x =>
x.Value != null
&& EF.Functions.Like(x.Value.ToLower(), $"%{parameters.Criteria.ToLower()}%")
);
}

if (parameters.MaxResults > 0 && query.Count() > parameters.MaxResults)
if (parameters.MaxResults > 0 && catalogQuery.Count() > parameters.MaxResults)
{
result.ShouldUseCriteria = true;
}
else
{
result.Entries = query
result.Entries = catalogQuery
.Select(x => new CatalogEntryModel<TKey>()
{
Display = x.Value,
Value = x.Key,
});
}

return result;
}

Expand Down Expand Up @@ -144,25 +154,6 @@ public static IQueryable<TEntity> TryFilter<TEntity>(this IQueryable<TEntity> qu
}
return query;
}

/// <summary>
/// Converts a query to key value pairs.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="query">The query.</param>
/// <param name="keySelector">The key selector.</param>
/// <param name="valueSelector">The value selector.</param>
/// <returns>The query converted to key value pairs.</returns>
public static IQueryable<KeyValuePair<TKey, string>> ConvertToKeyValuePairs<TEntity, TKey>(this IQueryable<TEntity> query, Expression<Func<TEntity, TKey>> keySelector, Expression<Func<TEntity, string>> valueSelector)
where TEntity : class
where TKey : struct
{
return query.Select(x => new KeyValuePair<TKey, string>(
keySelector.Compile().Invoke(x),
valueSelector.Compile().Invoke(x)
));
}
#endregion

#region Private methods
Expand Down Expand Up @@ -252,6 +243,16 @@ public static IQueryable<KeyValuePair<TKey, string>> ConvertToKeyValuePairs<TEnt
return null;
}

private static IQueryable<KeyValuePair<TKey, string?>> ConvertToKeyValuePairs<TEntity, TKey>(this IQueryable<TEntity> query, Expression<Func<TEntity, TKey>> keySelector, Expression<Func<TEntity, string?>> valueSelector)
where TEntity : class
where TKey : struct
{
return query.Select(x => new KeyValuePair<TKey, string?>(
keySelector.Compile().Invoke(x),
valueSelector.Compile().Invoke(x)
));
}

private static ParameterExpression GetParameter<TEntity>()
=> Expression.Parameter(typeof(TEntity), typeof(TEntity).Name.ToLower());

Expand Down
17 changes: 17 additions & 0 deletions ZWebAPI/Models/CatalogParametersModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using ZWebAPI.Interfaces;

namespace ZWebAPI.Models
{
/// <summary>
/// Parameters model for catalogs.
/// </summary>
/// <seealso cref="ZWebAPI.Interfaces.ICatalogParameters" />
public class CatalogParametersModel : SummaryParametersModel, ICatalogParameters
{
/// <inheritdoc />
public string? Criteria { get; set; }

/// <inheritdoc />
public int MaxResults { get; set; }
}
}
25 changes: 5 additions & 20 deletions ZWebAPI/Models/ListParametersModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,13 @@ namespace ZWebAPI.Models
/// <seealso cref="ZWebAPI.Interfaces.IListParameters" />
public class ListParametersModel : SummaryParametersModel, IListParameters
{
/// <summary>
/// Gets or sets the start row.
/// </summary>
/// <value>
/// The start row.
/// </value>
public int StartRow { get; set; }

/// <summary>
/// Gets or sets the end row.
/// </summary>
/// <value>
/// The end row.
/// </value>
/// <inheritdoc />
public int EndRow { get; set; }

/// <summary>
/// Gets or sets the sort.
/// </summary>
/// <value>
/// The sort.
/// </value>
/// <inheritdoc />
public IDictionary<string, string> Sort { get; set; } = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);

/// <inheritdoc />
public int StartRow { get; set; }
}
}
7 changes: 1 addition & 6 deletions ZWebAPI/Models/SummaryParametersModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ namespace ZWebAPI.Models
/// <seealso cref="ZWebAPI.Interfaces.ISummaryParameters" />
public class SummaryParametersModel : ISummaryParameters
{
/// <summary>
/// Gets or sets the filters.
/// </summary>
/// <value>
/// The filters.
/// </value>
/// <inheritdoc />
public IDictionary<string, object>? Filters { get; set; } = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
}
}

0 comments on commit 8ec9d6b

Please sign in to comment.