public async Task <IEnumerable <Store> > GetByAddressAsync(AddressInfo address, CancellationToken cancellation = default) { var Addressfilter = Builders <Store> .Filter.Regex(x => x.Address.Address, new MongoDB.Bson.BsonRegularExpression(address.Address, "i")); var StateFilter = Builders <Store> .Filter.Eq(x => x.Address.State, address.State); var CityFilter = Builders <Store> .Filter.Eq(x => x.Address.City, address.City); var ZipFilter = Builders <Store> .Filter.Eq(x => x.Address.Zip, address.Zip); List <FilterDefinition <Store> > filters = new List <FilterDefinition <Store> >(); if (!string.IsNullOrWhiteSpace(address.Zip)) { return(await _context.GetByCustom <Store>(async mongodb => { using (var cursor = await mongodb.GetCollection <Store>(Name()).FindAsync(ZipFilter, null, cancellation)) { while (cursor.MoveNext(cancellation)) { return cursor.Current; } return Enumerable.Empty <Store>(); } }, cancellation)); } if (!string.IsNullOrEmpty(address.City)) { filters.Add(CityFilter); } if (!string.IsNullOrEmpty(address.State)) { filters.Add(StateFilter); } if (!string.IsNullOrEmpty(address.Address)) { filters.Add(Addressfilter); } if (filters.Count > 0) { var andFilter = Builders <Store> .Filter.And(filters.ToArray()); return(await _context.GetByCustom <Store>(async mongo => { using (var cursor = await mongo.GetCollection <Store>(Name()).FindAsync(andFilter, null, cancellation)) { if (cursor.MoveNext(cancellation)) { return cursor.Current; } return Enumerable.Empty <Store>(); } }, cancellation)); } return(Enumerable.Empty <Store>()); }
public async Task <UserProfile> FindByExternalProviderAsync(string provider, string userId, CancellationToken cancellationToken = default) { var res = await _context.GetByCustom(async db => { var providerFilter = Builders <UserProfile> .Filter.Eq(x => x.ProviderName, provider); var userFilter = Builders <UserProfile> .Filter.Eq(x => x.Username, userId); var andFilter = Builders <UserProfile> .Filter.And(new[] { providerFilter, userFilter }); using (var cursor = await db.GetCollection <UserProfile>(Name()).FindAsync(andFilter, null, cancellationToken)) { if (cursor.MoveNext()) { return(cursor.Current); } return(Enumerable.Empty <UserProfile>()); } }, cancellationToken); return(res.SingleOrDefault()); }