public async Task HandleGetByFilter_TestAsync() { var filter = new WoodyPlantFilterModel(); var sort = new WoodyPlantSortModel(); var request = new GetWoodyPlantsByFilterRequest(filter, sort); var id = ObjectId.GenerateNewId(); var expectedPlants = new List <WoodyPlantDocument>() { new WoodyPlantDocument { Id = id } }; fWoodyPlantsRepository .GetByFilterAsync(Arg.Is(filter), Arg.Is(sort), Arg.Is(default(CancellationToken))) .Returns(expectedPlants); fWoodyPlantsRepository .CountByFilterAsync(Arg.Is(filter), Arg.Is(default(CancellationToken))) .Returns(56); var result = await new WoodyPlantsRequestHandler(fWoodyPlantsRepository, fVersionRepository).Handle(request, default); Assert.NotNull(result); Assert.Equal(expectedPlants.Count, result.WoodyPlants.Count); Assert.Equal(expectedPlants[0].Id, result.WoodyPlants[0].Id); Assert.Equal(56, result.TotalCount); Assert.Null(result.DataVersion); }
public static FindOptions <WoodyPlantDocument> ToFindOptions(this WoodyPlantSortModel model, int skip, int take, bool textSearch) { if (model is null) { throw new ArgumentNullException(nameof(model)); } SortDefinition <WoodyPlantDocument>?sort = null; switch (model.SortBy) { case nameof(WoodyPlantDocument.LocalizedNames): sort = model.Ascending ? Builders <WoodyPlantDocument> .Sort.Ascending(x => x.LocalizedNames.Czech) : Builders <WoodyPlantDocument> .Sort.Descending(x => x.LocalizedNames.Czech); break; case nameof(WoodyPlantDocument.LocalizedNotes): sort = model.Ascending ? Builders <WoodyPlantDocument> .Sort.Ascending(x => x.LocalizedNotes.Czech) : Builders <WoodyPlantDocument> .Sort.Descending(x => x.LocalizedNotes.Czech); break; case nameof(WoodyPlantDocument.LocalizedSpecies): sort = model.Ascending ? Builders <WoodyPlantDocument> .Sort.Ascending(x => x.LocalizedSpecies.Czech) : Builders <WoodyPlantDocument> .Sort.Descending(x => x.LocalizedSpecies.Czech); break; case nameof(WoodyPlantDocument.TextMatchScore): if (textSearch) { sort = Builders <WoodyPlantDocument> .Sort.MetaTextScore(nameof(WoodyPlantDocument.TextMatchScore)); } break; } var options = new FindOptions <WoodyPlantDocument> { Skip = skip, Limit = take, Collation = new Collation("cs") }; if (textSearch && model.SortBy == nameof(WoodyPlantDocument.TextMatchScore)) { options.Projection = Builders <WoodyPlantDocument> .Projection.MetaTextScore(nameof(WoodyPlantDocument.TextMatchScore)); } if (sort != null) { options.Sort = sort; } return(options); }
public async Task GetMany_TestAsync() { var filter = new WoodyPlantFilterModel(); var sort = new WoodyPlantSortModel(); var model = new WoodyPlantListModel(); fMediator .Send(Arg.Is <GetWoodyPlantsByFilterRequest>(x => x.Filter == filter && x.Sort == sort), Arg.Is(default(CancellationToken))) .Returns(model); var result = await new WoodyPlantsController(fMediator).GetManyAsync(filter, sort, default); Assert.Equal(model, result); }
public async Task HandleGetByFilter_OptimalizedGet_TestAsync() { var filter = new WoodyPlantFilterModel { Distance = null, Skip = 0, Text = null, Point = new Point { Latitude = 50, Longitude = 50 }, Take = 70 }; var sort = new WoodyPlantSortModel { SortBy = null, Ascending = true }; var request = new GetWoodyPlantsByFilterRequest(filter, sort); var id = ObjectId.GenerateNewId(); var expectedPlants = new List <WoodyPlantDocument>() { new WoodyPlantDocument { Id = id } }; fWoodyPlantsRepository .GetWithCoordsAsync(Arg.Is(default(CancellationToken))) .Returns(expectedPlants); fWoodyPlantsRepository .CountByFilterAsync(Arg.Is(filter), Arg.Is(default(CancellationToken))) .Returns(1); var result = await new WoodyPlantsRequestHandler(fWoodyPlantsRepository, fVersionRepository).Handle(request, default); Assert.NotNull(result); Assert.True(sort.Ascending); Assert.Equal(expectedPlants.Count, result.WoodyPlants.Count); Assert.Equal(expectedPlants[0].Id, result.WoodyPlants[0].Id); Assert.Equal(1, result.TotalCount); }
public GetWoodyPlantsByFilterRequest(WoodyPlantFilterModel filter, WoodyPlantSortModel sort) { Filter = filter ?? throw new ArgumentNullException(nameof(filter)); Sort = sort ?? throw new ArgumentNullException(nameof(sort)); }
public Task <WoodyPlantListModel> GetManyAsync([FromQuery] WoodyPlantFilterModel filter, [FromQuery] WoodyPlantSortModel sort, CancellationToken cancellationToken) { return(fMediator.Send(new GetWoodyPlantsByFilterRequest(filter, sort), cancellationToken)); }
private async Task <bool> IsFillCoordsFilterAsync(WoodyPlantFilterModel filter, WoodyPlantSortModel sort, CancellationToken cancellationToken) { var pointFilter = filter.Distance == null && filter.Text == null && filter.Skip == 0 && sort.SortBy == null && filter.Point != null; if (!pointFilter) { return(false); } var count = await fWoodyPlantsRepository.CountWithCoordsAsync(cancellationToken); return(count <= filter.Take); }
public async Task <List <WoodyPlantDocument> > GetByFilterAsync(WoodyPlantFilterModel filter, WoodyPlantSortModel sort, CancellationToken cancellationToken) { if (filter is null) { throw new ArgumentNullException(nameof(filter)); } if (sort is null) { throw new ArgumentNullException(nameof(sort)); } var cursor = await Collection.FindAsync( filter.ToFilterDefinition(), sort.ToFindOptions(filter.Skip, filter.Take, filter.Text != null), cancellationToken); var plants = new List <WoodyPlantDocument>(); while (await cursor.MoveNextAsync(cancellationToken)) { plants.AddRange(cursor.Current); } return(plants); }