Ejemplo n.º 1
0
        public async Task <SearchResult <object> > SearchObjects([NotNull] string objectIdentifier, [NotNull][FromBody] ObjectSearchRequest query)
        {
            var type       = schemaRegistry.GetTypeByTypeIdentifier(objectIdentifier);
            var schema     = schemaRegistry.GetSchemaByTypeIdentifier(objectIdentifier);
            var countLimit = schema.Description.CountLimit;
            var connector  = schemaRegistry.GetConnector(objectIdentifier);
            var counts     = await connector.Count(query.GetFilters(), countLimit + 1).ConfigureAwait(false);

            var results = await connector.Search(query.GetFilters(), query.GetSorts(), query.Offset ?? 0, query.Count ?? 20).ConfigureAwait(false);

            var typeInfo = TypeInfoExtractor.Extract(type, schema.PropertyDescriptionBuilder, schema.CustomPropertyConfigurationProvider);
            var objects  = results.Select(x => ObjectsConverter.StoredToApi(typeInfo, type, x, schema.CustomPropertyConfigurationProvider)).ToArray();

            return(new SearchResult <object>
            {
                Count = counts ?? objects.Length,
                CountLimit = countLimit,
                Items = objects,
            });
        }
Ejemplo n.º 2
0
        public async Task <DownloadResult> DownloadObjects([NotNull] string objectIdentifier, [NotNull][FromBody] ObjectSearchRequest query)
        {
            var type          = schemaRegistry.GetTypeByTypeIdentifier(objectIdentifier);
            var schema        = schemaRegistry.GetSchemaByTypeIdentifier(objectIdentifier);
            var downloadLimit = schema.Description.DownloadLimit;
            var count         = await schemaRegistry.GetConnector(objectIdentifier).Count(query.GetFilters(), downloadLimit + 1).ConfigureAwait(false);

            if (count > downloadLimit)
            {
                return new DownloadResult
                       {
                           File       = null,
                           Count      = (int)count,
                           CountLimit = downloadLimit,
                       }
            }
            ;

            var results = await schemaRegistry.GetConnector(objectIdentifier).Search(query.GetFilters(), query.GetSorts(), 0, downloadLimit).ConfigureAwait(false);

            var properties = new List <string>();
            var getters    = new List <Func <object, object> >();

            PropertyHelpers.BuildGettersForProperties(type, "", x => x, properties, getters);

            var excludedIndices    = properties.Select((x, i) => (x, i)).Where(x => query.ExcludedFields.Contains(x.x)).Select(x => x.i).ToArray();
            var filteredProperties = properties.Where((x, i) => !excludedIndices.Contains(i)).ToArray();
            var filteredGetters    = getters.Where((x, i) => !excludedIndices.Contains(i)).ToArray();

            var csvWriter = new CsvWriter(filteredProperties);

            foreach (var item in results)
            {
                csvWriter.AddRow(filteredGetters.Select(f => PropertyHelpers.ToString(f, item)).ToArray());
            }

            return(new DownloadResult
            {
                Count = count ?? 0,
                CountLimit = downloadLimit,
                File = new FileInfo
                {
                    Content = csvWriter.GetBytes(),
                    ContentType = "text/csv",
                    Name = $"{objectIdentifier}-{DateTime.UtcNow:yyyy-MM-dd-HHmm}.csv"
                }
            });
        }