public static MongoContentSchema ToMongo(this ContentSchema schema) { MongoContentSchema mongoContentItem = new MongoContentSchema(); mongoContentItem.Id = schema.Id; mongoContentItem.Name = schema.Name; mongoContentItem.CreatedAt = schema.CreatedAt; mongoContentItem.ModifiedAt = schema.ModifiedAt; mongoContentItem.Version = schema.Version; mongoContentItem.ListFields = schema.ListFields.ToList(); mongoContentItem.ReferenceFields = schema.ReferenceFields.ToList(); mongoContentItem.OrderFields = schema.OrderFields.ToList(); foreach (var field in schema.Fields) { MongoContentFieldDefinition definition = new MongoContentFieldDefinition(); definition.Label = field.Value.Label; definition.SortKey = field.Value.SortKey; definition.FieldType = field.Value.FieldType; if (field.Value.Options != null) { BsonDocument doc = field.Value.Options.ToBsonDocument(field.Value.Options.GetType()); if (doc.ElementCount > 0) { definition.Options = doc; } } mongoContentItem.Fields.Add(field.Key, definition); } return(mongoContentItem); }
public static ContentSchema ToModel(this MongoContentSchema mongoSchema) { ContentSchema schema = new ContentSchema(mongoSchema.Name); schema.Id = mongoSchema.Id; schema.CreatedAt = mongoSchema.CreatedAt; schema.ModifiedAt = mongoSchema.ModifiedAt; schema.Version = mongoSchema.Version; schema.ListFields = mongoSchema.ListFields.ToList(); schema.ReferenceFields = mongoSchema.ReferenceFields.ToList(); schema.OrderFields = mongoSchema.OrderFields.ToList(); foreach (var field in mongoSchema.Fields) { Type optionsType = ContentFieldManager.Default.GetOptionsType(field.Value.FieldType); ContentFieldOptions?options = null; if (field.Value.Options != BsonNull.Value) { options = (ContentFieldOptions)BsonSerializer.Deserialize((BsonDocument)field.Value.Options, optionsType); } if (options == null) { options = ContentFieldManager.Default.CreateOptions(field.Value.FieldType); } ContentSchemaField schemaField = new ContentSchemaField(field.Value.FieldType, options); schemaField.Label = field.Value.Label; schemaField.SortKey = field.Value.SortKey; schema.Fields.Add(field.Key, schemaField); } return(schema); }