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 DeliveryAddress(string line1, string line2, string zipcode, string city, CountryIsoCode country, double?longitude, double?latitude) : base(line1, line2, zipcode, city, country) { Longitude = longitude; Latitude = latitude; if (latitude.HasValue && longitude.HasValue) { Location = LocationProvider.CreatePoint(latitude.Value, longitude.Value); } }
public UserAddress(string line1, string line2, string zipcode, string city, CountryIsoCode country, Department department, double?longitude = null, double?latitude = null) : base(line1, line2, zipcode, city, country) { Department = department; Longitude = longitude; Latitude = latitude; if (latitude.HasValue && longitude.HasValue) { Location = LocationProvider.CreatePoint(latitude.Value, longitude.Value); } }
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 }); }
public async Task <ProductsSearchDto> SearchAsync( [GraphQLType(typeof(SearchProductsInputType))][GraphQLName("input")] SearchProductsDto terms, [ScopedService] QueryDbContext context, CancellationToken token) { SetLogTransaction(terms); var query = context.ConsumerProducts.AsQueryable(); if (!string.IsNullOrWhiteSpace(terms.Text)) { query = query.Where(p => p.Name.Contains(terms.Text)); } if (terms.ProducerId.HasValue) { query = query.Where(p => p.ProducerId == terms.ProducerId.Value); } if (terms.Tags != null && terms.Tags.Any()) { foreach (var tag in terms.Tags) { query = query.Where(p => p.Tags.Contains(tag)); } } Point currentPosition = null; if (terms.Longitude.HasValue && terms.Latitude.HasValue) { currentPosition = LocationProvider.CreatePoint(terms.Latitude.Value, terms.Longitude.Value); query = query.Where(p => p.Location.Distance(currentPosition) < _searchOptions.ProductsDistance); } var count = await query.CountAsync(token); if (!string.IsNullOrWhiteSpace(terms.Sort)) { var sort = terms.Sort.ToLowerInvariant(); if (sort.Contains("producer_geolocation") && currentPosition != null) { query = query.OrderBy(p => p.Location.Distance(currentPosition)); } else if (sort.Contains("price") && sort.Contains("asc")) { query = query.OrderBy(p => p.OnSalePricePerUnit); } else if (sort.Contains("price") && sort.Contains("desc")) { query = query.OrderByDescending(p => p.OnSalePricePerUnit); } 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 .Select(p => p.Id) .ToListAsync(token); var products = await context.Products.Where(p => results.Contains(p.Id) && p.Producer.CanDirectSell).ToListAsync(token); var orderedProducts = results.Select(result => products.SingleOrDefault(p => p.Id == result)).ToList(); return(new ProductsSearchDto { Count = count, Products = orderedProducts }); }