Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🍒 remove AutoFilterer #459

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,80 @@
using CleanArchitecture.Blazor.Application.Features.Products.Caching;
using CleanArchitecture.Blazor.Application.Features.Products.DTOs;
using CleanArchitecture.Blazor.Application.Features.Products.Queries.Specification;
using CleanArchitecture.Blazor.Domain.Entities;

namespace CleanArchitecture.Blazor.Application.Features.Products.Queries.Pagination;

public class ProductsWithPaginationQuery : PaginationFilterBase, ICacheableRequest<PaginatedData<ProductDto>>
{
public string? Name { get; set; }
public string? Brand { get; set; }

public string? Unit { get; set; }
public Range<decimal> Price { get; set; } = new();

[CompareTo("Name", "Brand", "Description")] // <-- This filter will be applied to Name or Brand or Description.
[StringFilterOptions(StringFilterOption.Contains)]
public string? Keyword { get; set; }

[CompareTo(typeof(SearchProductsWithListView), "Name")]
public ProductListView ListView { get; set; } =
ProductListView.All; //<-- When the user selects a different ListView,
public UserProfile? CurrentUser { get; set; } // <-- This CurrentUser property gets its value from the information of

// a custom query expression is executed on the backend.
// For example, if the user selects "My Products",
// the query will be x => x.CreatedBy == CurrentUser.UserId
[IgnoreFilter]
public UserProfile?
CurrentUser { get; set; } // <-- This CurrentUser property gets its value from the information of
public string CacheKey => ProductCacheKey.GetPaginationCacheKey($"{this}");

[IgnoreFilter] public string CacheKey => ProductCacheKey.GetPaginationCacheKey($"{this}");

[IgnoreFilter] public MemoryCacheEntryOptions? Options => ProductCacheKey.MemoryCacheEntryOptions;
public MemoryCacheEntryOptions? Options => ProductCacheKey.MemoryCacheEntryOptions;

// the currently logged in user
public override string ToString()
{
return
$"CurrentUser:{CurrentUser?.UserId},ListView:{ListView},Search:{Keyword},Name:{Name},Brand:{Brand},Unit:{Unit},MinPrice:{Price?.Min},MaxPrice:{Price?.Max},Sort:{Sort},SortBy:{SortBy},{Page},{PerPage}";
}

public Expression<Func<Product, bool>> LinqExpression()
{
Expression<Func<Product, bool>> initExpr = product => true;
Expression<Func<Product, bool>> keywordExpre = product => product.Name.Contains(Keyword) || product.Description.Contains(Keyword);
Expression<Func<Product, bool>> brandExpre = product => product.Brand.Contains(Brand);
Expression<Func<Product, bool>> unitExpre = product => product.Unit.Equals(Unit);
Expression<Func<Product, bool>> priceExpre = product => product.Price >= Price.Min && product.Price <= Price.Max;
var parameter = Expression.Parameter(typeof(Product));

switch (ListView)
{
case ProductListView.My:
Expression<Func<Product, bool>> myexp = product => product.CreatedBy == CurrentUser.UserId;
initExpr = initExpr.And(myexp);
break;
case ProductListView.CreatedToday:
var today = DateTime.Now;
Expression<Func<Product, bool>> todayexp = product => product.Created.Value.Date == today.Date;
initExpr = initExpr.And(todayexp);
break;
case ProductListView.Created30Days:
var last30day = DateTime.Now.AddDays(-30);
Expression<Func<Product, bool>> last30exp = product => product.Created.Value.Date >= last30day.Date;
initExpr = initExpr.And(last30exp);
break;
case ProductListView.All:
default:
break;
}

if (!string.IsNullOrEmpty(Keyword))
{
initExpr = initExpr.And(keywordExpre);
}
if (!string.IsNullOrEmpty(Brand))
{
initExpr = initExpr.And(brandExpre);
}
if (!string.IsNullOrEmpty(Unit))
{
initExpr = initExpr.And(unitExpre);
}
if (Price != null && Price.Max != null && Price.Min != null)
{
initExpr = initExpr.And(priceExpre);
}
return initExpr;
}
}

public class ProductsWithPaginationQueryHandler :
Expand All @@ -64,7 +102,8 @@ IStringLocalizer<ProductsWithPaginationQueryHandler> localizer
public async Task<PaginatedData<ProductDto>> Handle(ProductsWithPaginationQuery request,
CancellationToken cancellationToken)
{
var data = await _context.Products.ApplyFilterWithoutPagination(request)
var expresss = request.LinqExpression();
var data = await _context.Products.Where(expresss)
.ProjectTo<ProductDto>(_mapper.ConfigurationProvider)
.PaginatedDataAsync(request.Page, request.PerPage);
return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ public enum ProductListView
[Description("All")] All,
[Description("My Products")] My,
[Description("Created Toady")] CreatedToday,

[Description("Created within the last 30 days")]
Created30Days
[Description("Created within the last 30 days")] Created30Days
}

public class SearchProductsWithListView : FilteringOptionsBaseAttribute
Expand Down
8 changes: 6 additions & 2 deletions src/Blazor.Server.UI/Pages/Products/Products.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/pages/products"
@using CleanArchitecture.Blazor.Application.Features.Fluxor;
@using CleanArchitecture.Blazor.Application.Features.Products.DTOs
@using CleanArchitecture.Blazor.Application.Features.Products.Queries
@using CleanArchitecture.Blazor.Application.Features.Products.Queries.Export
Expand All @@ -12,7 +13,7 @@
@using CleanArchitecture.Blazor.Application.Features.Products.Queries.Specification
@using Severity = MudBlazor.Severity


@inherits FluxorComponent
@inject IJSRuntime JS
@inject IStringLocalizer<Products> L
@attribute [Authorize(Policy = Permissions.Products.View)]
Expand Down Expand Up @@ -255,7 +256,9 @@
private bool _exporting;
private bool _pdfExporting;
private int _defaultPageSize = 15;

[Inject]
private IState<UserProfileState> UserProfileState { get; set; } = null!;
private UserProfile UserProfile => UserProfileState.Value.UserProfile;
[Inject]
private IMediator Mediator { get; set; } = default!;

Expand Down Expand Up @@ -294,6 +297,7 @@
try
{
_loading = true;
Query.CurrentUser = UserProfile;
Query.Sort = state.SortDefinitions.FirstOrDefault()?.SortBy ?? "Id";
Query.SortBy = state.SortDefinitions.FirstOrDefault()?.Descending ?? true ? Sorting.Descending : Sorting.Ascending;
Query.Page = state.Page + 1;
Expand Down