public async Task <StoresSearchDto> SearchAsync( [GraphQLType(typeof(SearchTermsInputType))][GraphQLName("input")] SearchTermsDto terms, [ScopedService] QueryDbContext context, CancellationToken token) { SetLogTransaction(terms); var query = context.Stores.Where(c => c.OpenForNewBusiness); if (!string.IsNullOrWhiteSpace(terms.Text)) { query = query.Where(p => p.Name.Contains(terms.Text)); } if (terms.Tags != null && terms.Tags.Any()) { query = query.Where(p => p.Tags.Any(t => terms.Tags.Contains(t.Tag.Name))); } var producer = await context.Producers.SingleAsync(e => e.Id == CurrentUser.Id, token); Point currentPosition = null; if (producer.Address?.Latitude != null && producer.Address?.Longitude != null) { currentPosition = LocationProvider.CreatePoint(producer.Address.Latitude.Value, producer.Address.Longitude.Value); query = query.Where(p => p.Address.Location.Distance(currentPosition) < _searchOptions.StoresDistance); } var count = await query.CountAsync(token); if (!string.IsNullOrWhiteSpace(terms.Sort)) { if (terms.Sort.Contains("store_geolocation") && currentPosition != null) { query = query.OrderBy(p => p.Address.Location.Distance(currentPosition)); } else { query = query.OrderBy(p => p.Name); } } else { query = query.OrderBy(p => p.Name); } query = query.Skip(((terms.Page ?? 1) - 1) * terms.Take ?? 20); query = query.Take(terms.Take ?? 20); var results = await query.ToListAsync(token); return(new StoresSearchDto { Count = count, Stores = results }); }
public async Task <IEnumerable <Producer> > SuggestProducersAsync( [GraphQLType(typeof(SearchTermsInputType))][GraphQLName("input")] SearchTermsDto terms, [ScopedService] QueryDbContext context, CancellationToken token) { SetLogTransaction(terms); var query = context.Producers.Where(c => c.ProductsCount > 0); if (!string.IsNullOrWhiteSpace(terms.Text)) { query = query.Where(c => c.Name.Contains(terms.Text)); } return(await query.ToListAsync(token)); }
public async Task <ProducersSearchDto> SearchAsync( [GraphQLType(typeof(SearchTermsInputType))][GraphQLName("input")] SearchTermsDto terms, [ScopedService] QueryDbContext context, CancellationToken token) { SetLogTransaction(terms); var query = context.Catalogs .Where(c => c.Kind == CatalogKind.Stores && c.Available && c.Producer.OpenForNewBusiness); if (!string.IsNullOrWhiteSpace(terms.Text)) { query = query.Where(p => p.Producer.Name.Contains(terms.Text)); } if (terms.Tags != null && terms.Tags.Any()) { query = query.Where(p => p.Producer.Tags.Any(t => terms.Tags.Contains(t.Tag.Name))); } var store = await context.Stores.SingleAsync(e => e.Id == CurrentUser.Id, token); Point currentPosition = null; if (store.Address?.Latitude != null && store.Address?.Longitude != null) { currentPosition = LocationProvider.CreatePoint(store.Address.Latitude.Value, store.Address.Longitude.Value); query = query.Where(p => p.Producer.Address.Location.Distance(currentPosition) < _searchOptions.ProducersDistance); } var producersId = await query.Select(c => c.Producer.Id).Distinct().ToListAsync(token); var finalQuery = context.Producers.Where(p => producersId.Contains(p.Id)); if (!string.IsNullOrWhiteSpace(terms.Sort)) { if (terms.Sort.Contains("producer_geolocation") && currentPosition != null) { finalQuery = finalQuery.OrderBy(p => p.Address.Location.Distance(currentPosition)); } else { finalQuery = finalQuery.OrderBy(p => p.Name); } } else { finalQuery = finalQuery.OrderBy(p => p.Name); } finalQuery = finalQuery.Skip(((terms.Page ?? 1) - 1) * terms.Take ?? 20); finalQuery = finalQuery.Take(terms.Take ?? 20); var results = await finalQuery.ToListAsync(token); return(new ProducersSearchDto { Count = producersId.Count, Producers = results }); }