Skip to content

Commit

Permalink
Merge pull request #460 from neozhu/removeAutoFilterer
Browse files Browse the repository at this point in the history
🍒 remove AutoFilterer and use Ardalis.Specification
  • Loading branch information
neozhu authored Aug 8, 2023
2 parents 189744e + 4dfaba7 commit 61246df
Show file tree
Hide file tree
Showing 56 changed files with 265 additions and 2,131 deletions.
10 changes: 7 additions & 3 deletions src/Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Ardalis.Specification" Version="7.0.0" />
<PackageReference Include="Ardalis.Specification.EntityFrameworkCore" Version="7.0.0" />
<PackageReference Include="ClosedXML" Version="0.102.0" />
<PackageReference Include="jcamp.FluentEmail.Core" Version="3.2.0" />
<PackageReference Include="jcamp.FluentEmail.MailKit" Version="3.2.0" />
<PackageReference Include="jcamp.FluentEmail.Razor" Version="3.2.0" />
<PackageReference Include="MailKit" Version="4.1.0" />
<PackageReference Include="AutoFilterer" Version="2.13.0" />
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="FluentValidation" Version="11.6.0" />
Expand All @@ -26,10 +27,13 @@
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="7.0.9" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.3" />
<PackageReference Include="Hangfire.Core" Version="1.8.3" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.4" />
<PackageReference Include="Hangfire.Core" Version="1.8.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Common\Specification\" />
</ItemGroup>
</Project>
388 changes: 0 additions & 388 deletions src/Application/Common/Extensions/ExpressionExtensions.cs

This file was deleted.

13 changes: 5 additions & 8 deletions src/Application/Common/Extensions/QueryableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Ardalis.Specification.EntityFrameworkCore;
using Ardalis.Specification;
using CleanArchitecture.Blazor.Domain.Common;
using DocumentFormat.OpenXml.Bibliography;

namespace CleanArchitecture.Blazor.Application.Common.Extensions;

public static class QueryableExtensions
{
public static IQueryable<T> Specify<T>(this IQueryable<T> query, ISpecification<T> spec) where T : class, IEntity
public static IQueryable<T> Specify<T>(this IQueryable<T> query, ISpecification<T> spec, bool evaluateCriteriaOnly=false) where T : class, IEntity
{
var queryableResultWithIncludes = spec.Includes
.Aggregate(query,
(current, include) => include(current));
var secondaryResult = spec.IncludeStrings
.Aggregate(queryableResultWithIncludes,
(current, include) => current.Include(include));
return secondaryResult.Where(spec.Criteria);
return new SpecificationEvaluator().GetQuery(query, spec, evaluateCriteriaOnly);
}
}
16 changes: 0 additions & 16 deletions src/Application/Common/Interfaces/ISpecification.cs

This file was deleted.

4 changes: 3 additions & 1 deletion src/Application/Common/Mappings/MappingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Ardalis.Specification;

namespace CleanArchitecture.Blazor.Application.Common.Mappings;

public static class MappingExtensions
{
public static Task<PaginatedList<TDestination>> PaginatedListAsync<TDestination>(this IQueryable<TDestination> queryable, int pageNumber, int pageSize) where TDestination : class
public static Task<PaginatedList<TDestination>> PaginatedListAsync<T,TDestination>(this IQueryable<TDestination> queryable, ISpecification<T> spec, int pageNumber, int pageSize, CancellationToken cancellationToken = default) where TDestination : class
=> PaginatedList<TDestination>.CreateAsync(queryable.AsNoTracking(), pageNumber, pageSize);

public static Task<PaginatedData<TDestination>> PaginatedDataAsync<TDestination>(this IQueryable<TDestination> queryable, int pageNumber, int pageSize) where TDestination : class
Expand Down
11 changes: 0 additions & 11 deletions src/Application/Common/Models/FilterRule.cs

This file was deleted.

10 changes: 1 addition & 9 deletions src/Application/Common/Models/PaginationFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ public partial class PaginationFilter : BaseFilter

public class BaseFilter
{
public Search? AdvancedSearch { get; set; }

public string Keyword { get; set; }=String.Empty;
public string? Keyword { get; set; }
}

public partial class Search
{
public List<string> Fields { get; set; } = new();
public string Keyword { get; set; } = String.Empty;

}
34 changes: 0 additions & 34 deletions src/Application/Common/Specification/Specification.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,24 @@

using CleanArchitecture.Blazor.Application.Features.AuditTrails.Caching;
using CleanArchitecture.Blazor.Application.Features.AuditTrails.DTOs;
using CleanArchitecture.Blazor.Application.Features.Documents.Queries.PaginationQuery;
using CleanArchitecture.Blazor.Application.Features.Products.Queries.Specification;
using CleanArchitecture.Blazor.Domain.Enums;

namespace CleanArchitecture.Blazor.Application.Features.AuditTrails.Queries.PaginationQuery;

public class AuditTrailsWithPaginationQuery : PaginationFilterBase, ICacheableRequest<PaginatedData<AuditTrailDto>>
public class AuditTrailsWithPaginationQuery : PaginationFilter, ICacheableRequest<PaginatedData<AuditTrailDto>>
{
[CompareTo("TableName", "UserId")] // <-- This filter will be applied to Name or Brand or Description.
[StringFilterOptions(StringFilterOption.Contains)]
public string? Keyword { get; set; }

[OperatorComparison(OperatorType.Equal)]
public AuditType? AuditType { get; set; }

[CompareTo(typeof(SearchAuditTrailsWithListView), "Id")]
public AuditTrailListView ListView { get; set; } = AuditTrailListView.All;

public string CacheKey => AuditTrailsCacheKey.GetPaginationCacheKey($"{this}");
public UserProfile? CurrentUser { get; set; }
public MemoryCacheEntryOptions? Options => AuditTrailsCacheKey.MemoryCacheEntryOptions;

public AuditTrailsQuerySpec Specification => new AuditTrailsQuerySpec(this);
public override string ToString()
{
return
$"Listview:{ListView},AuditType:{AuditType},Search:{Keyword},Sort:{Sort},SortBy:{SortBy},{Page},{PerPage}";
$"Listview:{ListView},AuditType:{AuditType},Search:{Keyword},Sort:{SortDirection},OrderBy:{OrderBy},{PageNumber},{PageSize}";
}
}

Expand All @@ -49,54 +44,41 @@ IMapper mapper
public async Task<PaginatedData<AuditTrailDto>> Handle(AuditTrailsWithPaginationQuery request,
CancellationToken cancellationToken)
{
var data = await _context.AuditTrails.ApplyFilterWithoutPagination(request)
var data = await _context.AuditTrails.Specify(request.Specification)
.OrderBy($"{request.OrderBy} {request.SortDirection}")
.ProjectTo<AuditTrailDto>(_mapper.ConfigurationProvider)
.PaginatedDataAsync(request.Page, request.PerPage);
.PaginatedDataAsync(request.PageNumber, request.PageSize);

return data;
}
}

public class SearchAuditTrailsWithListView : FilteringOptionsBaseAttribute
public enum AuditTrailListView
{
[Description("All")] All,
[Description("My Change Histories")] My,
[Description("Created Toady")] CreatedToday,
[Description("View of the last 30 days")]
Last30days
}
public class AuditTrailsQuerySpec : Specification<AuditTrail>
{
public override Expression BuildExpression(Expression expressionBody, PropertyInfo targetProperty,
PropertyInfo filterProperty, object value)
public AuditTrailsQuerySpec(AuditTrailsWithPaginationQuery request)
{
var today = DateTime.Now.Date;
var start = Convert.ToDateTime(today.ToString("yyyy-MM-dd", CultureInfo.CurrentCulture) + " 00:00:00",
CultureInfo.CurrentCulture);
var end = Convert.ToDateTime(today.ToString("yyyy-MM-dd", CultureInfo.CurrentCulture) + " 23:59:59",
CultureInfo.CurrentCulture);
var last30days =
Convert.ToDateTime(today.AddDays(-30).ToString("yyyy-MM-dd", CultureInfo.CurrentCulture) + " 00:00:00",
CultureInfo.CurrentCulture);
//var currentUser = filterProperty.CurrentUser;
var listview = (AuditTrailListView)value;
return listview switch
{
AuditTrailListView.All => expressionBody,
AuditTrailListView.Last30days => Expression.GreaterThanOrEqual(
Expression.Property(expressionBody, "DateTime"),
Expression.Constant(last30days, typeof(DateTime)))
.Combine(Expression.LessThanOrEqual(Expression.Property(expressionBody, "DateTime"),
Expression.Constant(end, typeof(DateTime))),
CombineType.And),
AuditTrailListView.CreatedToday => Expression.GreaterThanOrEqual(
Expression.Property(expressionBody, "DateTime"),
Expression.Constant(start, typeof(DateTime)))
.Combine(Expression.LessThanOrEqual(Expression.Property(expressionBody, "DateTime"),
Expression.Constant(end, typeof(DateTime))),
CombineType.And),
_ => expressionBody
};
}
}
var last30day = Convert.ToDateTime(
today.AddDays(-30).ToString("yyyy-MM-dd", CultureInfo.CurrentCulture) + " 00:00:00",
CultureInfo.CurrentCulture);

public enum AuditTrailListView
{
[Description("All")] All,
[Description("Created Toady")] CreatedToday,
Query.Where(p => p.AuditType==request.AuditType, request.AuditType is not null)
.Where(p => p.UserId == request.CurrentUser.UserId, request.ListView == AuditTrailListView.My && request.CurrentUser is not null)
.Where(p => p.DateTime.Date == DateTime.Now.Date, request.ListView == AuditTrailListView.CreatedToday)
.Where(p => p.DateTime >= last30day, request.ListView == AuditTrailListView.Last30days)
.Where(x => x.TableName.Contains(request.Keyword) , !string.IsNullOrEmpty(request.Keyword));

[Description("View of the last 30 days")]
Last30days
}
}
45 changes: 0 additions & 45 deletions src/Application/Features/Customers/Caching/CustomerCacheKey.cs

This file was deleted.

This file was deleted.

Loading

0 comments on commit 61246df

Please sign in to comment.