using JSMR.Application.Common.Search; using JSMR.Application.Creators.Queries.Search.Contracts; using JSMR.Application.Creators.Queries.Search.Ports; using JSMR.Infrastructure.Common.Queries; using System.Linq.Expressions; namespace JSMR.Infrastructure.Data.Repositories.Creators; public class CreatorSearchProvider(AppDbContext context) : SearchProvider, ICreatorSearchProvider { protected override bool UseSelectIdQuery => false; protected override IQueryable GetBaseQuery() { return from creator in context.Creators join voiceWorkCreators in context.VoiceWorkCreators on creator.CreatorId equals voiceWorkCreators.CreatorId into vwcs select new CreatorSearchItem { CreatorId = creator.CreatorId, Name = creator.Name, Favorite = creator.Favorite, Blacklisted = creator.Blacklisted, VoiceWorkCount = vwcs.Count() }; } protected override IQueryable ApplyFilters(IQueryable query, CreatorSearchCriteria criteria) { IQueryable filteredQuery = query; if (string.IsNullOrEmpty(criteria.Name) == false) { string name = criteria.Name.Trim(); filteredQuery = filteredQuery.Where(x => x.Name.Contains(name)); } return filteredQuery; } protected override Expression> GetSortExpression(CreatorSortField field) { Expression> selector = field switch { CreatorSortField.VoiceWorkCount => x => x.VoiceWorkCount, CreatorSortField.Favorite => x => !x.Favorite, CreatorSortField.Blacklisted => x => !x.Blacklisted, _ => x => x.Name }; return selector; } protected override IEnumerable<(Expression> Selector, SortDirection Dir)> GetDefaultSortChain() { yield return (x => x.Name, SortDirection.Ascending); } protected override IOrderedQueryable GetSelectQuery(CreatorSearchCriteria criteria, IOrderedQueryable query) { return query; } protected override IQueryable GetSelectIdQuery(IOrderedQueryable query) { throw new NotImplementedException(); } protected override Task> GetItems(CreatorSearchCriteria criteria, int[] ids) { return Task.FromResult(new Dictionary()); } }