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);