Beispiel #1
0
        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 FilterDefinition <WoodyPlantDocument> ToFilterDefinition(this WoodyPlantFilterModel model)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var filter = FilterDefinition <WoodyPlantDocument> .Empty;

            if (model.Text != null)
            {
                var text = BuildAndTextSearch(model.Text);
                filter &= Builders <WoodyPlantDocument> .Filter.Text(text, new TextSearchOptions
                {
                    CaseSensitive      = false,
                    DiacriticSensitive = false
                });
            }

            if (model.Point != null)
            {
                filter &= Builders <WoodyPlantDocument> .Filter.NearSphere(x => x.Location !.Geometry, model.Point.Longitude, model.Point.Latitude, model.Distance);
            }

            return(filter);
        }
        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);
        }
        public async Task <long> CountByFilterAsync(WoodyPlantFilterModel filter, CancellationToken cancellationToken)
        {
            if (filter.Point != null)
            {
                return(-1);
            }

            return(await Collection.CountDocumentsAsync(filter.ToFilterDefinition(), cancellationToken : cancellationToken));
        }
        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);
        }
Beispiel #6
0
        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);
        }
        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);
        }
Beispiel #8
0
 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));
 }