Skip to content

Commit

Permalink
Use result pattern and decorator pattern for storages
Browse files Browse the repository at this point in the history
  • Loading branch information
usercode committed Apr 24, 2024
1 parent a842791 commit c726ffb
Show file tree
Hide file tree
Showing 74 changed files with 1,784 additions and 498 deletions.
38 changes: 26 additions & 12 deletions src/DragonFly.API.Client/ClientAPI/ClientContentService .Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Net.Http.Json;
using System.Net.Http.Headers;
using DragonFly.Query;
using Results;
using System.Reflection.Metadata.Ecma335;

namespace DragonFly.API.Client;

Expand All @@ -13,7 +15,7 @@ namespace DragonFly.API.Client;
/// </summary>
public partial class ClientContentService : IAssetStorage
{
public async Task<QueryResult<Asset>> QueryAsync(AssetQuery assetQuery)
public async Task<Result<QueryResult<Asset>>> QueryAsync(AssetQuery assetQuery)
{
var response = await Client.PostAsJsonAsync($"api/asset/query", assetQuery, ApiJsonSerializerDefault.Options);

Expand All @@ -22,7 +24,7 @@ public async Task<QueryResult<Asset>> QueryAsync(AssetQuery assetQuery)
return result.Convert(x => x.ToModel());
}

public async Task UploadAsync(Asset asset, string mimetype, Stream stream)
public async Task<Result> UploadAsync(Asset asset, string mimetype, Stream stream)
{
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, $"api/asset/{asset.Id}/upload");
requestMessage.Content = new StreamContent(stream);
Expand All @@ -31,70 +33,82 @@ public async Task UploadAsync(Asset asset, string mimetype, Stream stream)
var response = await Client.SendAsync(requestMessage);

response.EnsureSuccessStatusCode();

return Result.Ok();
}

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

//assets
public async Task<Asset?> GetAssetAsync(Guid id)
public async Task<Result<Asset>> GetAssetAsync(Guid id)
{
var response = await Client.GetAsync($"api/asset/{id}");

RestAsset? restAsset = await response.Content.ReadFromJsonAsync<RestAsset>(ApiJsonSerializerDefault.Options);

if (restAsset == null)
{
return null;
return Result.Ok();
}

return restAsset.ToModel();
}

public async Task CreateAsync(Asset entity)
public async Task<Result> CreateAsync(Asset entity)
{
var response = await Client.PostAsJsonAsync($"api/asset", entity.ToRest());

ResourceCreated? result = await response.Content.ReadFromJsonAsync<ResourceCreated>(ApiJsonSerializerDefault.Options) ?? throw new ArgumentNullException();

entity.Id = result.Id;

return Result.Ok();
}

public async Task PublishAsync(Asset asset)
public async Task<Result> PublishAsync(Asset asset)
{
var response = await Client.PostAsync($"api/asset/{asset.Id}/publish", new StringContent(string.Empty));

response.EnsureSuccessStatusCode();

return Result.Ok();
}

public async Task UpdateAsync(Asset entity)
public async Task<Result> UpdateAsync(Asset entity)
{
var response = await Client.PutAsJsonAsync($"api/asset", entity.ToRest(), ApiJsonSerializerDefault.Options);

response.EnsureSuccessStatusCode();

return Result.Ok();
}

public async Task DeleteAsync(Asset asset)
public async Task<Result> DeleteAsync(Asset asset)
{
var response = await Client.DeleteAsync($"api/asset/{asset.Id}");

response.EnsureSuccessStatusCode();

return Result.Ok();
}

public async Task ApplyMetadataAsync(Asset asset)
public async Task<Result> ApplyMetadataAsync(Asset asset)
{
var response = await Client.PostAsync($"api/asset/{asset.Id}/metadata", new StringContent(string.Empty));

response.EnsureSuccessStatusCode();

return Result.Ok();
}

public async Task<IBackgroundTaskInfo> ApplyMetadataAsync(AssetQuery query)
public async Task<Result<BackgroundTaskInfo>> ApplyMetadataAsync(AssetQuery query)
{
var response = await Client.PostAsJsonAsync($"api/asset/metadata", query, ApiJsonSerializerDefault.Options);

response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCode();

return await response.Content.ReadFromJsonAsync<BackgroundTaskInfo>(ApiJsonSerializerDefault.Options) ?? throw new ArgumentNullException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// MIT License

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

namespace DragonFly.API.Client;
Expand All @@ -12,14 +13,16 @@ namespace DragonFly.API.Client;
/// </summary>
public partial class ClientContentService : IAssetFolderStorage
{
public async Task CreateAsync(AssetFolder folder)
public async Task<Result> CreateAsync(AssetFolder folder)
{
var response = await Client.PostAsJsonAsync("api/assetfolder", folder.ToRest(), ApiJsonSerializerDefault.Options);

response.EnsureSuccessStatusCode();

return Result.Ok();
}

public async Task<AssetFolder?> GetAssetFolderAsync(Guid id)
public async Task<Result<AssetFolder?>> GetAssetFolderAsync(Guid id)
{
RestAssetFolder? entity = await Client.GetFromJsonAsync<RestAssetFolder>($"api/assetfolder/{id}", ApiJsonSerializerDefault.Options);

Expand All @@ -31,7 +34,7 @@ public async Task CreateAsync(AssetFolder folder)
return entity.ToModel();
}

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

Expand All @@ -42,15 +45,17 @@ public async Task<QueryResult<AssetFolder>> QueryAsync(AssetFolderQuery query)
return result.Convert(x => x.ToModel());
}

public Task UpdateAsync(AssetFolder folder)
public Task<Result> UpdateAsync(AssetFolder folder)
{
throw new NotImplementedException();
}

public async Task DeleteAsync(AssetFolder folder)
public async Task<Result> DeleteAsync(AssetFolder folder)
{
var response = await Client.DeleteAsync($"api/assetfolder/{folder.Id}");

response.EnsureSuccessStatusCode();

return Result.Ok();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Net.Http.Json;
using DragonFly.API.Client.BackgroundTasks;
using Results;

namespace DragonFly.API.Client;

Expand All @@ -12,7 +13,7 @@ namespace DragonFly.API.Client;
/// </summary>
public partial class ClientContentService : IBackgroundTaskService
{
public async Task<IBackgroundTaskInfo[]> GetTasksAsync()
public async Task<Result<BackgroundTaskInfo[]>> GetTasksAsync()
{
var response = await Client.PostAsync("api/task/query", new StringContent(string.Empty));

Expand All @@ -28,14 +29,14 @@ public async Task<IBackgroundTaskInfo[]> GetTasksAsync()
return result;
}

public async Task CancelAsync(int id)
public async Task<Result> CancelAsync(int id)
{
var response = await Client.PostAsync($"api/task/{id}/cancel", new StringContent(string.Empty));

response.EnsureSuccessStatusCode();
return await response.ToResultAsync();
}

public async Task<IBackgroundTaskNotificationProvider> StartNotificationProviderAsync()
public async Task<Result<IBackgroundTaskNotificationProvider>> StartNotificationProviderAsync()
{
SignalRBackgroundTaskNotificationProvider provider = new SignalRBackgroundTaskNotificationProvider(NavigationManager);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

namespace DragonFly.API.Client;

Expand All @@ -12,63 +13,67 @@ namespace DragonFly.API.Client;
/// </summary>
public partial class ClientContentService : IContentStorage
{
public async Task<ContentItem?> GetContentAsync(string schema, Guid id)
public async Task<Result<ContentItem>> GetContentAsync(string schema, Guid id)
{
RestContentItem? entity = await Client.GetFromJsonAsync<RestContentItem>($"api/content/{schema}/{id}", ApiJsonSerializerDefault.Options);

if (entity == null)
{
return null;
}
ArgumentNullException.ThrowIfNull(entity);

return entity.ToModel();
}

public async Task CreateAsync(ContentItem entity)
public async Task<Result> CreateAsync(ContentItem entity)
{
var response = await Client.PostAsJsonAsync($"api/content", entity.ToRest(), ApiJsonSerializerDefault.Options);

response.EnsureSuccessStatusCode();

ResourceCreated? result = await response.Content.ReadFromJsonAsync<ResourceCreated>(ApiJsonSerializerDefault.Options);

if (result == null)
{
throw new Exception();
}
ArgumentNullException.ThrowIfNull(result);

entity.Id = result.Id;

return Result.Ok();
}

public async Task UpdateAsync(ContentItem entity)
public async Task<Result> UpdateAsync(ContentItem entity)
{
var response = await Client.PutAsJsonAsync($"api/content", entity.ToRest(), ApiJsonSerializerDefault.Options);

response.EnsureSuccessStatusCode();

return Result.Ok();
}

public async Task DeleteAsync(ContentItem entity)
public async Task<Result> DeleteAsync(ContentItem entity)
{
var response = await Client.DeleteAsync($"api/content/{entity.Schema.Name}/{entity.Id}");

response.EnsureSuccessStatusCode();

return Result.Ok();
}

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

response.EnsureSuccessStatusCode();

return Result.Ok();
}

public async Task UnpublishAsync(ContentItem entity)
public async Task<Result> UnpublishAsync(ContentItem entity)
{
var response = await Client.PostAsync($"api/content/{entity.Schema.Name}/{entity.Id}/unpublish", new StringContent(string.Empty));

response.EnsureSuccessStatusCode();

return Result.Ok();
}

public async Task<QueryResult<ContentItem>> QueryAsync(ContentQuery queryParameters)
public async Task<Result<QueryResult<ContentItem>>> QueryAsync(ContentQuery queryParameters)
{
HttpResponseMessage response = await Client.PostAsJsonAsync($"api/content/query", queryParameters, ApiJsonSerializerDefault.Options);

Expand All @@ -77,7 +82,7 @@ public async Task<QueryResult<ContentItem>> QueryAsync(ContentQuery queryParamet
return queryResult.Convert(x => x.ToModel());
}

public async Task<IBackgroundTaskInfo> PublishQueryAsync(ContentQuery query)
public async Task<Result<BackgroundTaskInfo>> PublishQueryAsync(ContentQuery query)
{
var response = await Client.PostAsJsonAsync($"api/content/publish", query, ApiJsonSerializerDefault.Options);

Expand All @@ -86,7 +91,7 @@ public async Task<IBackgroundTaskInfo> PublishQueryAsync(ContentQuery query)
return await response.Content.ReadFromJsonAsync<BackgroundTaskInfo>(ApiJsonSerializerDefault.Options) ?? throw new ArgumentNullException();
}

public async Task<IBackgroundTaskInfo> UnpublishQueryAsync(ContentQuery query)
public async Task<Result<BackgroundTaskInfo>> UnpublishQueryAsync(ContentQuery query)
{
var response = await Client.PostAsJsonAsync($"api/content/unpublish", query, ApiJsonSerializerDefault.Options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

namespace DragonFly.API.Client;

Expand All @@ -12,7 +13,7 @@ namespace DragonFly.API.Client;
/// </summary>
public partial class ClientContentService : ISchemaStorage
{
public async Task<ContentSchema?> GetSchemaAsync(Guid id)
public async Task<Result<ContentSchema?>> GetSchemaAsync(Guid id)
{
var response = await Client.GetAsync($"api/schema/{id}");

Expand All @@ -26,7 +27,7 @@ public partial class ClientContentService : ISchemaStorage
return e.ToModel();
}

public async Task<ContentSchema?> GetSchemaAsync(string name)
public async Task<Result<ContentSchema?>> GetSchemaAsync(string name)
{
var response = await Client.GetAsync($"api/schema/{name}");

Expand All @@ -40,26 +41,32 @@ public partial class ClientContentService : ISchemaStorage
return e.ToModel();
}

public async Task CreateAsync(ContentSchema entity)
public async Task<Result> CreateAsync(ContentSchema entity)
{
var response = await Client.PostAsJsonAsync($"api/schema", entity.ToRest(), ApiJsonSerializerDefault.Options);

var result = await response.Content.ReadFromJsonAsync<ResourceCreated>(ApiJsonSerializerDefault.Options) ?? throw new ArgumentNullException();

entity.Id = result.Id;

return Result.Ok();
}

public async Task UpdateAsync(ContentSchema entity)
public async Task<Result> UpdateAsync(ContentSchema entity)
{
await Client.PutAsJsonAsync($"api/schema", entity.ToRest(), ApiJsonSerializerDefault.Options);

return Result.Ok();
}

public async Task DeleteAsync(ContentSchema entity)
public async Task<Result> DeleteAsync(ContentSchema entity)
{
await Client.DeleteAsync($"api/schema/{entity.Id}");

return Result.Ok();
}

public async Task<QueryResult<ContentSchema>> QuerySchemasAsync()
public async Task<Result<QueryResult<ContentSchema>>> QuerySchemasAsync()
{
var response = await Client.PostAsync("api/schema/query", new StringContent(string.Empty));

Expand Down
Loading

0 comments on commit c726ffb

Please sign in to comment.