feat(ProductFilter)

- Add IsEnable filter in products page
subProduct
Amir Hossein Khademi 2024-07-08 17:05:04 +03:30
parent a66ee7c8a2
commit e5f62f4b14
4 changed files with 16 additions and 21 deletions

View File

@ -1,6 +1,6 @@
namespace Netina.Api.Controllers;
public class WebSiteController:ICarterModule
public class WebSiteController : ICarterModule
{
public void AddRoutes(IEndpointRouteBuilder app)
{

View File

@ -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 ,

View File

@ -27,18 +27,7 @@ public class GetProductsQueryHandler : IRequestHandler<GetProductsQuery, GetProd
if (request.IsActive != null)
products = products.Where(p => 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<GetProductsQuery, GetProd
products = products.OrderByDescending(p => 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)

View File

@ -29,7 +29,6 @@ public class ApplicationContext : IdentityDbContext<ApplicationUser, Application
builder.HasPostgresExtension("pg_trgm");
builder.HasPostgresExtension("fuzzystrmatch");
builder.HasDbFunction(() => FuzzyStringMatchDifference(default, default));
RenameIdentityTables(builder);
builder.RegisterEntityTypeConfiguration(entitiesAssembly);
@ -38,12 +37,6 @@ public class ApplicationContext : IdentityDbContext<ApplicationUser, Application
//builder.AddSequentialGuidForIdConvention();
}
[DbFunction("difference", Schema = "public")]
public static int FuzzyStringMatchDifference(string source, string target)
{
throw new NotImplementedException();
}
protected void RenameIdentityTables(ModelBuilder builder)
{
builder.HasDefaultSchema("public");