feat(sorting): implement ApplyOrderWithInStockPriority method for enhanced product sorting
Build and Deploy to Kubernetes / build-and-deploy (push) Has been cancelled
Build and Deploy to Kubernetes / build-and-deploy (push) Has been cancelled
- 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.
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// موجودیدارها اول، سپس سورت درخواستی کاربر (یا Created desc اگر SortBy خالی باشد).
|
||||
/// </summary>
|
||||
public static IQueryable<TSource> ApplyOrderWithInStockPriority<TSource>(
|
||||
this IQueryable<TSource> source,
|
||||
string? sortBy,
|
||||
Expression<Func<TSource, bool>> isInStock) where TSource : BaseAuditableEntity
|
||||
{
|
||||
var ordered = source.OrderByDescending(isInStock);
|
||||
if (string.IsNullOrWhiteSpace(sortBy))
|
||||
return ordered.ThenByDescending(p => p.Created);
|
||||
|
||||
return ordered.ThenBy(NormalizeSortBy(sortBy));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dynamic LINQ treats "-Created" as unary minus on DateTime (invalid).
|
||||
/// Project convention: leading '-' means descending sort.
|
||||
|
||||
+2
-4
@@ -57,10 +57,8 @@ public class GetDiscountProductsQueryHandler : IRequestHandler<GetDiscountProduc
|
||||
query = query.Where(p => 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);
|
||||
|
||||
|
||||
+2
-5
@@ -67,11 +67,8 @@ public class GetCustomerProductsByFilterQueryHandler : IRequestHandler<GetCustom
|
||||
if (request.IsActive.HasValue)
|
||||
query = query.Where(x => 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);
|
||||
|
||||
Reference in New Issue
Block a user