From e5f62f4b146fbf7f11b1fcacc23ab8c7a9917a0e Mon Sep 17 00:00:00 2001 From: "Amir.H Khademi" Date: Mon, 8 Jul 2024 17:05:04 +0330 Subject: [PATCH] feat(ProductFilter) - Add IsEnable filter in products page --- Netina.Api/Controllers/WebSiteController.cs | 2 +- .../CommandQueries/Queries/ProductQueries.cs | 2 +- .../Products/GetProductsQueryHandler.cs | 26 ++++++++++--------- .../Models/ApplicationContext.cs | 7 ----- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/Netina.Api/Controllers/WebSiteController.cs b/Netina.Api/Controllers/WebSiteController.cs index 70336a9..eff8bdc 100644 --- a/Netina.Api/Controllers/WebSiteController.cs +++ b/Netina.Api/Controllers/WebSiteController.cs @@ -1,6 +1,6 @@ namespace Netina.Api.Controllers; -public class WebSiteController:ICarterModule +public class WebSiteController : ICarterModule { public void AddRoutes(IEndpointRouteBuilder app) { diff --git a/Netina.Domain/CommandQueries/Queries/ProductQueries.cs b/Netina.Domain/CommandQueries/Queries/ProductQueries.cs index 2b21e64..f5e4d15 100644 --- a/Netina.Domain/CommandQueries/Queries/ProductQueries.cs +++ b/Netina.Domain/CommandQueries/Queries/ProductQueries.cs @@ -7,7 +7,7 @@ public sealed record GetProductsQuery( bool? IsActive, bool? SpecialOffer, int Page = 0 , - string? ProductName = default, + string? ProductName = null, QuerySortBy SortBy = QuerySortBy.None , Guid CategoryId = default , double MinPrice = -1 , diff --git a/Netina.Repository/Handlers/Products/GetProductsQueryHandler.cs b/Netina.Repository/Handlers/Products/GetProductsQueryHandler.cs index 8ddf449..3da07f3 100644 --- a/Netina.Repository/Handlers/Products/GetProductsQueryHandler.cs +++ b/Netina.Repository/Handlers/Products/GetProductsQueryHandler.cs @@ -27,18 +27,7 @@ public class GetProductsQueryHandler : IRequestHandler p.IsEnable == request.IsActive); if (request.ProductName != null) - products = products.Where(p => p.PersianName.ToLower().Trim().Contains(request.ProductName.ToLower().Trim())); - if (request.CategoryId != default) - { - var cats = await _mediator.Send(new GetProductCategoryChildrenQuery(request.CategoryId), cancellationToken); - products = products.Where(p => cats.Contains(p.CategoryId)); - } - if (request.BrandIds is { Length: > 0 }) - products = products.Where(p => request.BrandIds.Contains(p.BrandId)); - if (request.MinPrice > -1) - products = products.Where(p => p.Cost >= request.MinPrice); - if (request.MaxPrice > 0) - products = products.Where(p => p.Cost <= request.MaxPrice); + products = products.OrderByDescending(p => EF.Functions.TrigramsSimilarity(p.PersianName,request.ProductName)); if (request.SortBy != QuerySortBy.None) { products = request.SortBy switch @@ -55,6 +44,19 @@ public class GetProductsQueryHandler : IRequestHandler p.CreatedAt); } + if (request.CategoryId != default) + { + var cats = await _mediator.Send(new GetProductCategoryChildrenQuery(request.CategoryId), cancellationToken); + products = products.Where(p => cats.Contains(p.CategoryId)); + } + if (request.BrandIds is { Length: > 0 }) + products = products.Where(p => request.BrandIds.Contains(p.BrandId)); + if (request.MinPrice > -1) + products = products.Where(p => p.Cost >= request.MinPrice); + if (request.MaxPrice > 0) + products = products.Where(p => p.Cost <= request.MaxPrice); + + if (request.SpecialOffer != null) { if (request.SpecialOffer.Value) diff --git a/Netina.Repository/Models/ApplicationContext.cs b/Netina.Repository/Models/ApplicationContext.cs index f6e9439..bb21cd6 100644 --- a/Netina.Repository/Models/ApplicationContext.cs +++ b/Netina.Repository/Models/ApplicationContext.cs @@ -29,7 +29,6 @@ public class ApplicationContext : IdentityDbContext FuzzyStringMatchDifference(default, default)); RenameIdentityTables(builder); builder.RegisterEntityTypeConfiguration(entitiesAssembly); @@ -38,12 +37,6 @@ public class ApplicationContext : IdentityDbContext