Beispiel #1
0
        public async Task <IActionResult> List([FromQuery] int?page,
                                               [FromQuery] int?perPage, [FromQuery] string types, [FromQuery] string name, [FromQuery] bool geoJSON = false)
        {
            var query = new ListObjectsQuery(page, perPage)
            {
                NameToken = name
            };

            if (types != null)
            {
                query.Types = types.Split(',').Select(x => (EObjectType)(int.Parse(x))).ToArray();
            }

            var model = await _objectsRepository.GetObjects(query, CancellationToken.None);

            if (model == null)
            {
                return(BadRequest());
            }

            if (geoJSON)
            {
                var featureCollection = new FeatureCollection();

                foreach (var item in model.Data)
                {
                    if (!string.IsNullOrEmpty(item.Latitude) && !string.IsNullOrEmpty(item.Longitude))
                    {
                        var point      = new Point(new Position(double.Parse(item.Latitude, CultureInfo.InvariantCulture), double.Parse(item.Longitude, CultureInfo.InvariantCulture)));
                        var properties = new Dictionary <string, object>
                        {
                            { "objectId", item.ObjectId },
                            { "name", item.Name },
                            { "type", item.Type }
                        };

                        if (!string.IsNullOrEmpty(item.MainPhoto?.Url))
                        {
                            properties.Add("photoUrl", item.MainPhoto.Url);
                        }

                        var feature = new Feature(point, properties, item.ObjectId);

                        featureCollection.Features.Add(feature);
                    }
                }

                return(Ok(featureCollection));
            }
            else
            {
                return(Ok(model));
            }
        }
Beispiel #2
0
        public async Task QueryObjectListWithPaging()
        {
            var runner   = MongoDbRunner.Start();
            var database = TestHelpers.GetDefaultDatabase(runner.ConnectionString);

            var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "3.json");

            runner.Import("okn", "objects", path, true);

            var handler = new ObjectsRepository(TestHelpers.GetDefaultMapper(), new DbContext(database));

            var query = new ListObjectsQuery(page: 2, perPage: 5);

            var result = await handler.GetObjects(query, CancellationToken.None);

            Assert.NotNull(result);
            Assert.Equal(4, result.Data?.Count);

            runner.Dispose();
        }