From ce98efdd6815ef3be7201936815522c18e26ae2c Mon Sep 17 00:00:00 2001 From: masoodafar-web Date: Sun, 26 Jul 2026 00:17:44 +0330 Subject: [PATCH] feat(sorting): implement ApplyOrderWithInStockPriority method for enhanced product sorting - Added ApplyOrderWithInStockPriority method to prioritize in-stock products in queries. - Updated GetDiscountProductsQueryHandler and GetCustomerProductsByFilterQueryHandler to utilize the new sorting method, ensuring in-stock items are listed first followed by user-defined sorting or creation date. - Improved overall query handling for product availability and sorting logic. --- .../Common/Extensions/SortByExtensions.cs | 16 ++++++++++++++++ .../GetDiscountProductsQueryHandler.cs | 6 ++---- .../GetCustomerProductsByFilterQueryHandler.cs | 7 ++----- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/CMSMicroservice.Application/Common/Extensions/SortByExtensions.cs b/src/CMSMicroservice.Application/Common/Extensions/SortByExtensions.cs index 439e9a5..ebb6a06 100644 --- a/src/CMSMicroservice.Application/Common/Extensions/SortByExtensions.cs +++ b/src/CMSMicroservice.Application/Common/Extensions/SortByExtensions.cs @@ -1,3 +1,4 @@ +using System.Linq.Expressions; using System.Linq.Dynamic.Core; using CMSMicroservice.Domain.Common; @@ -16,6 +17,21 @@ public static class SortByExtensions return source.OrderBy(NormalizeSortBy(sortBy)); } + /// + /// موجودی‌دارها اول، سپس سورت درخواستی کاربر (یا Created desc اگر SortBy خالی باشد). + /// + public static IQueryable ApplyOrderWithInStockPriority( + this IQueryable source, + string? sortBy, + Expression> isInStock) where TSource : BaseAuditableEntity + { + var ordered = source.OrderByDescending(isInStock); + if (string.IsNullOrWhiteSpace(sortBy)) + return ordered.ThenByDescending(p => p.Created); + + return ordered.ThenBy(NormalizeSortBy(sortBy)); + } + /// /// Dynamic LINQ treats "-Created" as unary minus on DateTime (invalid). /// Project convention: leading '-' means descending sort. diff --git a/src/CMSMicroservice.Application/DiscountShopCQ/Queries/GetDiscountProducts/GetDiscountProductsQueryHandler.cs b/src/CMSMicroservice.Application/DiscountShopCQ/Queries/GetDiscountProducts/GetDiscountProductsQueryHandler.cs index f7e5c9b..5edacaf 100644 --- a/src/CMSMicroservice.Application/DiscountShopCQ/Queries/GetDiscountProducts/GetDiscountProductsQueryHandler.cs +++ b/src/CMSMicroservice.Application/DiscountShopCQ/Queries/GetDiscountProducts/GetDiscountProductsQueryHandler.cs @@ -57,10 +57,8 @@ public class GetDiscountProductsQueryHandler : IRequestHandler p.Price <= request.MaxPrice.Value); } - if (!string.IsNullOrEmpty(request.SortBy)) - query = query.ApplyOrder(request.SortBy); - else - query = query.OrderByDescending(p => p.Created); + // موجودی‌دارها اول، سپس سورت درخواستی + query = query.ApplyOrderWithInStockPriority(request.SortBy, p => p.RemainingCount > 0); var totalCount = await query.CountAsync(cancellationToken); diff --git a/src/CMSMicroservice.Application/ProductsCQ/Queries/GetCustomerProductsByFilter/GetCustomerProductsByFilterQueryHandler.cs b/src/CMSMicroservice.Application/ProductsCQ/Queries/GetCustomerProductsByFilter/GetCustomerProductsByFilterQueryHandler.cs index 669c9d4..e468628 100644 --- a/src/CMSMicroservice.Application/ProductsCQ/Queries/GetCustomerProductsByFilter/GetCustomerProductsByFilterQueryHandler.cs +++ b/src/CMSMicroservice.Application/ProductsCQ/Queries/GetCustomerProductsByFilter/GetCustomerProductsByFilterQueryHandler.cs @@ -67,11 +67,8 @@ public class GetCustomerProductsByFilterQueryHandler : IRequestHandler x.IsDeleted != request.IsActive.Value); - // Apply sorting - if (!string.IsNullOrEmpty(request.SortBy)) - query = query.ApplyOrder(request.SortBy); - else - query = query.OrderByDescending(x => x.Created); + // موجودی‌دارها اول، سپس سورت درخواستی + query = query.ApplyOrderWithInStockPriority(request.SortBy, x => x.RemainingCount > 0); // Pagination var totalCount = await query.CountAsync(cancellationToken);