From c4c3786f7dab2db0cb21f87d23f16668373c0acf Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Mon, 13 Jan 2025 15:48:51 +0000 Subject: [PATCH] Use Shared Response Handling --- .../FileProcessor.BusinessLogic.csproj | 14 +-- .../FileProcessor.Client.csproj | 3 +- FileProcessor.Client/FileProcessorClient.cs | 99 ++----------------- .../FileProcessor.File.DomainEvents.csproj | 2 +- .../FileProcessor.FileAggregate.csproj | 4 +- ...rocessor.FileImportLog.DomainEvents.csproj | 2 +- ...ileProcessor.FileImportLogAggregate.csproj | 2 +- ...rocessor.IntegrationTesting.Helpers.csproj | 4 +- .../FileProcessor.IntegrationTests.csproj | 18 ++-- FileProcessor/FileProcessor.csproj | 2 +- 10 files changed, 34 insertions(+), 116 deletions(-) diff --git a/FileProcessor.BusinessLogic/FileProcessor.BusinessLogic.csproj b/FileProcessor.BusinessLogic/FileProcessor.BusinessLogic.csproj index d1a63b6..dcede87 100644 --- a/FileProcessor.BusinessLogic/FileProcessor.BusinessLogic.csproj +++ b/FileProcessor.BusinessLogic/FileProcessor.BusinessLogic.csproj @@ -5,16 +5,16 @@ - + - - - - + + + + - + - + diff --git a/FileProcessor.Client/FileProcessor.Client.csproj b/FileProcessor.Client/FileProcessor.Client.csproj index e07f520..b0c5b99 100644 --- a/FileProcessor.Client/FileProcessor.Client.csproj +++ b/FileProcessor.Client/FileProcessor.Client.csproj @@ -6,7 +6,8 @@ - + + diff --git a/FileProcessor.Client/FileProcessorClient.cs b/FileProcessor.Client/FileProcessorClient.cs index e357fba..f6045ee 100644 --- a/FileProcessor.Client/FileProcessorClient.cs +++ b/FileProcessor.Client/FileProcessorClient.cs @@ -1,4 +1,6 @@ -namespace FileProcessor.Client { +using Shared.Results; + +namespace FileProcessor.Client { using System; using System.Collections.Generic; using System.Net.Http; @@ -14,7 +16,7 @@ /// /// /// - /// + /// /// public class FileProcessorClient : ClientProxyBase, IFileProcessorClient { #region Fields @@ -76,7 +78,7 @@ public async Task> GetFile(String accessToken, return ResultHelpers.CreateFailure(result); ResponseData responseData = - JsonConvert.DeserializeObject>(result.Data); + HandleResponseContent(result.Data); // call was successful so now deserialise the body to the response object response = responseData.Data; @@ -128,7 +130,7 @@ public async Task> GetFileImportLog(String accessToken, return ResultHelpers.CreateFailure(result); ResponseData responseData = - JsonConvert.DeserializeObject>(result.Data); + HandleResponseContent(result.Data); // call was successful so now deserialise the body to the response object response = responseData.Data; @@ -183,7 +185,7 @@ public async Task> GetFileImportLogs(String accessToke return ResultHelpers.CreateFailure(result); ResponseData responseData = - JsonConvert.DeserializeObject>(result.Data); + HandleResponseContent(result.Data); // call was successful so now deserialise the body to the response object response = responseData.Data; @@ -241,7 +243,7 @@ public async Task> UploadFile(String accessToken, return ResultHelpers.CreateFailure(result); ResponseData responseData = - JsonConvert.DeserializeObject>(result.Data); + HandleResponseContent(result.Data); // call was successful so now deserialise the body to the response object response = responseData.Data; @@ -271,89 +273,4 @@ private String BuildRequestUrl(String route) { #endregion } - - internal class ResponseData { - public T Data { get; set; } - } - - public static class ResultHelpers { - public static Result CreateFailure(Result result) { - if (result.IsFailed) { - return BuildResult(result.Status, result.Message, result.Errors); - } - - return Result.Failure("Unknown Failure"); - } - - public static Result CreateFailure(Result result) { - if (result.IsFailed) { - return BuildResult(result.Status, result.Message, result.Errors); - } - - return Result.Failure("Unknown Failure"); - } - - private static Result BuildResult(ResultStatus status, - String messageValue, - IEnumerable errorList) { - return (status, messageValue, errorList) switch { - // If the status is NotFound and there are errors, return the errors - (ResultStatus.NotFound, _, List errors) when errors is { Count: > 0 } => - Result.NotFound(errors), - - // If the status is NotFound and the message is not null or empty, return the message - (ResultStatus.NotFound, string message, _) when !string.IsNullOrEmpty(message) => Result.NotFound( - message), - - // If the status is Failure and there are errors, return the errors - (ResultStatus.Failure, _, List errors) when errors is { Count: > 0 } => Result.Failure(errors), - - // If the status is Failure and the message is not null or empty, return the message - (ResultStatus.Failure, string message, _) when !string.IsNullOrEmpty(message) => - Result.Failure(message), - - // If the status is Forbidden and there are errors, return the errors - (ResultStatus.Forbidden, _, List errors) when errors is { Count: > 0 } => Result.Forbidden( - errors), - - // If the status is Forbidden and the message is not null or empty, return the message - (ResultStatus.Forbidden, string message, _) when !string.IsNullOrEmpty(message) => Result.NotFound( - message), - //### - // If the status is Invalid and there are errors, return the errors - (ResultStatus.Invalid, _, List errors) when errors is { Count: > 0 } => Result.Invalid(errors), - - // If the status is Invalid and the message is not null or empty, return the message - (ResultStatus.Invalid, string message, _) when !string.IsNullOrEmpty(message) => - Result.Invalid(message), - - // If the status is Unauthorized and there are errors, return the errors - (ResultStatus.Unauthorized, _, List errors) when errors is { Count: > 0 } => - Result.Unauthorized(errors), - - // If the status is Unauthorized and the message is not null or empty, return the message - (ResultStatus.Unauthorized, string message, _) when !string.IsNullOrEmpty(message) => Result - .Unauthorized(message), - - // If the status is Conflict and there are errors, return the errors - (ResultStatus.Conflict, _, List errors) when errors is { Count: > 0 } => - Result.Conflict(errors), - - // If the status is Conflict and the message is not null or empty, return the message - (ResultStatus.Conflict, string message, _) when !string.IsNullOrEmpty(message) => Result.Conflict( - message), - - // If the status is CriticalError and there are errors, return the errors - (ResultStatus.CriticalError, _, List errors) when errors is { Count: > 0 } => Result - .CriticalError(errors), - - // If the status is CriticalError and the message is not null or empty, return the message - (ResultStatus.CriticalError, string message, _) when !string.IsNullOrEmpty(message) => Result - .CriticalError(message), - - // Default case, return a generic failure message - _ => Result.Failure("An unexpected error occurred.") - }; - } - } } \ No newline at end of file diff --git a/FileProcessor.File.DomainEvents/FileProcessor.File.DomainEvents.csproj b/FileProcessor.File.DomainEvents/FileProcessor.File.DomainEvents.csproj index f8b34ed..e4ea0f7 100644 --- a/FileProcessor.File.DomainEvents/FileProcessor.File.DomainEvents.csproj +++ b/FileProcessor.File.DomainEvents/FileProcessor.File.DomainEvents.csproj @@ -6,7 +6,7 @@ - + diff --git a/FileProcessor.FileAggregate/FileProcessor.FileAggregate.csproj b/FileProcessor.FileAggregate/FileProcessor.FileAggregate.csproj index c69fdd8..a5d1b2c 100644 --- a/FileProcessor.FileAggregate/FileProcessor.FileAggregate.csproj +++ b/FileProcessor.FileAggregate/FileProcessor.FileAggregate.csproj @@ -6,8 +6,8 @@ - - + + diff --git a/FileProcessor.FileImportLog.DomainEvents/FileProcessor.FileImportLog.DomainEvents.csproj b/FileProcessor.FileImportLog.DomainEvents/FileProcessor.FileImportLog.DomainEvents.csproj index ae2540a..8bdddc8 100644 --- a/FileProcessor.FileImportLog.DomainEvents/FileProcessor.FileImportLog.DomainEvents.csproj +++ b/FileProcessor.FileImportLog.DomainEvents/FileProcessor.FileImportLog.DomainEvents.csproj @@ -6,7 +6,7 @@ - + diff --git a/FileProcessor.FileImportLogAggregate/FileProcessor.FileImportLogAggregate.csproj b/FileProcessor.FileImportLogAggregate/FileProcessor.FileImportLogAggregate.csproj index 4ddb107..07d7be8 100644 --- a/FileProcessor.FileImportLogAggregate/FileProcessor.FileImportLogAggregate.csproj +++ b/FileProcessor.FileImportLogAggregate/FileProcessor.FileImportLogAggregate.csproj @@ -6,7 +6,7 @@ - + diff --git a/FileProcessor.IntegrationTesting.Helpers/FileProcessor.IntegrationTesting.Helpers.csproj b/FileProcessor.IntegrationTesting.Helpers/FileProcessor.IntegrationTesting.Helpers.csproj index 8e3fc19..dcc4a46 100644 --- a/FileProcessor.IntegrationTesting.Helpers/FileProcessor.IntegrationTesting.Helpers.csproj +++ b/FileProcessor.IntegrationTesting.Helpers/FileProcessor.IntegrationTesting.Helpers.csproj @@ -7,8 +7,8 @@ - - + + diff --git a/FileProcessor.IntegrationTests/FileProcessor.IntegrationTests.csproj b/FileProcessor.IntegrationTests/FileProcessor.IntegrationTests.csproj index b654650..ec42345 100644 --- a/FileProcessor.IntegrationTests/FileProcessor.IntegrationTests.csproj +++ b/FileProcessor.IntegrationTests/FileProcessor.IntegrationTests.csproj @@ -11,12 +11,12 @@ - + - + - + @@ -24,13 +24,13 @@ - - - - + + + + - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/FileProcessor/FileProcessor.csproj b/FileProcessor/FileProcessor.csproj index fb56caa..b864873 100644 --- a/FileProcessor/FileProcessor.csproj +++ b/FileProcessor/FileProcessor.csproj @@ -18,7 +18,7 @@ - +