From 07efa87fe5abfb64e020379031a2f1208a8305bc Mon Sep 17 00:00:00 2001 From: rockstardev Date: Sun, 14 Jun 2020 23:17:21 -0500 Subject: [PATCH] Refactoring conversion of data to view model, switching to FillViewModel --- .../Notifications/NewVersionNotification.cs | 15 +++------------ .../Events/Notifications/NotificationBase.cs | 2 +- .../NotificationViewModels/IndexViewModel.cs | 17 ++++++++++++++--- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/BTCPayServer/Events/Notifications/NewVersionNotification.cs b/BTCPayServer/Events/Notifications/NewVersionNotification.cs index e4367e2bc..5f16ec564 100644 --- a/BTCPayServer/Events/Notifications/NewVersionNotification.cs +++ b/BTCPayServer/Events/Notifications/NewVersionNotification.cs @@ -10,19 +10,10 @@ namespace BTCPayServer.Events.Notifications public string Version { get; set; } - public override NotificationViewModel ToViewModel(NotificationData data) + public override void FillViewModel(NotificationViewModel vm) { - var casted = JsonConvert.DeserializeObject(ZipUtils.Unzip(data.Blob)); - var obj = new NotificationViewModel - { - Id = data.Id, - Created = data.Created, - Body = $"New version {casted.Version} released!", - ActionLink = "https://github.com/btcpayserver/btcpayserver/releases/tag/v" + casted.Version, - Seen = data.Seen - }; - - return obj; + vm.Body = $"New version {Version} released!"; + vm.ActionLink = $"https://github.com/btcpayserver/btcpayserver/releases/tag/v{Version}"; } } } diff --git a/BTCPayServer/Events/Notifications/NotificationBase.cs b/BTCPayServer/Events/Notifications/NotificationBase.cs index 0a32bcc28..44c992366 100644 --- a/BTCPayServer/Events/Notifications/NotificationBase.cs +++ b/BTCPayServer/Events/Notifications/NotificationBase.cs @@ -27,6 +27,6 @@ namespace BTCPayServer.Events.Notifications return data; } - public abstract NotificationViewModel ToViewModel(NotificationData data); + public abstract void FillViewModel(NotificationViewModel data); } } diff --git a/BTCPayServer/Models/NotificationViewModels/IndexViewModel.cs b/BTCPayServer/Models/NotificationViewModels/IndexViewModel.cs index 0196e1551..7b4fbfb91 100644 --- a/BTCPayServer/Models/NotificationViewModels/IndexViewModel.cs +++ b/BTCPayServer/Models/NotificationViewModels/IndexViewModel.cs @@ -31,10 +31,21 @@ namespace BTCPayServer.Models.NotificationViewModels { var baseType = typeof(NotificationBase); - var typeName = baseType.FullName.Replace(nameof(NotificationBase), data.NotificationType, StringComparison.OrdinalIgnoreCase); - var instance = Activator.CreateInstance(baseType.Assembly.GetType(typeName)) as NotificationBase; + var fullTypeName = baseType.FullName.Replace(nameof(NotificationBase), data.NotificationType, StringComparison.OrdinalIgnoreCase); + var parsedType = baseType.Assembly.GetType(fullTypeName); + var instance = Activator.CreateInstance(parsedType) as NotificationBase; - return instance.ToViewModel(data); + var casted = JsonConvert.DeserializeObject(ZipUtils.Unzip(data.Blob), parsedType); + var obj = new NotificationViewModel + { + Id = data.Id, + Created = data.Created, + Seen = data.Seen + }; + + instance.FillViewModel(obj); + + return obj; } } }