|
|
|
@ -6,6 +6,7 @@ |
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Linq; |
|
|
|
using Microsoft.Extensions.Caching.Memory; |
|
|
|
using Microsoft.OData.Edm; |
|
|
|
using Squidex.Domain.Apps.Core; |
|
|
|
@ -26,34 +27,56 @@ namespace Squidex.Domain.Apps.Entities.Contents.Edm |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
public virtual IEdmModel BuildEdmModel(ISchemaEntity schema, IAppEntity app) |
|
|
|
public virtual IEdmModel BuildEdmModel(IAppEntity app, ISchemaEntity schema, bool withHidden) |
|
|
|
{ |
|
|
|
Guard.NotNull(schema, nameof(schema)); |
|
|
|
|
|
|
|
var cacheKey = $"{schema.Id}_{schema.Version}_{app.Id}_{app.Version}"; |
|
|
|
var cacheKey = BuildCacheKey(app, schema, withHidden); |
|
|
|
|
|
|
|
var result = Cache.GetOrCreate<IEdmModel>(cacheKey, entry => |
|
|
|
{ |
|
|
|
entry.AbsoluteExpirationRelativeToNow = CacheTime; |
|
|
|
|
|
|
|
return BuildEdmModel(schema.SchemaDef, app.PartitionResolver()); |
|
|
|
return BuildEdmModel(schema.SchemaDef, app, withHidden); |
|
|
|
}); |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
private static EdmModel BuildEdmModel(Schema schema, PartitionResolver partitionResolver) |
|
|
|
private static EdmModel BuildEdmModel(Schema schema, IAppEntity app, bool withHidden) |
|
|
|
{ |
|
|
|
var model = new EdmModel(); |
|
|
|
|
|
|
|
var schemaType = schema.BuildEdmType(partitionResolver, x => |
|
|
|
var pascalAppName = app.Name.ToPascalCase(); |
|
|
|
var pascalSchemaName = schema.Name.ToPascalCase(); |
|
|
|
|
|
|
|
var typeFactory = new EdmTypeFactory(name => |
|
|
|
{ |
|
|
|
var finalName = pascalSchemaName; |
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(name)) |
|
|
|
{ |
|
|
|
finalName += "."; |
|
|
|
finalName += name; |
|
|
|
} |
|
|
|
|
|
|
|
var result = model.SchemaElements.OfType<EdmComplexType>().FirstOrDefault(x => x.Name == finalName); |
|
|
|
|
|
|
|
if (result != null) |
|
|
|
{ |
|
|
|
model.AddElement(x); |
|
|
|
return (result, false); |
|
|
|
} |
|
|
|
|
|
|
|
result = new EdmComplexType(pascalAppName, finalName); |
|
|
|
|
|
|
|
return x; |
|
|
|
model.AddElement(result); |
|
|
|
|
|
|
|
return (result, true); |
|
|
|
}); |
|
|
|
|
|
|
|
var entityType = new EdmEntityType("Squidex", schema.Name); |
|
|
|
var schemaType = schema.BuildEdmType(withHidden, app.PartitionResolver(), typeFactory); |
|
|
|
|
|
|
|
var entityType = new EdmEntityType(app.Name.ToPascalCase(), schema.Name); |
|
|
|
entityType.AddStructuralProperty(nameof(IContentEntity.Id).ToCamelCase(), EdmPrimitiveTypeKind.String); |
|
|
|
entityType.AddStructuralProperty(nameof(IContentEntity.Created).ToCamelCase(), EdmPrimitiveTypeKind.DateTimeOffset); |
|
|
|
entityType.AddStructuralProperty(nameof(IContentEntity.CreatedBy).ToCamelCase(), EdmPrimitiveTypeKind.String); |
|
|
|
@ -73,5 +96,10 @@ namespace Squidex.Domain.Apps.Entities.Contents.Edm |
|
|
|
|
|
|
|
return model; |
|
|
|
} |
|
|
|
|
|
|
|
private static string BuildCacheKey(IAppEntity app, ISchemaEntity schema, bool withHidden) |
|
|
|
{ |
|
|
|
return string.Join("_", schema.Id, schema.Version, app.Id, app.Version, withHidden); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|