Skip to content

Commit

Permalink
Added ComponentField + Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
usercode committed Dec 27, 2022
1 parent 5487fd2 commit e007e07
Show file tree
Hide file tree
Showing 232 changed files with 1,085 additions and 621 deletions.
1 change: 0 additions & 1 deletion src/DragonFly.API.Client/ClientContentService .Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using DragonFly.Data.Models;
using System.Net.Http.Json;
using System.Net.Http.Headers;
using DragonFly.Storage;
using DragonFly.Assets.Query;

namespace DragonFly.Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using DragonFly.Assets.Query;
using DragonFly.Data.Models;
using System.Net.Http.Json;
using DragonFly.Storage;

namespace DragonFly.Client;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using DragonFly.Data.Models;
using DragonFly.Models;
using System.Net.Http.Json;
using DragonFly.Storage;
using DragonFly.AspNetCore.API.Exports.Json;
using DragonFly.Query;

Expand Down
2 changes: 1 addition & 1 deletion src/DragonFly.API.Client/ClientContentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace DragonFly.Client;
/// <summary>
/// ContentService
/// </summary>
public partial class ClientContentService
public partial class ClientContentService : IDataStorage
{
public ClientContentService(HttpClient httpClient)
{
Expand Down
7 changes: 4 additions & 3 deletions src/DragonFly.API.Client/DragonFlyBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// https://github.com/usercode/DragonFly
// MIT License

using DragonFly.API.Json;
using DragonFly.Builders;
using DragonFly.API.Exports.Json;
using DragonFly.Core.ContentStructures;
using DragonFly.Storage;
using Microsoft.Extensions.DependencyInjection;

namespace DragonFly.Client;
Expand All @@ -15,13 +14,15 @@ public static class StartupExtensions
public static IDragonFlyBuilder AddRestApi(this IDragonFlyBuilder builder)
{
builder.Services.AddTransient<ClientContentService>();
builder.Services.AddTransient<IDataStorage, ClientContentService>();
builder.Services.AddTransient<IContentStorage, ClientContentService>();
builder.Services.AddTransient<IStructureStorage, ClientContentService>();
builder.Services.AddTransient<IWebHookStorage, ClientContentService>();
builder.Services.AddTransient<IAssetStorage, ClientContentService>();
builder.Services.AddTransient<IAssetFolderStorage, ClientContentService>();

builder.PostInit<JsonDerivedTypesAction>();
builder.Init(api => api.JsonField().AddDefaults());
builder.PostInit<CreateMissingJsonFieldSerializer>();

return builder;
}
Expand Down
6 changes: 3 additions & 3 deletions src/DragonFly.API.Exports/Assets/RestAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class RestAsset : RestContentBase
{
public RestAsset()
{
_metaddata = new Dictionary<string, JsonNode>();
_metaddata = new JsonObject();
}

/// <summary>
Expand Down Expand Up @@ -62,10 +62,10 @@ public RestAsset()
/// </summary>
public virtual RestAssetFolder Folder { get; set; }

private IDictionary<string, JsonNode> _metaddata;
private JsonObject _metaddata;

/// <summary>
/// Metaddata
/// </summary>
public virtual IDictionary<string, JsonNode> Metaddata { get => _metaddata; set => _metaddata = value; }
public virtual JsonObject Metaddata { get => _metaddata; set => _metaddata = value; }
}
2 changes: 1 addition & 1 deletion src/DragonFly.API.Exports/Assets/RestAssetConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static RestAsset ToRest(this Asset asset)
restAsset.Folder = asset.Folder.ToRest();
}

foreach(var metadata in asset.Metaddata)
foreach (var metadata in asset.Metaddata)
{
restAsset.Metaddata.Add(metadata.Key, JsonSerializer.SerializeToNode(metadata.Value, metadata.Value.GetType()));
}
Expand Down
38 changes: 38 additions & 0 deletions src/DragonFly.API.Exports/ContentItems/RestContentComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) usercode
// https://github.com/usercode/DragonFly
// MIT License

using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using DragonFly.Contents.Content;

namespace DragonFly.Models;

/// <summary>
/// RestContentComponent
/// </summary>
public class RestContentComponent
{
public RestContentComponent()
{
Fields = new JsonObject();
}

/// <summary>
/// Schema
/// </summary>
[JsonPropertyOrder(3)]
public JsonNode? Schema { get; set; }

/// <summary>
/// SchemaVersion
/// </summary>
[JsonPropertyOrder(2)]
public int SchemaVersion { get; set; }

/// <summary>
/// Fields
/// </summary>
[JsonPropertyOrder(30)]
public JsonObject Fields { get; set; }
}
29 changes: 0 additions & 29 deletions src/DragonFly.API.Exports/ContentItems/RestContentEmbedded.cs

This file was deleted.

17 changes: 0 additions & 17 deletions src/DragonFly.API.Exports/ContentItems/RestContentFields.cs

This file was deleted.

6 changes: 3 additions & 3 deletions src/DragonFly.API.Exports/ContentItems/RestContentItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using DragonFly.Content;
using DragonFly.Contents.Content;

namespace DragonFly.Models;

/// <summary>
Expand All @@ -15,7 +15,7 @@ public class RestContentItem : RestContentBase
{
public RestContentItem()
{
Fields = new RestContentFields();
Fields = new JsonObject();
}

/// <summary>
Expand All @@ -34,5 +34,5 @@ public RestContentItem()
/// Fields
/// </summary>
[JsonPropertyOrder(30)]
public RestContentFields Fields { get; set; }
public JsonObject Fields { get; set; }
}
81 changes: 52 additions & 29 deletions src/DragonFly.API.Exports/ContentItems/RestContentItemConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// https://github.com/usercode/DragonFly
// MIT License

using DragonFly.AspNetCore.API.Exports.Json;
using DragonFly.AspNetCore.API.Models;
using DragonFly.Models;
using System.Text.Json;
Expand All @@ -13,28 +14,22 @@ public static class RestContentItemConverter
{
public static ContentItem ToModel(this RestContentItem restContentItem)
{
return ToModel(restContentItem, null);
}

public static ContentItem ToModel(this RestContentItem restContentItem, ContentSchema? schema)
{
if (schema == null)
ContentSchema schema;

if (restContentItem.Schema is JsonValue jsonValue)
{
if (restContentItem.Schema is JsonValue jsonValue)
schema = new ContentSchema(jsonValue.GetValue<string>());
}
else
{
RestContentSchema? restSchema = restContentItem.Schema.Deserialize<RestContentSchema>(JsonSerializerDefault.Options);

if (restSchema == null)
{
schema = new ContentSchema(jsonValue.GetValue<string>());
throw new Exception("Unknown schema");
}
else
{
RestContentSchema? restSchema = restContentItem.Schema.Deserialize<RestContentSchema>();

if (restSchema == null)
{
throw new Exception("Unknown schema");
}

schema = restSchema.ToModel();
}
schema = restSchema.ToModel();
}

ContentItem contentItem = schema.CreateContent();
Expand All @@ -54,11 +49,27 @@ public static ContentItem ToModel(this RestContentItem restContentItem, ContentS
return contentItem;
}

public static ContentEmbedded ToModel(this RestContentEmbedded restContentItem)
public static ContentComponent ToModel(this RestContentComponent restContentItem)
{
ContentSchema schema = restContentItem.Schema.ToModel();
ContentSchema? schema;

ContentEmbedded contentItem = schema.CreateEmbeddedContent();
if (restContentItem.Schema is JsonValue jsonValue)
{
schema = new ContentSchema(jsonValue.GetValue<string>());
}
else
{
RestContentSchema? restSchema = restContentItem.Schema.Deserialize<RestContentSchema>(JsonSerializerDefault.Options);

if (restSchema == null)
{
throw new Exception("Unknown schema");
}

schema = restSchema.ToModel();
}

ContentComponent contentItem = schema.CreateEmbeddedContent();

foreach (var restField in restContentItem.Fields)
{
Expand All @@ -73,14 +84,16 @@ public static RestContentItem ToRest(this ContentItem contentItem, bool includeS
RestContentItem restContentItem = new RestContentItem();

restContentItem.Id = contentItem.Id;

if (includeSchema)
{
restContentItem.Schema = JsonSerializer.SerializeToNode(contentItem.Schema.ToRest());
restContentItem.Schema = JsonSerializer.SerializeToNode(contentItem.Schema.ToRest(), JsonSerializerDefault.Options);
}
else
{
restContentItem.Schema = JsonValue.Create(contentItem.Schema.Name);
}

restContentItem.CreatedAt = contentItem.CreatedAt;
restContentItem.ModifiedAt = contentItem.ModifiedAt;
restContentItem.PublishedAt = contentItem.PublishedAt;
Expand All @@ -91,23 +104,33 @@ public static RestContentItem ToRest(this ContentItem contentItem, bool includeS
{
JsonNode? node = field.Value.ToRestValue(includeNavigationProperties);

if (node != null)
{
//if (node != null)
//{
restContentItem.Fields.Add(field.Key, node);
}
//}
}

return restContentItem;
}

public static RestContentItem ToRest(this ContentEmbedded contentItem)
public static RestContentComponent ToRest(this ContentComponent contentItem, bool includeSchema, bool includeNavigationProperties)
{
RestContentItem restContentItem = new RestContentItem();
restContentItem.Schema = JsonSerializer.SerializeToNode(contentItem.Schema.ToRest());
RestContentComponent restContentItem = new RestContentComponent();

if (includeSchema)
{
restContentItem.Schema = JsonSerializer.SerializeToNode(contentItem.Schema.ToRest(), JsonSerializerDefault.Options);
}
else
{
restContentItem.Schema = JsonValue.Create(contentItem.Schema.Name);
}

foreach (var field in contentItem.Fields)
{
restContentItem.Fields.Add(field.Key, field.Value.ToRestValue());
JsonNode? node = field.Value.ToRestValue(includeNavigationProperties);

restContentItem.Fields[field.Key] = node;
}

return restContentItem;
Expand Down
Loading

0 comments on commit e007e07

Please sign in to comment.