From d8e4b40d64a3fe54780d8500cbcadb42af27d860 Mon Sep 17 00:00:00 2001 From: Bruno de Souza Melo Date: Mon, 9 Sep 2024 10:37:34 -0300 Subject: [PATCH] Major fix in Result Wrapper. --- src/NuvTools.Common/NuvTools.Common.csproj | 2 +- src/NuvTools.Common/ResultWrapper/IResult.cs | 2 +- src/NuvTools.Common/ResultWrapper/Result.cs | 80 +++++++++--------- .../ResultWrapper/ResultExtensions.cs | 1 - .../ResultWrapper/ResultTests.cs | 82 +++++++++++++++++-- 5 files changed, 117 insertions(+), 50 deletions(-) diff --git a/src/NuvTools.Common/NuvTools.Common.csproj b/src/NuvTools.Common/NuvTools.Common.csproj index 8df0908..836bda3 100644 --- a/src/NuvTools.Common/NuvTools.Common.csproj +++ b/src/NuvTools.Common/NuvTools.Common.csproj @@ -10,7 +10,7 @@ True NuvTools.Common.snk Common library for Web, Desktop and Mobile (MAUI) applications. - 8.1.0 + 8.1.2 True True icon.png diff --git a/src/NuvTools.Common/ResultWrapper/IResult.cs b/src/NuvTools.Common/ResultWrapper/IResult.cs index cbd571c..7fea648 100644 --- a/src/NuvTools.Common/ResultWrapper/IResult.cs +++ b/src/NuvTools.Common/ResultWrapper/IResult.cs @@ -30,5 +30,5 @@ public interface IResult public interface IResult : IResult { - T Data { get; } + T? Data { get; } } \ No newline at end of file diff --git a/src/NuvTools.Common/ResultWrapper/Result.cs b/src/NuvTools.Common/ResultWrapper/Result.cs index 3125cdc..62dedea 100644 --- a/src/NuvTools.Common/ResultWrapper/Result.cs +++ b/src/NuvTools.Common/ResultWrapper/Result.cs @@ -1,15 +1,14 @@ - -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging; namespace NuvTools.Common.ResultWrapper; -public abstract class ResultBase : IResult +public class Result : IResult { - public bool Succeeded { get; protected set; } - public ResultType ResultType { get; protected set; } - public List Messages { get; protected set; } = []; + public bool Succeeded { get; set; } + public ResultType ResultType { get; set; } = ResultType.Success; + public List Messages { get; set; } = []; - protected static void Log(ResultType resultType, IEnumerable? messages, ILogger? logger) + protected static void Log(List? messages, ILogger? logger) { if (logger == null || messages == null) return; @@ -33,55 +32,54 @@ protected static void Log(ResultType resultType, IEnumerable? mes } } } -} -public class Result : ResultBase, IResult -{ - private Result(bool succeeded, ResultType resultType, List? messages) + protected static List ConvertToMessageDetail(IEnumerable value) { - Succeeded = succeeded; - ResultType = resultType; - Messages = messages ?? []; + return value.Select(e => new MessageDetail(e)).ToList(); } - public static IResult Create(bool succeeded, ResultType resultType, List? messages = null, ILogger? logger = null) + private static Result CreateResult(ResultType resultType, List? messages = null, ILogger? logger = null) { - Log(resultType, messages, logger); - return new Result(succeeded, resultType, messages); + Log(messages, logger); + return new Result { Succeeded = resultType == ResultType.Success, ResultType = resultType, Messages = messages ?? [] }; } - public static IResult Success(List? messages = null) => Create(true, ResultType.Success, messages); - public static IResult Fail(List? messages = null, ILogger? logger = null) => Create(false, ResultType.Error, messages, logger); - public static IResult ValidationFail(List? messages = null, ILogger? logger = null) => Create(false, ResultType.ValidationError, messages, logger); + public static IResult Fail(List? messages = null, ILogger? logger = null) => CreateResult(ResultType.Error, messages, logger); + public static IResult Fail(List messages, ILogger? logger = null) => Fail(ConvertToMessageDetail(messages), logger); + public static IResult Fail(string message, ILogger? logger = null) => Fail([message], logger); + public static IResult Fail(MessageDetail message, ILogger? logger = null) => Fail([message], logger); + + public static IResult ValidationFail(List messages, ILogger? logger = null) => CreateResult(ResultType.ValidationError, messages, logger); + public static IResult ValidationFail(List messages, ILogger? logger = null) => ValidationFail(ConvertToMessageDetail(messages), logger); + public static IResult ValidationFail(MessageDetail message, ILogger? logger = null) => ValidationFail([message], logger); + public static IResult ValidationFail(string message, ILogger? logger = null) => ValidationFail([message], logger); - public static Task SuccessAsync(List? messages = null) => Task.FromResult(Success(messages)); - public static Task FailAsync(List? messages = null, ILogger? logger = null) => Task.FromResult(Fail(messages, logger)); - public static Task ValidationFailAsync(List? messages = null, ILogger? logger = null) => Task.FromResult(ValidationFail(messages, logger)); + public static IResult Success(MessageDetail? message = null) => new Result { Succeeded = true, Messages = [message] }; + public static IResult Success(string message) => Success(new MessageDetail(message)); } -public class Result : ResultBase, IResult +public class Result : Result, IResult { - public T Data { get; private set; } + public T? Data { get; set; } - private Result(bool succeeded, ResultType resultType, T data, List? messages) : base() + private static Result CreateResult(ResultType resultType, T? data = default, List? messages = null, ILogger? logger = null) { - Succeeded = succeeded; - ResultType = resultType; - Data = data; - Messages = messages ?? []; + Log(messages, logger); + return new Result { Succeeded = resultType == ResultType.Success, ResultType = resultType, Data = data, Messages = messages ?? [] }; } - public static IResult Create(bool succeeded, ResultType resultType, T data, List? messages = null, ILogger? logger = null) - { - Log(resultType, messages, logger); - return new Result(succeeded, resultType, data, messages); - } + public static IResult Fail(List? messages = null, T? data = default, ILogger? logger = null) => CreateResult(ResultType.Error, data, messages, logger); + public static IResult Fail(List messages, T? data = default, ILogger? logger = null) => Fail(ConvertToMessageDetail(messages), data, logger); + public static IResult Fail(MessageDetail message, T? data = default, ILogger? logger = null) => Fail([message], data, logger); + public static IResult Fail(string message, T? data = default, ILogger? logger = null) => Fail([new MessageDetail(message)], data, logger); - public static IResult Success(T data, List? messages = null) => Create(true, ResultType.Success, data, messages); - public static IResult Fail(T data, List? messages = null, ILogger? logger = null) => Create(false, ResultType.Error, data, messages, logger); - public static IResult ValidationFail(T data, List? messages = null, ILogger? logger = null) => Create(false, ResultType.ValidationError, data, messages, logger); + public static IResult ValidationFail(List messages, T? data = default, ILogger? logger = null) => CreateResult(ResultType.ValidationError, data, messages, logger); + public static IResult ValidationFail(List messages, T? data = default, ILogger? logger = null) => ValidationFail(ConvertToMessageDetail(messages), data, logger); + public static IResult ValidationFail(MessageDetail message, T? data = default, ILogger? logger = null) => ValidationFail([message], data, logger); + public static IResult ValidationFail(string message, T? data = default, ILogger? logger = null) => ValidationFail([message], data, logger); - public static Task> SuccessAsync(T data, List? messages = null) => Task.FromResult(Success(data, messages)); - public static Task> FailAsync(T data, List? messages = null, ILogger? logger = null) => Task.FromResult(Fail(data, messages, logger)); - public static Task> ValidationFailAsync(T data, List? messages = null, ILogger? logger = null) => Task.FromResult(ValidationFail(data, messages, logger)); + public static IResult Success(T? data = default, MessageDetail? message = null) => CreateResult(ResultType.Success, data, [message]); + public static IResult Success(T? data, string message) => Success(data, new MessageDetail(message)); + public static new IResult Success(MessageDetail? message) => Success(default, message); + public static new IResult Success(string message) => Success(default, new MessageDetail(message)); } \ No newline at end of file diff --git a/src/NuvTools.Common/ResultWrapper/ResultExtensions.cs b/src/NuvTools.Common/ResultWrapper/ResultExtensions.cs index e359c89..137f3a3 100644 --- a/src/NuvTools.Common/ResultWrapper/ResultExtensions.cs +++ b/src/NuvTools.Common/ResultWrapper/ResultExtensions.cs @@ -5,7 +5,6 @@ namespace NuvTools.Common.ResultWrapper; public static class ResultExtensions { - private static readonly JsonSerializerOptions serializerOptions = new() { PropertyNameCaseInsensitive = true, diff --git a/tests/NuvTools.Common.Test/ResultWrapper/ResultTests.cs b/tests/NuvTools.Common.Test/ResultWrapper/ResultTests.cs index 185dc87..0edbf32 100644 --- a/tests/NuvTools.Common.Test/ResultWrapper/ResultTests.cs +++ b/tests/NuvTools.Common.Test/ResultWrapper/ResultTests.cs @@ -1,7 +1,7 @@ using NUnit.Framework; +using NUnit.Framework.Internal; using NuvTools.Common.ResultWrapper; using System.Collections.Generic; -using System.Threading.Tasks; namespace NuvTools.Common.Tests.ResultWrapper; @@ -9,18 +9,88 @@ namespace NuvTools.Common.Tests.ResultWrapper; public class ResultTests { [Test()] - public async Task GetResultOnly() + public void ValidationFail() { var list = new List() { new("aa"), new("bb") }; - await Result.ValidationFailAsync(messages: list); + Result.ValidationFail(list); + + Result.ValidationFail(new MessageDetail("Validation error")); + + Result.ValidationFail("Validation error"); + + var result = Result.ValidationFail(["Validation error"]); + Assert.That(result.Messages[0].Title, Is.EqualTo("Validation error")); } [Test()] - public async Task GetResultLong() + public void ValidationFailTyped() { - var list = new List() { new("aa"), new("bb") }; + Result.ValidationFail("Validation error"); + Result.ValidationFail(new MessageDetail("Validation error")); + + Result.ValidationFail("Validation error", 1); + Result.ValidationFail(new MessageDetail("Validation error"), 1); + + var resultTyped = Result.ValidationFail(["Validation error"]); + Assert.That(resultTyped.Messages[0].Title, Is.EqualTo("Validation error")); + + resultTyped = Result.ValidationFail(["Validation error"], 0); + Assert.That(resultTyped.Messages[0].Title, Is.EqualTo("Validation error")); + } + + [Test()] + public void Fail() + { + Result.Fail(); + + var result = Result.Fail("Not work"); + + Assert.That(result.Messages[0].Title, Is.EqualTo("Not work")); + + Result.Fail(new MessageDetail("Not work")); + + Result.Fail(["not work"]); + + Result.Fail([new MessageDetail("Not work")]); + + Assert.That(result.Messages[0].Title, Is.EqualTo("Not work")); + + } + + [Test()] + public void FailTyped() + { + var resultTyped = Result.Fail("Not work"); + Assert.That(resultTyped.Messages[0].Title, Is.EqualTo("Not work")); + } + + [Test()] + public void Success() + { + Result.Success(); + + var result = Result.Success("It works!"); + Assert.That(result.Messages[0].Title, Is.EqualTo("It works!")); + + Result.Success(new MessageDetail("It works!")); + } + + private static IResult TestSuccessTypedReturn() + { + return Result.Success(); + } + + [Test()] + public void SuccessTyped() + { + TestSuccessTypedReturn(); + + Result.Success(1); + Result.Success(1, "It works!"); - await Result.ValidationFailAsync(0L, list); + var resultTyped = Result.Success(1, new MessageDetail("It works!")); + Assert.That(resultTyped.Messages[0].Title, Is.EqualTo("It works!")); + Assert.That(resultTyped.Data, Is.EqualTo(1)); } } \ No newline at end of file