/// <summary> /// Edits the sql query by adding sorting based on sortable properties of <see cref="TFilterable"/> /// </summary> /// <typeparam name="TFilterable">The filterable entity</typeparam> /// <param name="sql">The sql to be modified</param> /// <param name="sortingClauses">The sorting clauses</param> protected void AttachSortOptions <TFilterable>(ref string sql, IEnumerable <SortByClause> sortingClauses) where TFilterable : class { var sortablePropertyInfo = TypeInfoExtractor.GetPropertiesByAttribute <TFilterable, SortableAttribute>().ToList(); var propertyNameAttributes = sortablePropertyInfo.ToDictionary(k => k.Name.ToUpper(), v => v.GetCustomAttribute(typeof(SortableAttribute)) as SortableAttribute); if (sortingClauses == null || !propertyNameAttributes.Any()) { return; } var sortingList = sortingClauses.ToList(); // keep only sortable fields sortingList.RemoveAll(s => !propertyNameAttributes.Any(f => f.Key.ToUpper().Equals(s.Path.ToUpper()))); if (!sortingList.Any()) { return; } var sortQuerySegment = string.Empty; foreach (SortByClause clause in sortingList) { var fieldName = propertyNameAttributes[clause.Path.ToUpper()]?.FieldName; var propertyName = sortablePropertyInfo.FirstOrDefault(x => string.Equals(x.Name, clause.Path, StringComparison.CurrentCultureIgnoreCase))?.Name; var field = fieldName == null?propertyName?.ToUpper() : fieldName.ToUpper(); sortQuerySegment += $"\n {field} {clause.Direction.ToUpper()} {(clause != sortingList.Last() ? "," : string.Empty)}"; } sql += $"\n ORDER BY {sortQuerySegment}"; }
public async Task Delete(string typeIdentifier, [FromBody] object obj) { var type = schemaRegistry.GetTypeByTypeIdentifier(typeIdentifier); var schema = schemaRegistry.GetSchemaByTypeIdentifier(typeIdentifier); var typeInfo = TypeInfoExtractor.Extract(type, schema.PropertyDescriptionBuilder, schema.CustomPropertyConfigurationProvider); var x = ObjectsConverter.ApiToStored(typeInfo, type, obj, schema.CustomPropertyConfigurationProvider); await schemaRegistry.GetConnector(typeIdentifier).Delete(x).ConfigureAwait(false); }
public Task DeleteObject([NotNull] string objectIdentifier, [NotNull][FromBody] object obj) { var type = schemaRegistry.GetTypeByTypeIdentifier(objectIdentifier); var schema = schemaRegistry.GetSchemaByTypeIdentifier(objectIdentifier); var typeInfo = TypeInfoExtractor.Extract(type, schema.PropertyDescriptionBuilder, schema.CustomPropertyConfigurationProvider); var x = ObjectsConverter.ApiToStored(typeInfo, type, obj, schema.CustomPropertyConfigurationProvider); return(schemaRegistry.GetConnector(objectIdentifier).Delete(x)); }
public async Task <object> UpdateObject([NotNull] string objectIdentifier, [NotNull][FromBody] object obj) { var type = schemaRegistry.GetTypeByTypeIdentifier(objectIdentifier); var schema = schemaRegistry.GetSchemaByTypeIdentifier(objectIdentifier); var typeInfo = TypeInfoExtractor.Extract(obj, type, schema.PropertyDescriptionBuilder, schema.CustomPropertyConfigurationProvider); var stored = ObjectsConverter.ApiToStored(typeInfo, type, obj, schema.CustomPropertyConfigurationProvider); var newObject = await schemaRegistry.GetConnector(objectIdentifier).Write(stored).ConfigureAwait(false); return(ObjectsConverter.StoredToApi(typeInfo, type, newObject, schema.CustomPropertyConfigurationProvider)); }
public async Task <ObjectDetailsModel> Read(string typeIdentifier, [FromBody] ReadModel filters) { var type = schemaRegistry.GetTypeByTypeIdentifier(typeIdentifier); var schema = schemaRegistry.GetSchemaByTypeIdentifier(typeIdentifier); var result = await schemaRegistry.GetConnector(typeIdentifier).Read(filters.Filters).ConfigureAwait(false); var typeInfo = TypeInfoExtractor.Extract(result, type, schema.PropertyDescriptionBuilder, schema.CustomPropertyConfigurationProvider); var obj = ObjectsConverter.StoredToApi(typeInfo, type, result, schema.CustomPropertyConfigurationProvider); return(new ObjectDetailsModel { Object = obj, TypeInfo = typeInfo, }); }
public TypesListModel GetTypes() { var allSchemas = schemaRegistry.GetAllSchemas(); return(new TypesListModel { Types = allSchemas.SelectMany(schema => schema.Types.Select(x => new TypeModel { Name = x.TypeIdentifier, SchemaDescription = schema.Description, Shape = TypeInfoExtractor.Extract(x.Type, schema.PropertyDescriptionBuilder, schema.CustomPropertyConfigurationProvider), })) .OrderBy(x => x.Name).ToArray(), }); }
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, }); }
public async Task <ObjectDetails> ReadObject([NotNull] string objectIdentifier, [NotNull][FromBody] ObjectSearchRequest query) { var type = schemaRegistry.GetTypeByTypeIdentifier(objectIdentifier); var schema = schemaRegistry.GetSchemaByTypeIdentifier(objectIdentifier); var typeMeta = PropertyHelpers.BuildTypeMetaInformation(type, schema.PropertyDescriptionBuilder, schema.CustomPropertyConfigurationProvider); var result = await schemaRegistry.GetConnector(objectIdentifier).Read(query.GetFilters()).ConfigureAwait(false); var typeInfo = TypeInfoExtractor.Extract(result, type, schema.PropertyDescriptionBuilder, schema.CustomPropertyConfigurationProvider); var obj = ObjectsConverter.StoredToApi(typeInfo, type, result, schema.CustomPropertyConfigurationProvider); return(new ObjectDetails { Object = obj, Meta = new ObjectDescription { Identifier = objectIdentifier, SchemaDescription = schema.Description, TypeMetaInformation = typeMeta, } }); }
/// <summary> /// Edits the sql query by adding search on searchable properties of <see cref="TFilterable"/> /// </summary> /// <typeparam name="TFilterable">The filterable entity</typeparam> /// <param name="sql">The sql to be modified</param> /// <param name="term">The search term</param> /// <param name="parameters">The parameters object to use to include the additional search parameters</param> protected void AttachSearchOptions <TFilterable>(ref string sql, string term, DynamicParameters parameters) where TFilterable : class { var searchablePropertyInfo = TypeInfoExtractor.GetPropertiesByAttribute <TFilterable, SearchableAttribute>().ToList(); var propertyNameAttributes = searchablePropertyInfo.ToDictionary(k => k.Name.ToUpper(), v => v.GetCustomAttribute(typeof(SearchableAttribute)) as SearchableAttribute); var isPreParameterized = parameters.ParameterNames.Any(); if (string.IsNullOrEmpty(term) || !propertyNameAttributes.Any()) { return; } List <string> expressions = new List <string>(); foreach (KeyValuePair <string, SearchableAttribute> property in propertyNameAttributes) { if (term.Length < property.Value.MinimumLength) { continue; } var fieldName = property.Value.FieldName; var propertyName = searchablePropertyInfo.FirstOrDefault(x => string.Equals(x.Name, property.Key, StringComparison.CurrentCultureIgnoreCase))?.Name; var field = fieldName == null?propertyName?.ToUpper() : fieldName.ToUpper(); expressions.Add($"UPPER({field}) LIKE UPPER(:{field})"); parameters.Add($"@{field}", $"%{term}%", null, ParameterDirection.Input); } if (isPreParameterized) { // If parameters exist before adding new ones, then a where clause is assumed to be present in the sql query QueryBuilder.AddSearchQueryParameters(ref sql, expressions, buildWhereClause: false); } else { QueryBuilder.AddSearchQueryParameters(ref sql, expressions); } }
public void Test_Primitives(Type type, TypeInfo expected) { CheckResult(TypeInfoExtractor.Extract(type, new SamplePropertyDescriptionBuilder(), null), expected); }