using JSMR.Application.Tags.Queries.Search.Contracts; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace JSMR.Application.Common.Search; //public abstract class SearchReader2 : ISearchReader // where TSortField : struct, Enum //{ // public Task> SearchAsync(SearchOptions options, CancellationToken cancellationToken = default) // { // throw new NotImplementedException(); // } //} //public abstract class SearchReader where TSortField : struct, Enum //{ // public async Task> SearchAsync(SearchOptions options, CancellationToken cancellationToken = default) // { // IQueryable baseQuery = GetBaseQuery(); // IQueryable filteredQuery = ApplyFilters(baseQuery, options); // int total = await filteredQuery.CountAsync(cancellationToken); // IOrderedQueryable orderedQuery = ApplySorting(filteredQuery, options); // TSearchResultItem[] items = await orderedQuery // .Skip((options.PageNumber - 1) * options.PageSize) // .Take(options.PageSize) // .ToArrayAsync(cancellationToken); // return new SearchResult() // { // Items = items, // TotalItems = total // }; // } // protected abstract IQueryable GetBaseQuery(); // protected abstract IQueryable ApplyFilters(IQueryable query, SearchOptions options); // private IOrderedQueryable ApplySorting(IQueryable query, SearchOptions options) // { // IOrderedQueryable? ordered = null; // for (int i = 0; i < options.SortOptions.Length; i++) // { // var (field, direction) = (options.SortOptions[i].Field, options.SortOptions[i].Direction); // bool isDescending = direction == SortDirection.Descending; // IOrderedQueryable applyFirst(Expression> selector) => isDescending ? query.OrderByDescending(selector) : query.OrderBy(selector); // IOrderedQueryable applyNext(Expression> selector) => isDescending ? ordered!.ThenByDescending(selector) : ordered!.ThenBy(selector); // Expression> selector = GetSortExpression(field); // ordered = (i == 0) ? applyFirst(selector) : applyNext(selector); // } // return ordered ?? GetDefaultSortExpression(query); // } // protected abstract Expression> GetSortExpression(TSortField field); // protected abstract IOrderedQueryable GetDefaultSortExpression(IQueryable query); //}