Skip to content

Commit

Permalink
Improved storage API
Browse files Browse the repository at this point in the history
  • Loading branch information
usercode committed Mar 5, 2023
1 parent 6c69988 commit 5a13835
Show file tree
Hide file tree
Showing 32 changed files with 352 additions and 297 deletions.
28 changes: 11 additions & 17 deletions src/DragonFly.API.Client/ClientAPI/ClientContentService .Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,12 @@ public async Task<QueryResult<Asset>> QueryAsync(AssetQuery assetQuery)
throw new Exception();
}

QueryResult<Asset> assetResult = new QueryResult<Asset>();
assetResult.Offset = result.Offset;
assetResult.Count = result.Count;
assetResult.TotalCount = result.TotalCount;
assetResult.Items = result.Items.Select(x => x.ToModel()).ToList();

return assetResult;
return result.Convert(x => x.ToModel());
}

public async Task UploadAsync(Guid id, string mimetype, Stream stream)
public async Task UploadAsync(Asset asset, string mimetype, Stream stream)
{
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, $"api/asset/{id}/upload");
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, $"api/asset/{asset.Id}/upload");
requestMessage.Content = new StreamContent(stream);
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(mimetype);

Expand All @@ -45,9 +39,9 @@ public async Task UploadAsync(Guid id, string mimetype, Stream stream)
response.EnsureSuccessStatusCode();
}

public async Task<Stream> GetStreamAsync(Guid id)
public async Task<Stream> GetStreamAsync(Asset asset)
{
return await Client.GetStreamAsync($"api/asset/{id}/download");
return await Client.GetStreamAsync($"api/asset/{asset.Id}/download");
}

//assets
Expand All @@ -69,23 +63,23 @@ public async Task CreateAsync(Asset entity)
entity.Id = result.Id;
}

public async Task PublishAsync(Guid id)
public async Task PublishAsync(Asset asset)
{
await Client.PostAsync($"api/asset/{id}/publish", new StringContent(string.Empty));
await Client.PostAsync($"api/asset/{asset.Id}/publish", new StringContent(string.Empty));
}

public async Task UpdateAsync(Asset entity)
{
await Client.PutAsJsonAsync($"api/asset", entity.ToRest());
}

public async Task DeleteAsync(Guid id)
public async Task DeleteAsync(Asset asset)
{
await Client.DeleteAsync($"api/asset/{id}");
await Client.DeleteAsync($"api/asset/{asset.Id}");
}

public async Task ApplyMetadataAsync(Guid id)
public async Task ApplyMetadataAsync(Asset asset)
{
await Client.PostAsync($"api/asset/{id}/metadata", new StringContent(string.Empty));
await Client.PostAsync($"api/asset/{asset.Id}/metadata", new StringContent(string.Empty));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// MIT License

using DragonFly.Assets.Query;
using DragonFly.Query;
using System.Net.Http.Json;

namespace DragonFly.API.Client;
Expand All @@ -19,20 +20,27 @@ public async Task CreateAsync(AssetFolder folder)
response.EnsureSuccessStatusCode();
}

public Task<AssetFolder> GetAssetFolderAsync(Guid id)
public async Task<AssetFolder?> GetAssetFolderAsync(Guid id)
{
throw new NotImplementedException();
RestAssetFolder? entity = await Client.GetFromJsonAsync<RestAssetFolder>($"api/assetfolder/{id}");

if (entity == null)
{
return null;
}

return entity.ToModel();
}

public async Task<IEnumerable<AssetFolder>> QueryAsync(AssetFolderQuery query)
public async Task<QueryResult<AssetFolder>> QueryAsync(AssetFolderQuery query)
{
var response = await Client.PostAsJsonAsync("api/assetfolder/query", query);

response.EnsureSuccessStatusCode();

IEnumerable<RestAssetFolder>? result = await response.Content.ReadFromJsonAsync<IEnumerable<RestAssetFolder>>();
QueryResult<RestAssetFolder>? result = await response.Content.ReadFromJsonAsync<QueryResult<RestAssetFolder>>();

return result.Select(x => x.ToModel()).ToList();
return result.Convert(x => x.ToModel());
}

public Task UpdateAsync(AssetFolder folder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ namespace DragonFly.API.Client;
/// </summary>
public partial class ClientContentService : IContentStorage
{
public async Task<ContentItem> GetContentAsync(string schema, Guid id)
public async Task<ContentItem?> GetContentAsync(string schema, Guid id)
{
var response = await Client.GetAsync($"api/content/{schema}/{id}");
RestContentItem? entity = await Client.GetFromJsonAsync<RestContentItem>($"api/content/{schema}/{id}");

response.EnsureSuccessStatusCode();

var e = await response.Content.ReadFromJsonAsync<RestContentItem>();
if (entity == null)
{
return null;
}

return e.ToModel();
return entity.ToModel();
}

public async Task CreateAsync(ContentItem entity)
Expand All @@ -43,23 +44,16 @@ public async Task DeleteAsync(ContentItem entity)
await Client.DeleteAsync($"api/content/{entity.Schema.Name}/{entity.Id}");
}

public async Task PublishAsync(string schema, Guid id)
public async Task PublishAsync(ContentItem entity)
{
var response = await Client.PostAsync($"api/content/{schema}/{id}/publish", new StringContent(string.Empty));
var response = await Client.PostAsync($"api/content/{entity.Schema.Name}/{entity.Id}/publish", new StringContent(string.Empty));

response.EnsureSuccessStatusCode();
}

public async Task UnpublishAsync(string schema, Guid id)
public async Task UnpublishAsync(ContentItem entity)
{
var response = await Client.PostAsync($"api/content/{schema}/{id}/unpublish", new StringContent(string.Empty));

response.EnsureSuccessStatusCode();
}

public async Task DeleteAsync(string schema, Guid id)
{
var response = await Client.DeleteAsync($"api/content/{schema}/{id}");
var response = await Client.PostAsync($"api/content/{entity.Schema.Name}/{entity.Id}/unpublish", new StringContent(string.Empty));

response.EnsureSuccessStatusCode();
}
Expand All @@ -68,15 +62,9 @@ public async Task<QueryResult<ContentItem>> QueryAsync(ContentQuery queryParamet
{
HttpResponseMessage response = await Client.PostAsJsonAsync($"api/content/query", queryParameters, JsonSerializerDefault.Options);

var queryResult = await response.Content.ReadFromJsonAsync<QueryResult<RestContentItem>>();
QueryResult<RestContentItem>? queryResult = await response.Content.ReadFromJsonAsync<QueryResult<RestContentItem>>();

return new QueryResult<ContentItem>()
{
Offset = queryResult.Offset,
Count = queryResult.Count,
TotalCount = queryResult.TotalCount,
Items = queryResult.Items.Select(x => x.ToModel()).ToList()
};
return queryResult.Convert(x => x.ToModel());
}

public async Task PublishQueryAsync(ContentQuery query)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,9 @@ public async Task<QueryResult<ContentSchema>> QuerySchemasAsync()
{
var response = await Client.PostAsync("api/schema/query", new StringContent(""));

var restQueryResult = await response.Content.ReadFromJsonAsync<QueryResult<RestContentSchema>>();
QueryResult<RestContentSchema>? queryResult = await response.Content.ReadFromJsonAsync<QueryResult<RestContentSchema>>();

QueryResult<ContentSchema> queryResult = new QueryResult<ContentSchema>();
queryResult.Offset = restQueryResult.Offset;
queryResult.Count = restQueryResult.Count;
queryResult.TotalCount = restQueryResult.TotalCount;
queryResult.Items = restQueryResult.Items.Select(x => x.ToModel()).ToList();

return queryResult;
return queryResult.Convert(x => x.ToModel());
}
catch(Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,7 @@ public async Task<QueryResult<ContentStructure>> QueryAsync(StructureQuery query

var restQueryResult = await response.Content.ReadFromJsonAsync<QueryResult<RestContentStructure>>();

QueryResult<ContentStructure> queryResult = new QueryResult<ContentStructure>();
queryResult.Offset = restQueryResult.Offset;
queryResult.Count = restQueryResult.Count;
queryResult.TotalCount = restQueryResult.TotalCount;
queryResult.Items = restQueryResult.Items.Select(x => x.ToModel()).ToList();

return queryResult;
return restQueryResult.Convert(x => x.ToModel());
}
catch(Exception ex)
{
Expand All @@ -78,13 +72,7 @@ public async Task<QueryResult<ContentNode>> QueryAsync(NodesQuery query)

var restQueryResult = await response.Content.ReadFromJsonAsync<QueryResult<RestContentNode>>();

QueryResult<ContentNode> queryResult = new QueryResult<ContentNode>();
queryResult.Offset = restQueryResult.Offset;
queryResult.Count = restQueryResult.Count;
queryResult.TotalCount = restQueryResult.TotalCount;
queryResult.Items = restQueryResult.Items.Select(x => x.ToModel()).ToList();

return queryResult;
return restQueryResult.Convert(x => x.ToModel());
}

public async Task CreateAsync(ContentNode node)
Expand All @@ -98,8 +86,6 @@ public async Task CreateAsync(ContentNode node)

public async Task UpdateAsync(ContentNode node)
{
string type = node.GetType().Name;

await Client.PutAsJsonAsync($"api/node/{node.Id}", node);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public async Task<QueryResult<WebHook>> QueryAsync(WebHookQuery query)

QueryResult<RestWebHook>? result = await response.Content.ReadFromJsonAsync<QueryResult<RestWebHook>>();

IList<WebHook> items = result.Items.Select(x => x.ToModel()).ToList();

return new QueryResult<WebHook>() { Items = items };
return result.Convert(x => x.ToModel());
}
}
2 changes: 1 addition & 1 deletion src/DragonFly.API.Client/ClientAPI/ClientContentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace DragonFly.API.Client;
/// <summary>
/// ContentService
/// </summary>
public partial class ClientContentService : IContentStorage, ISchemaStorage, IAssetStorage, IAssetFolderStorage, IWebHookStorage, IStructureStorage
public partial class ClientContentService
{
public ClientContentService(HttpClient httpClient)
{
Expand Down
46 changes: 25 additions & 21 deletions src/DragonFly.API/Extensions/AssetApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,14 @@ public static void MapAssetRestApi(this IEndpointRouteBuilder endpoints)

private static async Task<QueryResult<RestAsset>> MapQuery(HttpContext context, IAssetStorage storage, AssetQuery query)
{
QueryResult<Asset> assets = await storage.QueryAsync(query);
QueryResult<Asset> queryResult = await storage.QueryAsync(query);

QueryResult<RestAsset> queryResult = new QueryResult<RestAsset>();
queryResult.Offset = assets.Offset;
queryResult.Count = assets.Count;
queryResult.TotalCount = assets.TotalCount;
queryResult.Items = assets.Items.Select(x => x.ToRest()).ToList();

return queryResult;
return queryResult.Convert(x => x.ToRest());
}

private static async Task<RestAsset> MapGet(HttpContext context, IAssetStorage storage, Guid id)
{
Asset entity = await storage.GetAssetAsync(id);
Asset entity = await storage.GetRequiredAssetAsync(id);

RestAsset restAsset = entity.ToRest();

Expand All @@ -67,31 +61,41 @@ private static async Task MapUpdate(HttpContext context, IAssetStorage storage,

private static async Task MapPublish(HttpContext context, IAssetStorage storage, Guid id)
{
await storage.PublishAsync(id);
Asset entity = await storage.GetRequiredAssetAsync(id);

await storage.PublishAsync(entity);
}

private static async Task MapDownload(HttpContext context, IAssetStorage storage, Guid id)
{
Asset asset = await storage.GetAssetAsync(id);
Asset asset = await storage.GetRequiredAssetAsync(id);

using (Stream assetStream = await storage.GetStreamAsync(id))
{
context.Response.GetTypedHeaders().CacheControl = new CacheControlHeaderValue() { Public = true, MaxAge = TimeSpan.FromDays(30) };
context.Response.GetTypedHeaders().ETag = new EntityTagHeaderValue($"\"{asset.Hash}\"");
context.Response.ContentType = asset.MimeType;
context.Response.ContentLength = assetStream.Length;
using Stream assetStream = await storage.GetStreamAsync(asset);

await assetStream.CopyToAsync(context.Response.Body);
}
context.Response.GetTypedHeaders().CacheControl = new CacheControlHeaderValue() { Public = true, MaxAge = TimeSpan.FromDays(30) };
context.Response.GetTypedHeaders().ETag = new EntityTagHeaderValue($"\"{asset.Hash}\"");
context.Response.ContentType = asset.MimeType;
context.Response.ContentLength = assetStream.Length;

await assetStream.CopyToAsync(context.Response.Body);
}

private static async Task MapUpload(HttpContext context, IAssetStorage storage, Guid id)
{
await storage.UploadAsync(id, context.Request.ContentType, context.Request.Body);
if (context.Request.ContentType == null)
{
throw new Exception("Content-type was not set.");
}

Asset asset = await storage.GetRequiredAssetAsync(id);

await storage.UploadAsync(asset, context.Request.ContentType, context.Request.Body);
}

private static async Task MapRefreshMetadata(HttpContext context, IAssetStorage storage, Guid id)
{
await storage.ApplyMetadataAsync(id);
Asset asset = await storage.GetRequiredAssetAsync(id);

await storage.ApplyMetadataAsync(asset);
}
}
25 changes: 16 additions & 9 deletions src/DragonFly.API/Extensions/AssetFolderApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// MIT License

using DragonFly.Assets.Query;
using DragonFly.Query;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
Expand All @@ -22,22 +23,23 @@ public static void MapAssetFolderRestApi(this IEndpointRouteBuilder endpoints)
groupRoute.MapPost("", MapCreate);
}

private static async Task<IEnumerable<RestAssetFolder>> MapQuery(HttpContext context, IAssetFolderStorage storage, AssetFolderQuery query)
private static async Task<QueryResult<RestAssetFolder>> MapQuery(HttpContext context, IAssetFolderStorage storage, AssetFolderQuery query)
{
IEnumerable<AssetFolder> assets = await storage.QueryAsync(query);
QueryResult<AssetFolder> queryResult = await storage.QueryAsync(query);

IEnumerable<RestAssetFolder> result = assets.Select(x => x.ToRest()).ToList();

return result;
return queryResult.Convert(x => x.ToRest());
}

private static async Task<RestAssetFolder> MapGet(HttpContext context, IAssetFolderStorage storage, Guid id)
{
AssetFolder entity = await storage.GetAssetFolderAsync(id);
AssetFolder? entity = await storage.GetAssetFolderAsync(id);

RestAssetFolder restAsset = entity.ToRest();
if (entity == null)
{
throw new Exception("Asset folder not found.");
}

return restAsset;
return entity.ToRest();
}

private static async Task MapCreate(HttpContext context, IAssetFolderStorage storage, RestAssetFolder input)
Expand All @@ -47,7 +49,12 @@ private static async Task MapCreate(HttpContext context, IAssetFolderStorage sto

private static async Task MapDelete(HttpContext context, IAssetFolderStorage storage, Guid id)
{
AssetFolder entity = await storage.GetAssetFolderAsync(id);
AssetFolder? entity = await storage.GetAssetFolderAsync(id);

if (entity == null)
{
throw new Exception("Asset folder not found.");
}

await storage.DeleteAsync(entity);
}
Expand Down
Loading

0 comments on commit 5a13835

Please sign in to comment.