diff --git a/BTCPayServer.Abstractions/Extensions/Extensions.cs b/BTCPayServer.Abstractions/Extensions/Extensions.cs deleted file mode 100644 index d8c762fb1..000000000 --- a/BTCPayServer.Abstractions/Extensions/Extensions.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Text.Json; -using BTCPayServer.Abstractions.Models; -using Microsoft.AspNetCore.Mvc.ViewFeatures; - -namespace BTCPayServer.Abstractions.Extensions -{ - public static class SetStatusMessageModelExtensions - { - public static void SetStatusMessageModel(this ITempDataDictionary tempData, StatusMessageModel statusMessage) - { - if (statusMessage == null) - { - tempData.Remove("StatusMessageModel"); - return; - } - - tempData["StatusMessageModel"] = JsonSerializer.Serialize(statusMessage, new JsonSerializerOptions()); - } - } -} diff --git a/BTCPayServer.Abstractions/Extensions/ITempDataDictionaryExtensions.cs b/BTCPayServer.Abstractions/Extensions/ITempDataDictionaryExtensions.cs new file mode 100644 index 000000000..ddbd1679c --- /dev/null +++ b/BTCPayServer.Abstractions/Extensions/ITempDataDictionaryExtensions.cs @@ -0,0 +1,59 @@ +using System.Text.Json; +using BTCPayServer.Abstractions.Constants; +using BTCPayServer.Abstractions.Models; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using Newtonsoft.Json.Linq; + +namespace BTCPayServer.Abstractions.Extensions; + +public static class SetStatusMessageModelExtensions +{ + public static void SetStatusMessageModel(this ITempDataDictionary tempData, StatusMessageModel statusMessage) + { + if (statusMessage == null) + { + tempData.Remove("StatusMessageModel"); + return; + } + + tempData["StatusMessageModel"] = JsonSerializer.Serialize(statusMessage, new JsonSerializerOptions()); + } + + public static StatusMessageModel GetStatusMessageModel(this ITempDataDictionary tempData) + { + tempData.TryGetValue(WellKnownTempData.SuccessMessage, out var successMessage); + tempData.TryGetValue(WellKnownTempData.ErrorMessage, out var errorMessage); + tempData.TryGetValue("StatusMessageModel", out var model); + if (successMessage != null || errorMessage != null) + { + var parsedModel = new StatusMessageModel(); + parsedModel.Message = (string)successMessage ?? (string)errorMessage; + if (successMessage != null) + { + parsedModel.Severity = StatusMessageModel.StatusSeverity.Success; + } + else + { + parsedModel.Severity = StatusMessageModel.StatusSeverity.Error; + } + return parsedModel; + } + else if (model != null && model is string str) + { + return JObject.Parse(str).ToObject(); + } + return null; + } + + public static bool HasStatusMessage(this ITempDataDictionary tempData) + { + return (tempData.Peek(WellKnownTempData.SuccessMessage) ?? + tempData.Peek(WellKnownTempData.ErrorMessage) ?? + tempData.Peek("StatusMessageModel")) != null; + } + + public static bool HasErrorMessage(this ITempDataDictionary tempData) + { + return GetStatusMessageModel(tempData)?.Severity == StatusMessageModel.StatusSeverity.Error; + } +} diff --git a/BTCPayServer.Tests/TestAccount.cs b/BTCPayServer.Tests/TestAccount.cs index 044e98c9a..f6d9891c0 100644 --- a/BTCPayServer.Tests/TestAccount.cs +++ b/BTCPayServer.Tests/TestAccount.cs @@ -5,6 +5,7 @@ using System.Net.Http; using System.Text; using System.Threading; using System.Threading.Tasks; +using BTCPayServer.Abstractions.Extensions; using BTCPayServer.BIP78.Sender; using BTCPayServer.Client; using BTCPayServer.Client.Models; diff --git a/BTCPayServer.Tests/UnitTest1.cs b/BTCPayServer.Tests/UnitTest1.cs index 24497270f..0966e9c90 100644 --- a/BTCPayServer.Tests/UnitTest1.cs +++ b/BTCPayServer.Tests/UnitTest1.cs @@ -13,6 +13,7 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Abstractions.Constants; using BTCPayServer.Abstractions.Contracts; +using BTCPayServer.Abstractions.Extensions; using BTCPayServer.Abstractions.Models; using BTCPayServer.Client; using BTCPayServer.Client.Models; diff --git a/BTCPayServer/Views/Shared/_StatusMessage.cshtml b/BTCPayServer/Views/Shared/_StatusMessage.cshtml index 19ebf5f4c..f06889eca 100644 --- a/BTCPayServer/Views/Shared/_StatusMessage.cshtml +++ b/BTCPayServer/Views/Shared/_StatusMessage.cshtml @@ -1,3 +1,4 @@ +@using BTCPayServer.Abstractions.Extensions @{ var parsedModel = TempData.GetStatusMessageModel(); }